72

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
0

7 Answers 7

114

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
Sign up to request clarification or add additional context in comments.

1 Comment

As an additional note, "recent versions" here means after the 0.10.2 ~> 0.11 architectural revamp, and the "-i" option is backwards compatible to ipython 0.10.2 (I can confirm) and likely lower (I can speculate).
35

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!-)

1 Comment

This answer is now out of date. See @Maarten's answer below. You need to use a -i flag, along with any other options you want, followed by the python script name. This way, you will be left in the interpreter rather than dropping back out immediately
18

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 configuration 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.

1 Comment

I don't know if it's a recent addition, but locating the path for a given profile is done by IPython.paths.locate_profile()
16

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.

2 Comments

Thanks, nice to know about the startup dir. I recently found about exec_files in the config file options. Seems startup dir files are executed before exec_files entries. The link is informative.
~/.config/ipython/profile_default/startup on ubuntu
1

You seem to be looking for IPython'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.

1 Comment

The question is how to start ipython with a file, not to run a file within ipython.
1

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.

Comments

-1

Update to Caleb's answer for Python 3.5 in Ubuntu 14.04 (Trusty Tahr): I 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_config_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

3 Comments

As it is this answer does not answer the question. An answer has to be fully contained in itself. I suggest to complete it.
Should I just edit @Calebs answer? with this update! or make it self contained? Thanks! Have edited it though!
I don't think you should modify Calebs answer because it considerably changes it. I would add the relevant information to make your answer to answer the question by itself. Put yourself in the position of someone that read your answer without reading Calebs's, and see what information is required for that person to follow your answer start to end. :)

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.