How do I add a Python import path permanently?
Asked Answered
G

5

10

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.

Genealogy answered 10/5, 2012 at 9:42 Comment(1)
docs.python.org/install/…Schoof
N
11

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 .
Nanon answered 10/5, 2012 at 9:45 Comment(0)
Z
5

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.

Zendejas answered 10/5, 2012 at 10:19 Comment(0)
K
3

execute following from the shell:

echo -e "\nexport PYTHONPATH=\$PYTHONPATH:/path/to/directory" >> ~/.bashrc

and restart it

Kenon answered 10/5, 2012 at 9:46 Comment(2)
I did that, but nothing has changed. Do I need to restart, maybe?Genealogy
@Bane, 1) I edited the line, there was a mistake. 2) normally you have to relogin into your shell (just close it and open again) 3) you might also want to edit ~/.bashrc and remove the line added by the wrong command you entered before 4) Finally, don't forget to substitute path/to/directory with the actual path.Kenon
M
3

You can set a environmental variable called PYTHONPATH to include you directory.

Read more about it in the docs

Mullah answered 10/5, 2012 at 9:46 Comment(0)
L
0

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

Legman answered 5/8, 2022 at 21:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.