0

I'd like to run a script which does some setup then opens up a shell with that environment. So rather than then doing

$ python
>>> # do some setup
>>> # start doing what I really came here to do

I want to do

$ python my_script.py
>>> # start doing what I really came here to do
3
  • 1
    You could write the file, then type from filename import * into the shell. Commented Nov 16, 2020 at 13:32
  • Does this answer your question? stackoverflow.com/questions/22163422/… Commented Nov 16, 2020 at 13:33
  • @gmdev perhaps, but the answers I have so far are concise and relevant Commented Nov 16, 2020 at 13:38

2 Answers 2

3

Run your script with the -i argument:

python -i my_script.py

This will execute my_script.py and drop to an interactive shell afterwards.

Sign up to request clarification or add additional context in comments.

Comments

2

you can do some thing like this

import code

variables = {"test": True}
shell = code.InteractiveConsole(variables)
shell.interact()

now it will open python shell and you can access test variable directly

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.