Run ipython by running the script

My use case, I want to initialize some functions in a file, and then run ipython with certain functions. Is there a way to do something like ipython --run_script = myscript.py?

+48
python ipython
Jul 24 '10 at 0:14
source share
7 answers

Per docs , this is trivial:

You start IPython with the command:

$ ipython [options] files 

If called without parameters, it executes all the files listed in the sequence and puts you in the translator, still recognizing any options that you might have installed in your ipythonrc . This behavior is different from standard Python, which when called as python -i will execute a single file and ignore the configurations.

So just use ipython myfile.py ... and here you are! -)

+26
Jul 24 '10 at 5:08
source share

In recent versions of ipython, you need to add the -i option to subsequently enter the interactive environment. Without -i it simply runs the code in the myfile.py file and returns to the prompt.

 $ ipython -i myfile.py 
+69
Mar 29 2018-12-12T00:
source share

Currently, you can use the ipython start folder, which is located in your home directory (C: \ users \ [username] \. Ipython on Windows). Go to the default profile and you will see the startup folder with the README file. Just place any Python scripts there, or if you want to use ipython commands, put them in a file with the extension .ipy.

+12
Aug 28 2018-12-12T00:
source share

You can use ipython profiles to define startup scripts that will run each time ipython starts. A full description of the profiles is given here . You can create multiple profiles with different startup files.

Assuming you only need one profile, and you always need to have the same startup files every time you start ipython, you can simply change the default profile. To do this, first find out where your ipython configuration directory is located in the 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 

In this example, I use Ubuntu Linux, and the config directory is in /home/johndoe/.config/ipython , where johndoe is the username.

The default_profile file is located in the profile_default subdirectory. Put any startup scripts in profile_default/startup . In this example, the full path would be /home/johndoe/.config/ipython/profile_default/startup .

+8
Apr 16 '13 at 18:15
source share

Update for @Caleb answer for Python 3.5 on Ubuntu 14.04: I made this answer myself by copying the corresponding parts of @Caleb answer.

You can use ipython profiles to define startup scripts that will run each time ipython starts. A full description of the profiles is given here . You can create multiple profiles with different startup files.

Assuming you only need one profile, and you always need to have the same startup files every time you start ipython, you can simply change the default profile. To do this, first find out where your ipython configuration directory is located in the ipython session .:

Input:

 import IPython ipython_config_dir = IPython.paths.get_ipython_dir() print(ipython_cofig_dir) 

Output:

 /home/johndoe/.ipython 

In this example, johndoe is the username.

Inside the /.ipython folder, the /.ipython file is located in the profile_default subdirectory. Put any startup scripts in profile_default / startup. In the example here, the full path will be

 /home/johndoe/.ipython/profile_default/startup 
0
May 23 '16 at 8:23 a.m.
source share

The following is an example when you want your startup scripts to run automatically whenever you use ipython (instead of having a script that you must specify each time ipython starts).

For the latest versions (i.e. 5.1.0) of ipython, put one or more python scripts that you want to execute in the IPYTHON_CONFIG_DIR/profile_PROFILENAME/startup folder.

On Linux, for example, you can put your python startup code in a file called ~/.ipython/profile_default/startup/10-mystartupstuff.py if you want it to start when the ipython profile is not specified.

Information on creating and using ipython profiles is available here .

0
Apr 26 '17 at 17:42 on
source share

You seem to be looking for ipyhton% run magic command.

By entering ipython:

  %run hello_world.py 

you run the hello.py program stored in your home directory. Functions and variables defined in this script will also be available to you.

-one
Apr 6 '13 at 0:50
source share



All Articles