I find myself typing import numpy as np almost every single time I fire up the python interpreter. How do I set up the python or ipython interpreter so that numpy is automatically imported?
For ipython, there are two ways to achieve this. Both involve ipython's configuration directory which is located in ~/.ipython.
Create a custom ipython profile.
Or you can add a startup file to ~/.ipython/profile_default/startup/
For simplicity, I'd use option 2. All you have to do is place a .py or .ipy file in the ~/.ipython/profile_default/startup directory and it will automatically be executed. So you could simple place import numpy as np in a simple file and you'll have np in the namespace of your ipython prompt.
Option 2 will actually work with a custom profile, but using a custom profile will allow you to change the startup requirements and other configuration based on a particular case. However, if you'd always like np to be available to you then by all means put it in the startup directory.
For more information on ipython configuration. The docs have a much more complete explanation.
Use the environment variable PYTHONSTARTUP. From the official documentation:
If this is the name of a readable file, the Python commands in that
file are executed before the first prompt is displayed in interactive
mode. The file is executed in the same namespace where interactive
commands are executed so that objects defined or imported in it can be
used without qualification in the interactive session.
So, just create a python script with the import statement and point the environment variable to it. Having said that, remember that 'Explicit is always better than implicit', so don't rely on this behavior for production scripts.
For Ipython, see this tutorial on how to make a ipython_config file
I use a ~/.startup.py file like this:
# Ned's .startup.py file
print("(.startup.py)")
import datetime, os, pprint, re, sys, time
print("(imported datetime, os, pprint, re, sys, time)")
pp = pprint.pprint
Then define PYTHONSTARTUP=~/.startup.py, and Python will use it when starting a shell.
The print statements are there so when I start the shell, I get a reminder that it's in effect, and what has been imported already. The pp shortcut is really handy too...
As a simpler alternative to the accepted answer, on linux:
just define an alias, e.g. put alias pynp='python -i -c"import numpy as np"' in your ~/.bash_aliases file. You can then invoke python+numpy with pynp, and you can still use just python with python. Python scripts' behaviour is left untouched.
While creating a custom startup script like ravenac95 suggests is the best general answer for most cases, it won't work in circumstances where you want to use a from __future__ import X. If you sometimes work in Python 2.x but want to use modern division, there is only one way to do this. Once you create a profile, edit the profile_default (For Ubuntu this is located in ~/.ipython/profile_default) and add something like the following to the bottom:
c.InteractiveShellApp.exec_lines = [
'from __future__ import division, print_function',
'import numpy as np',
'import matplotlib.pyplot as plt',
]
You can create a normal python script as import_numpy.py or anything you like
#!/bin/env python3
import numpy as np
then launch it with -i flag.
python -i import_numpy.py
Way like this will give you flexibility to choose only modules you want for different projects.
As ravenac95 mentioned in his answer, you can either create a custom profile or modify the default profile. This answer is quick view of Linux commands needed to import numpy as np automatically.
If you want to use a custom profile called numpy, run:
ipython profile create numpy
echo 'import numpy as np' >> $(ipython locate profile numpy)/startup/00_imports.py
ipython --profile=numpy
Or if you want to modify the default profile to always import numpy:
echo 'import numpy as np' >> $(ipython locate profile default)/startup/00_imports.py
ipython
Check out the IPython config tutorial to read more in depth about configuring profiles. See .ipython/profile_default/startup/README to understand how the startup directory works.
My default ipython invocation is
ipython --pylab --nosep --InteractiveShellApp.pylab_import_all=False
--pylab has been a ipython option for some time. It imports numpy and (parts of) matplotlib. I've added the --Inter... option so it does not use the * import, since I prefer to use the explicit np.....
This can be a shortcut, alias or script.
I created a little script to get ipython initialized with the code you want.
Create a start.ipy file at your project root folder.
Edit the created file with all the things you need to get into ipython.
ipython profile create <your_profile_name>. Tip, do not add the word "profile" to the name because ipython already includes it.
cp start.ipy ~/.ipython/profile_<your_profile_name>/startup/start.ipy
Run ipython --profile=<your_profile_name> everytime you need everything loaded in ipython.
With this solution, you don't need to set any env variable up. You will need to copy the start.ipy file to the ipython folder every time you modify it, though.
Related
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
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
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!-)
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.
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 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.
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.
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_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
I use PHP in Windows 11. I cannot execute Python script in PHP exec.
The current situation is as follows, Commands that do not call Python scripts can be executed:
exec("cd E:/Python/WordFrequency && ipconfig", $output, $result_code);
exec("Python -V", $output, $result_code);
The above two lines of code return code 0.
However, the following code returns code 1:
exec("Python E:/Python/Mnist/main.py", $output, $result_code);
Run directly in Windows PowerShell:
Python E:/Python/Mnist/main.py
There is no problem.
However, calling in PHP returns code 1.
What's the matter, please?
After hard work, I have solved this problem a few days ago. Now, I put the solution here.
The reason why PHP cannot call Python files is that when executing the following command in exec():
Python E:/Python/Mnist/main.py
The following error was returned:
RuntimeError: Could not determine home directory.
After further testing, it was found that:
import os
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
It is found that PHP can successfully call Python if main.py contains only the above five lines of code. This means that my entire PHP and Python program logic and operating system permissions are all right.
And once I add another line of code:
from matplotlib import pyplot as plt
PHP will not be able to call Python successfully, and will prompt the error message "Could not determine home directory". This means the error introduced by the code from matplotlib import pyplot as plt.
At the beginning, I thought there was a problem with the path or permissions of matplotlib installation. After inspection, there are no problems.
After inquiring more information, I gradually understood the reason. For the module matplotlib, it will ask to determine the user's home directory. Because usually only the users of the operating system call the matplotlib module, which is meaningful and won't cause trouble. However, unspecified visitors who call Python scripts through browsers will not own or determine the user's home directory. Therefore, in this case, it is very reasonable for the matplotlib module to throw an error that cannot determine the user's home directory.
How to solve the problem of Could not determine home directory?
The method is to add the following code before the line from matplotlib import pyplot as plt (or directly at the beginning of the Python file):
import os
os.environ['HOMEPATH'] = 'E:/Python/Mnist'
That is, the home directory of the unspecified user who accesses the Python script through the browser is manually specified. So as to provide matplotlib with a place where necessary files can be saved.
I see how to change certain settings for matplotlib in such a way that the are used to configure it each time I launch, including when I launch interactively with
ipython --pylab
but I'm not sure how run arbitrary code each time I launch in this way, or how to ensure that certain packages have been imported. For example I'd like to do the following whenever I launch as above:
from mpltools import style
style.use('ggplot')
How do I load and run specific packages when I launch 'matplotlib'?
From the ipython website it seems you can place any .py file in the startup folder of your profile and it will be run when ipython is initiated. For help finding the profile where your startup folder is [see here].
To get the effect your looking for you need to check that matplotlib has been imported, the only way I can think of doing that is to add the following
import sys
if "matplotlib" in sys.modules:
# Do something
print
print "This seems to be working"
I have left it with the test so you can quickly determine if it is working.
Two things to note: I am using ipython 2.0.0-dev (in case this has any bearing), and secondly I suspect that calling sys.modules is not recommended, or at least that was the impression I had off the SO post I borrowed the idea from, perhaps someone better than I can enlighten us.
I would like to have
from __future__ import (absolute_import, division, print_function, unicode_literals)
loaded in each interactive session, using a PYTHONSTARTUP file. This works with basic python (2.7.5, installed with Anaconda), but does not work with ipython (1.0.0, Anaconda). Other import statements work with ipython, but __future__ ones are just ignored (although they work if I enter them on the command line). Is this an ipython bug, or is there something I'm missing?
There are two issues here.
The first one is more general than __future__ statements: At least in my tests, ipython 1.0.0 and earlier just don't process the PYTHONSTARTUP environment variable at all. You can see this pretty easily:
$ echo -e 'print "PYTHONSTARTUP!"\n' > pythonstartup.py
$ PYTHONSTARTUP=./pythonstartup.py ipython
Nothing extra gets printed out.
#2706 suggested that it should do so, #3569 patched it, and 1.1.0 seems to be the first version with the change.
So, the fix is to upgrade to 1.1.0. Or, if you're stuck with an older version, do what was suggested in #2706, and add this to your first $IPYTHONDIR/profile_default/startup/*py file:
import os
if os.environ['PYTHONSTARTUP']:
execfile(os.environ['PYTHONSTARTUP'])
However, that still won't fix the problem.
The way $PYTHONSTARTUP gets run (either explicitly by you or implicitly by iPython) is equivalent to an exec. It does explicitly give the appropriate globals to the exec, which ensures that you end up with the print_function tuple available… but that won't affect the parser. (Compare typing exec('from __future__ import print_function)` at the interactive shell.)
And the same is true for the startup files described above, the backward-compat ipython.rc file (if you have that enabled), and any other files that are supposed to be executed in your interactive environment—they're actually just exec'd in your globals, which isn't quite the same thing.
Even files executed as part of the exec_files mechanism in your ipython_config.py or other app-config script are handled this way.
However, lines executed as part of the exec_lines mechanism are not. So, that's the solution.
Edit or create ~/.ipython/profile_default/ipython_config.py. (If you're using a different profile, ipythondir, app name, etc., you presumably know that, and know how to adjust.)
If it's not already present, add this line:
c = get_config()
Then add this:
c.InteractiveShellApp.exec_lines = ['from __future__ import print_function']
See Configuring the ipython command line application for more details.
If you really wanted to, you could probably do something like this:
import os
try:
# Make sure to pop it so it won't get exec'd later in the startup
pythonstartup = os.environ.pop('PYTHONSTARTUP')
with open(pythonstartup) as f:
c.InteractiveShellApp.exec_lines.append(list(f))
except KeyError:
pass
But that seems pretty hacky.
I do the following in my IPython notebook:
import sys
sys.path.append('my_directory')
from db import *
It works fine. But then I added a new function to the db.py and IPython does not see it. It OK. But it does not see it even if I reset everything end re-execute the cell that imports everything. It does not see it even if I user reload. It does not see it even if I close the IPython notebook and restart it.
What is the way to force IPython (or python) to see the updated content of the file?
You need to use autoreload. Check the manual at http://ipython.org/ipython-doc/dev/config/extensions/autoreload.html. Seems you need:
%autoreload 2
The above will automatically reload all imported modules. Except those inlcuded in a separate special list of modules specified by %aimport modulename. Those will only be autoreloaded if you specify %autoreload 1.