Alexander Soare
Alexander Soare

Reputation: 3257

Is there any way to run all of or some of a py file and maintain the resulting state?

If I have an IPython notebook and a separate py file, can I run all the code in that file and then have the resulting state in the notebook? Kind of like running python -i myscript.py from the command line where the -i flag then opens up an interpreter with the state after myscript.py has run. But instead, I'm in my existing notebook session.

Upvotes: 1

Views: 37

Answers (1)

neves
neves

Reputation: 39173

Try this magic command:

%run script.py

It will run your script and all variables defined by the script will be in the global namespace of your ipython shell.

Using VSCode editor you can also run just part of your script file in a excecuting ipython shell with the shortcut SHIFT-ENTER.

The %load magic is also nice, you copy the contents of the file to your working session, but it is better while running inside a notebook.

Upvotes: 1

Related Questions