每当我使用sys.path。追加,新目录将被添加。然而,一旦我关闭python,列表将恢复到以前的(默认?)值。如何将目录永久添加到PYTHONPATH?
当前回答
下面的脚本可以在所有平台上运行,因为它是纯Python。它使用了pathlib路径(请参阅https://docs.python.org/3/library/pathlib.html),使其能够跨平台工作。运行一次,重新启动内核,就完成了。灵感来自https://medium.com/@arnaud.bertrand/ modiing-python-s - sear-path-with-pth -files-2a41a4143574。为了运行它,它需要管理员权限,因为你修改了一些系统文件。
from pathlib import Path
to_add=Path(path_of_directory_to_add)
from sys import path
if str(to_add) not in path:
minLen=999999
for index,directory in enumerate(path):
if 'site-packages' in directory and len(directory)<=minLen:
minLen=len(directory)
stpi=index
pathSitePckgs=Path(path[stpi])
with open(str(pathSitePckgs/'current_machine_paths.pth'),'w') as pth_file:
pth_file.write(str(to_add))
其他回答
您可以通过pythonrc文件添加路径,该文件默认为~/。linux上的Pythonrc。ie。
import sys
sys.path.append('/path/to/dir')
您还可以在全局rc文件中设置PYTHONPATH环境变量,例如~/。mac或linux上的配置文件,或通过控制面板->系统->高级选项卡-> windows上的环境变量。
下面的脚本可以在所有平台上运行,因为它是纯Python。它使用了pathlib路径(请参阅https://docs.python.org/3/library/pathlib.html),使其能够跨平台工作。运行一次,重新启动内核,就完成了。灵感来自https://medium.com/@arnaud.bertrand/ modiing-python-s - sear-path-with-pth -files-2a41a4143574。为了运行它,它需要管理员权限,因为你修改了一些系统文件。
from pathlib import Path
to_add=Path(path_of_directory_to_add)
from sys import path
if str(to_add) not in path:
minLen=999999
for index,directory in enumerate(path):
if 'site-packages' in directory and len(directory)<=minLen:
minLen=len(directory)
stpi=index
pathSitePckgs=Path(path[stpi])
with open(str(pathSitePckgs/'current_machine_paths.pth'),'w') as pth_file:
pth_file.write(str(to_add))
修复从bash切换到zsh时Python路径问题
当我从bash切换到zsh时,我遇到了Python路径问题。
解决办法很简单,但我没有注意到。
Pip告诉我,脚本blah blah或包blah blah安装在~/。不在路径中的本地/bin。
在阅读了这个问题的一些解决方案后,我打开我的.zshrc,发现解决方案已经存在。
我不得不取消注释一行:
看一看
您需要将新目录添加到环境变量PYTHONPATH中,以冒号与之前的内容分隔。在任何形式的Unix中,您都可以在适合于您使用的任何shell的启动脚本中完成(。配置文件或其他,取决于你最喜欢的shell)与一个命令,同样,取决于所讨论的shell;在Windows中,您可以通过系统GUI来实现此目的。
Superuser.com可能是一个更好的地方,可以进一步询问,例如,如果您需要详细说明如何在所选的平台和shell中丰富环境变量,可以询问更多细节,因为这本身并不是一个真正的编程问题。
为了给出更多的解释,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.
推荐文章
- 获取列表列表到pandas DataFrame
- 在Sublime Text 2中使用Ctrl+D进行多个选择时,我如何跳过匹配?
- #定义WIN32_LEAN_AND_MEAN具体排除了什么?
- 找到python的安装位置(如果不是默认目录)
- 哈希字典?
- 如何为matplotlib散点图放置单独的标签?
- 如何动态创建变量?
- 如何在文本文件中搜索字符串?
- 生成具有给定(数值)分布的随机数
- 用Python计算Pearson相关性和显著性
- Python列表目录,子目录和文件
- 提取正则表达式匹配的一部分
- 如何访问Django模板中的字典元素?
- 如何在方法中访问“静态”类变量?
- Python:查找列表中的元素