Whenever I use sys.path.append
, the new directory will be added. However, once I close python, the list will revert to the previous (default?) values. How do I permanently add a directory to PYTHONPATH
?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
To give a bit more explanation, Python will automatically construct its search paths (as mentioned above and here) using the
site.py
script (typically located in sys.prefix +lib/python<version>/site-packages
as well aslib/site-python
). One can obtain the value of sys.prefix:The site.py script then adds a number of directories, dependent upon the platform, such as
/usr/{lib,share}/python<version>/dist-packages
,/usr/local/lib/python<version>/dist-packages
to the search path and also searches these paths for<package>.pth
config files which contain specific additional search paths. For example easy-install maintains its collection of installed packages which are added to a system specific file e.g on Ubuntu it's/usr/local/lib/python2.7/dist-packages/easy-install.pth
. On a typical system there are a bunch of these .pth files around which can explain some unexpected paths in sys.path:So one can create a .pth file and put in any of these directories (including the sitedir as mentioned above). This seems to be the way most packages get added to the sys.path as opposed to using the PYTHONPATH.
Note: On OSX there's a special additional search path added by site.py for 'framework builds' (but seems to work for normal command line use of python):
/Library/Python/<version>/site-packages
(e.g. for Python2.7:/Library/Python/2.7/site-packages/
) which is where 3rd party packages are supposed to be installed (see the README in that dir). So one can add a path configuration file in there containing additional search paths e.g. create a file called/Library/Python/2.7/site-packages/pip-usr-local.pth
which contains/usr/local/lib/python2.7/site-packages/
and then the system python will add that search path.On linux you can create a symbolic link from your package to a directory of the PYTHONPATH without having to deal with the environment variables. Something like:
In Python 3.6.4 you can persist sys.path across python sessions like this:
I strongly suggest you use virtualenv and virtualenvwrapper otherwise you will clutter your path
Instead of manipulating
PYTHONPATH
you can also create a path configuration file. First find out in which directory Python searches for this information:For some reason this doesn't seem to work in Python 2.7. There you can use:
Then create a
.pth
file in that directory containing the path you want to add (create the directory if it doesn't exist).For example:
You could add the path via your pythonrc file, which defaults to ~/.pythonrc on linux. ie.
You could also set the
PYTHONPATH
environment variable, in a global rc file, such~/.profile
on mac or linux, or via Control Panel -> System -> Advanced tab -> Environment Variables on windows.The add a new path to PYTHONPATH is doing in manually by:
adding the path to your ~/.bashrc profile, in terminal by:
paste the following to your profile
then, make sure to source your bashrc profile when ever you run your code in terminal:
Hope this helps.