每当我使用sys.path。追加,新目录将被添加。然而,一旦我关闭python,列表将恢复到以前的(默认?)值。如何将目录永久添加到PYTHONPATH?
当前回答
您需要将新目录添加到环境变量PYTHONPATH中,以冒号与之前的内容分隔。在任何形式的Unix中,您都可以在适合于您使用的任何shell的启动脚本中完成(。配置文件或其他,取决于你最喜欢的shell)与一个命令,同样,取决于所讨论的shell;在Windows中,您可以通过系统GUI来实现此目的。
Superuser.com可能是一个更好的地方,可以进一步询问,例如,如果您需要详细说明如何在所选的平台和shell中丰富环境变量,可以询问更多细节,因为这本身并不是一个真正的编程问题。
其他回答
如果有人仍然感到困惑,如果你在Mac上,请执行以下操作:
打开终端 输入open .bash_profile 在弹出的文本文件中,在最后添加这行: 出口到PYTHONPATH = $ PYTHONPATH: foo / bar 保存文件,重新启动终端,就完成了
为了给出更多的解释,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.
我在Windows Vista中永久添加了Python 3.5
系统>控制面板>系统高级设置>高级(点击)环境变量>系统变量>(如果在变量列中没有看到PYTHONPATH)(点击)新建>变量名称:PYTHONPATH >变量值:
请将目录写在变量值中。这是Blue Peppers回答的细节。
在MacOS上,而不是给出特定库的路径。给出根项目文件夹的完整路径
~/.bash_profile
让我很开心,例如:
export PYTHONPATH="${PYTHONPATH}:/Users/<myuser>/project_root_folder_path"
这样做之后:
source ~/.bash_profile
这是对这个线程的更新,它有一些旧的答案。
对于那些使用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。
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 我如何找到哪个程序正在使用端口80在Windows?
- 使用try和。Python中的if