每当我使用sys.path。追加,新目录将被添加。然而,一旦我关闭python,列表将恢复到以前的(默认?)值。如何将目录永久添加到PYTHONPATH?
当前回答
为了给出更多的解释,Python将使用site.py脚本(通常位于sys. py中)自动构造它的搜索路径(如上所述和这里)。Prefix + lib/python<version>/site-packages以及lib/site-python)。可以获取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)'
因此,可以创建一个.pth文件,并将其放在这些目录中的任何一个目录中(包括上面提到的sitedir)。这似乎是大多数包被添加到系统的方式。而不是使用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.
其他回答
在linux上,您可以创建从包到PYTHONPATH目录的符号链接,而不必处理环境变量。喜欢的东西:
ln -s /your/path /usr/lib/pymodules/python2.7/
在Python 3.6.4中,可以持久化sys。通过python会话的路径如下所示:
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))
我强烈建议你使用virtualenv和virtualenvwrapper,否则你会弄乱你的路径
在Mac上:
user@terminal$ env PYTHONPATH=module_path python3
>>> import sys
>>> sys.path
['module_path', 'plus_other_python3_paths',...]
如果您正在使用bash(在Mac或GNU/Linux发行版上),请将此添加到~/.bashrc中
export PYTHONPATH="${PYTHONPATH}:/my/other/path"
这是对这个线程的更新,它有一些旧的答案。
对于那些使用MAC-OS Catalina或更新版本(>= 10.15)的用户,它引入了一个名为zsh的新终端(旧bash的替代品)。
由于这个更改,我在上面的回答中遇到了一些问题,我通过创建文件~/来解决一些问题。并将文件目录粘贴到$PATH和$PYTHONPATH
所以,首先我做了:
nano ~/.zshrc
当编辑器打开时,我粘贴以下内容:
export PATH="${PATH}:/Users/caio.hc.oliveira/Library/Python/3.7/bin"
export PYTHONPATH="${PYTHONPATH}:/Users/caio.hc.oliveira/Library/Python/3.7/bin"
保存,并重新启动终端。
重要提示:上面的路径设置为我的计算机的路径,你必须适应你的python。
推荐文章
- set()是如何实现的?
- 如何使Python脚本在Linux中像服务或守护进程一样运行
- 返回大列表中每n项的python方式
- 如何使用Python中的DLL文件?
- 我如何量化两幅图像之间的差异?
- 获取列表列表到pandas DataFrame
- 在Sublime Text 2中使用Ctrl+D进行多个选择时,我如何跳过匹配?
- #定义WIN32_LEAN_AND_MEAN具体排除了什么?
- 找到python的安装位置(如果不是默认目录)
- 哈希字典?
- 如何为matplotlib散点图放置单独的标签?
- 如何动态创建变量?
- 如何在文本文件中搜索字符串?
- 生成具有给定(数值)分布的随机数
- 用Python计算Pearson相关性和显著性