Reputation: 32344
I know that I can add an import path to Python like this:
import sys
sys.path.append("/path/to/directory/")
But, when I restart Python, this is gone. I'd find it quite annoying if I had to do this all the time, I'd like to do this once and for all and be done with it.
So, how? Where can I find that file? Or do I need to edit something else? I'm using the latest version of Ubuntu.
Upvotes: 10
Views: 24226
Reputation: 155
This has been mentioned elsewhere, but if you use Anaconda you can do:
conda develop /Path/To/Your/Modules
from the Shell and it will write your path into a conda.pth file into the standard directory for 3rd party modules (site-packages in my case).
(I found this post in my attempt to figure out how to create a .pth file from one line of code because I knew I did it in the past, so hopefully this helps some people though it is Anaconda dependent).
Upvotes: 0
Reputation: 1303
execute following from the shell:
echo -e "\nexport PYTHONPATH=\$PYTHONPATH:/path/to/directory" >> ~/.bashrc
and restart it
Upvotes: 3
Reputation: 81
You can also use a path file.
If you want to add a module called mymodule to your import path, add the file mymodule.pth to the standard directory for 3rd party modules, usually called dist-packages, or site-packages. On Ubuntu you will probably find it somewhere like
/usr/local/lib/python2.7/dist-packages
The file mymodule.pth should contain a single line, the directory you want to add to the python import path
<mymodule.pth>
/path/to/directory/containing/mymodule
Any python modules or packages in the directory will now be importable from the interpreter.
Upvotes: 5
Reputation: 45634
You can set a environmental variable called PYTHONPATH
to include you directory.
Read more about it in the docs
Upvotes: 3
Reputation: 3037
From man python
~/.pythonrc.py
User-specific initialization file loaded by the user module; not used by default or by most applications.
ENVIRONMENT VARIABLES
PYTHONPATH
Augments the default search path for module files. The format is the same as the shell's $PATH: one or more directory pathnames
separated by colons. Non-existent directories are silently ignored. The default search path is installation dependent, but gen-
erally begins with ${prefix}/lib/python<version> (see PYTHONHOME above). The default search path is always appended to $PYTHON-
PATH. If a script argument is given, the directory containing the script is inserted in the path in front of $PYTHONPATH. The
search path can be manipulated from within a Python program as the variable sys.path .
Upvotes: 11