Permanently add a directory to PYTHONPATH?

前端 未结 19 1293
别那么骄傲
别那么骄傲 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:38

    On MacOS, Instead of giving path to a specific library. Giving full path to the root project folder in

    ~/.bash_profile 
    

    made my day, for example:

    export PYTHONPATH="${PYTHONPATH}:/Users/<myuser>/project_root_folder_path"
    

    after this do:

    source ~/.bash_profile
    
    0 讨论(0)
  • 2020-11-22 01:38

    Adding export PYTHONPATH="${PYTHONPATH}:/my/other/path" to the ~/.bashrc might not work if PYTHONPATH does not currently exist (because of the :).

    export PYTHONPATH="/my/other/path1"
    export PYTHONPATH="${PYTHONPATH}:/my/other/path2"
    

    Adding the above to my ~/.bashrc did the trick for me on Ubuntu 16.04

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

    Shortest path between A <-> B is a straight line;

    import sys
    if not 'NEW_PATH' in sys.path:
      sys.path += ['NEW_PATH']
    
    0 讨论(0)
  • 2020-11-22 01:41

    Fix Python Path issues when you switch from bash to zsh

    I ran into Python Path problems when I switched to zsh from bash.

    The solution was simple, but I failed to notice.

    Pip was showing me, that the scripts blah blah or package blah blah is installed in ~/.local/bin which is not in path.

    After reading some solutions to this question, I opened my .zshrc to find that the solution already existed.

    I had to simply uncomment a line:

    Take a look

    Screenshot from 2020-10-07 13-38-17

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

    Just to add on awesomo's answer, you can also add that line into your ~/.bash_profile or ~/.profile

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

    You could add the path via your pythonrc file, which defaults to ~/.pythonrc on linux. ie.

    import sys
    sys.path.append('/path/to/dir')
    

    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.

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