首页 > 解决方案 > 为什么我的脚本目录不在 Python sys.path 中?

问题描述

Python 3.6.5
我知道这一点:为什么我的 python 没有将当前工作目录添加到路径中? 但问题在于他正在做一些更复杂的事情(指的是子文件夹但从主文件夹执行)。答案是要么简化事情,要么添加包定义。

选择的答案甚至说:“这是添加的脚本目录”

但是,我的问题实际上更简单:我的脚本目录没有添加。

基本上,互联网上的所有教程都说: import mymodule
当我这样做时,我得到一个名称错误......

我的文件夹结构:

C:/Projects/interner
    interner.py   # this is the main program body
    aux.py        # this is the auxiliary file I would like to import into the above

我已经尝试过在 interner.py 中编码“import aux”,也尝试过使用交互式控制台:

cd c:/Projects/interner
python
import aux

无济于事(ModuleNotFoundError:没有名为“aux”的模块)

我的系统路径:

['C:\\Tools\\Python\\python365\\python36.zip', 'C:\\Tools\\Python\\python365']

(来自脚本内部和交互式控制台)

你能告诉我为什么我不能导入本地脚本吗?是因为我的 sys.path 缺少 PWD 吗?如果是这样,为什么它会丢失它?

编辑:这样做有助于调查:

>>> import os; print(os.listdir("."))
['aux.py', 'hw.py', 'interner.py', 'my_funcs.py']

标签: pythonpython-3.ximportpython-import

解决方案


我相信这是一个 Python 错误,特定于可嵌入(没有安装程序的 ZIP 文件)Windows 发行版。我已经提交了https://bugs.python.org/issue34841

从发行版中删除python37._pth文件(大概python36._pth在你的情况下)为我修复了它。


推荐阅读