How can I start IPython running a script?
Question:
My use case is I want to initialize some functions in a file and then start up IPython with those functions defined. Is there a way to do something like this?
ipython --run_script=myscript.py
Answers:
Per the docs, it’s trivial:
You start IPython with the command:
$ ipython [options] files
If invoked with no options, it
executes all the files listed in
sequence and drops you into the
interpreter while still acknowledging
any options you may have set in your
ipythonrc
file. This behavior is
different from standard Python, which
when called as python -i
will only
execute one file and ignore your
configuration setup.
So, just use ipython myfile.py
… and there you are!-)
In recent versions of ipython you do need to add the -i
option to get into the interactive environment afterwards. Without the -i
it just runs the code in myfile.py and returns to the prompt.
$ ipython -i myfile.py
Nowadays, you can use the startup folder of ipython, which is located in your home directory (C:users[username].ipython on Windows). Go into the default profile and you’ll see a startup folder with a README file. Just put any Python scripts in there, or if you want ipython commands, put them in a file with an .ipy extension.
You seem to be looking for ipyhton’s %run magic command.
By typing in ipython:
%run hello_world.py
you’ll run the hello.py program saved in your home directory. The functions and variables defined in that script will be accessible to you too.
You can use ipython profiles to define startup scripts that will run every time you start ipython. A full description of profiles, is given here. You can create multiple profiles with different startup files.
Assuming you only need one profile, and always want the same startup files every time you start ipython, you can simply modify the default profile. To do this, first find out where your ipython configuration directory is in an ipython session.:
In [1]: import IPython
In [2]: IPython.paths.get_ipython_dir() # As of IPython v4.0
In [3]: print(ipython_config_dir)
/home/johndoe/.config/ipython
For this example, I am using Ubuntu Linux, and the config directory is in /home/johndoe/.config/ipython
, where johndoe
is the username.
The default_profile is in the profile_default
subdirectory. Put any starting scripts in profile_default/startup
. In the example here, the full path would be /home/johndoe/.config/ipython/profile_default/startup
.
Update to @Caleb’s answer for Python 3.5 in Ubuntu 14.04: Made this answer self contained by copying relevant parts of @Caleb’s answer.
You can use ipython profiles to define startup scripts that will run every time you start ipython. A full description of profiles, is given here. You can create multiple profiles with different startup files.
Assuming you only need one profile, and always want the same startup files every time you start ipython, you can simply modify the default profile. To do this, first find out where your ipython configuration directory is in an ipython session.:
Input:
import IPython
ipython_config_dir = IPython.paths.get_ipython_dir()
print(ipython_cofig_dir)
Output:
/home/johndoe/.ipython
For this example johndoe
is the username.
Inside the /.ipython
folder, the default_profile is in the profile_default
subdirectory. Put any starting scripts in profile_default/startup. In the example here, the full path would be
/home/johndoe/.ipython/profile_default/startup
The following is for the case when you want your startup scripts to automatically be run whenever you use ipython (instead of having a script that you must specify each time you run ipython).
For recent versions (i.e. 5.1.0) of ipython, place one or more python scripts you wish to have executed in the IPYTHON_CONFIG_DIR/profile_PROFILENAME/startup
folder.
On linux, for example, you could put your python startup code into a file named ~/.ipython/profile_default/startup/10-mystartupstuff.py
if you want it to run when no ipython profile is specified.
Information on creating and using ipython profiles is available here.
My use case is I want to initialize some functions in a file and then start up IPython with those functions defined. Is there a way to do something like this?
ipython --run_script=myscript.py
Per the docs, it’s trivial:
You start IPython with the command:
$ ipython [options] files
If invoked with no options, it
executes all the files listed in
sequence and drops you into the
interpreter while still acknowledging
any options you may have set in your
ipythonrc
file. This behavior is
different from standard Python, which
when called aspython -i
will only
execute one file and ignore your
configuration setup.
So, just use ipython myfile.py
… and there you are!-)
In recent versions of ipython you do need to add the -i
option to get into the interactive environment afterwards. Without the -i
it just runs the code in myfile.py and returns to the prompt.
$ ipython -i myfile.py
Nowadays, you can use the startup folder of ipython, which is located in your home directory (C:users[username].ipython on Windows). Go into the default profile and you’ll see a startup folder with a README file. Just put any Python scripts in there, or if you want ipython commands, put them in a file with an .ipy extension.
You seem to be looking for ipyhton’s %run magic command.
By typing in ipython:
%run hello_world.py
you’ll run the hello.py program saved in your home directory. The functions and variables defined in that script will be accessible to you too.
You can use ipython profiles to define startup scripts that will run every time you start ipython. A full description of profiles, is given here. You can create multiple profiles with different startup files.
Assuming you only need one profile, and always want the same startup files every time you start ipython, you can simply modify the default profile. To do this, first find out where your ipython configuration directory is in an ipython session.:
In [1]: import IPython
In [2]: IPython.paths.get_ipython_dir() # As of IPython v4.0
In [3]: print(ipython_config_dir)
/home/johndoe/.config/ipython
For this example, I am using Ubuntu Linux, and the config directory is in /home/johndoe/.config/ipython
, where johndoe
is the username.
The default_profile is in the profile_default
subdirectory. Put any starting scripts in profile_default/startup
. In the example here, the full path would be /home/johndoe/.config/ipython/profile_default/startup
.
Update to @Caleb’s answer for Python 3.5 in Ubuntu 14.04: Made this answer self contained by copying relevant parts of @Caleb’s answer.
You can use ipython profiles to define startup scripts that will run every time you start ipython. A full description of profiles, is given here. You can create multiple profiles with different startup files.
Assuming you only need one profile, and always want the same startup files every time you start ipython, you can simply modify the default profile. To do this, first find out where your ipython configuration directory is in an ipython session.:
Input:
import IPython
ipython_config_dir = IPython.paths.get_ipython_dir()
print(ipython_cofig_dir)
Output:
/home/johndoe/.ipython
For this example johndoe
is the username.
Inside the /.ipython
folder, the default_profile is in the profile_default
subdirectory. Put any starting scripts in profile_default/startup. In the example here, the full path would be
/home/johndoe/.ipython/profile_default/startup
The following is for the case when you want your startup scripts to automatically be run whenever you use ipython (instead of having a script that you must specify each time you run ipython).
For recent versions (i.e. 5.1.0) of ipython, place one or more python scripts you wish to have executed in the IPYTHON_CONFIG_DIR/profile_PROFILENAME/startup
folder.
On linux, for example, you could put your python startup code into a file named ~/.ipython/profile_default/startup/10-mystartupstuff.py
if you want it to run when no ipython profile is specified.
Information on creating and using ipython profiles is available here.