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.
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.
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 .
                        You can set a environmental variable called PYTHONPATH to include you directory.
Read more about it in the docs
execute following from the shell:
echo -e "\nexport PYTHONPATH=\$PYTHONPATH:/path/to/directory" >> ~/.bashrc
and restart it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With