Permanently add a directory to PYTHONPATH?

前端 未结 19 1292
别那么骄傲
别那么骄傲 2020-11-22 01:12

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

相关标签:
19条回答
  • 2020-11-22 01:26

    You need to add your new directory to the environment variable PYTHONPATH, separated by a colon from previous contents thereof. In any form of Unix, you can do that in a startup script appropriate to whatever shell you're using (.profile or whatever, depending on your favorite shell) with a command which, again, depends on the shell in question; in Windows, you can do it through the system GUI for the purpose.

    superuser.com may be a better place to ask further, i.e. for more details if you need specifics about how to enrich an environment variable in your chosen platform and shell, since it's not really a programming question per se.

    0 讨论(0)
  • 2020-11-22 01:27

    If you're using bash (on a Mac or GNU/Linux distro), add this to your ~/.bashrc

    export PYTHONPATH="${PYTHONPATH}:/my/other/path"
    
    0 讨论(0)
  • 2020-11-22 01:27

    This is an update to this thread which has some old answers.

    For those using MAC-OS Catalina or some newer (>= 10.15), it was introduced a new Terminal named zsh (a substitute to the old bash).

    I had some problems with the answers above due to this change, and I somewhat did a workaround by creating the file ~/.zshrc and pasting the file directory to the $PATH and $PYTHONPATH

    So, first I did:

    nano ~/.zshrc
    

    When the editor opened I pasted the following content:

    export PATH="${PATH}:/Users/caio.hc.oliveira/Library/Python/3.7/bin"
    export PYTHONPATH="${PYTHONPATH}:/Users/caio.hc.oliveira/Library/Python/3.7/bin"
    

    saved it, and restarted the terminal.

    IMPORTANT: The path above is set to my computer's path, you would have to adapt it to your python.

    0 讨论(0)
  • 2020-11-22 01:28

    This works on Windows

    1. On Windows, with Python 2.7 go to the Python setup folder.
    2. Open Lib/site-packages.
    3. Add an example.pth empty file to this folder.
    4. Add the required path to the file, one per each line.

    Then you'll be able to see all modules within those paths from your scripts.

    0 讨论(0)
  • 2020-11-22 01:28

    In Python 3.6.4 you can persist sys.path across python sessions like this:

    import sys
    import os
    
    print(str(sys.path))
    
    dir_path = os.path.dirname(os.path.realpath(__file__))
    print(f"current working dir: {dir_path}")
    
    root_dir = dir_path.replace("/util", '', 1)
    print(f"root dir: {root_dir}")
    
    sys.path.insert(0, root_dir)
    
    print(str(sys.path))
    

    I strongly suggest you use virtualenv and virtualenvwrapper otherwise you will clutter your path

    0 讨论(0)
  • 2020-11-22 01:29

    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 as lib/site-python). One can obtain the value of sys.prefix:

    python -c 'import sys; print(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:

    python -c 'import sys; print(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.

    0 讨论(0)
提交回复
热议问题