python - 如何永久编辑(删除)sys.path?

标签 python python-3.x

import sys
print(sys.path)

'C:\\python32\\Lib\\site-packages\\django'
'C:\\Python32'
'C:\\Python32\\lib\\site-packages'
...

由于某种原因,我的 pythonpath 变得困惑。我想组织一下。我不需要上面的第一个和最后一个,对吗?我怎样才能永久改变它? (不像 sys.path.remove 或 sys.path.append)

我使用的是Python3.2。在Windows8中。

最佳答案

路径“C:\Python32\lib\site-packages”通过内置 site 添加到 sys.path模块。

如果您愿意,可以使用 -S 标志启动 python 来告诉站点模块“不要添加站点包”。

python -S

接下来,'C:\python32\Lib\site-packages\django'。
这是一个疯狂的猜测:您使用 pip/easy_install/msi-installer 安装了 django 并且有一个文件 C:\python32\Lib\site-packages\django.pth (或类似以 .pth 结尾的内容)

引用文档:

A path configuration file is a file whose name has the form name.pth and exists in one of the four directories mentioned above; its contents are additional items (one per line) to be added to sys.path.

您可以删除 django.pth 文件(不推荐,见下文)以从 sys.path 中删除“..../django”

所以,简短的回答:不要弄乱 sys.path,sys.path 中的内容可能是有充分理由的。 如果您不需要 django,请使用您用来安装 django 的任何工具卸载 django。对于您不需要的每个包都相同。

关于python - 如何永久编辑(删除)sys.path?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27910483/

相关文章:

regex - Python 正则表达式匹配多行

Python处理字符串匹配

python - 贝塞尔曲线和 matplotlib

python - 正则表达式匹配到以 `- [` 开头的最后一行

python - Pandas Dataframe - 根据条件获取索引值

python - 在 Python 中过滤 (Nx1) 列表

python - pip3 install ruamel.yaml[jinja2] 破坏 ruamel.yaml

python - 在Linux上部署Django项目

python - 在 linux 上用 python 将文件写入 usb

python - Django PIL : IOError Cannot identify image file