python - 为什么我的脚本目录不在 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']
解决方案
我相信这是一个 Python 错误,特定于可嵌入(没有安装程序的 ZIP 文件)Windows 发行版。我已经提交了https://bugs.python.org/issue34841。
从发行版中删除python37._pth
文件(大概python36._pth
在你的情况下)为我修复了它。
推荐阅读
- java - 如何删除字符串中的重复项,例如:“我的名字是这个和那个” - 输出将是“我的名字是这个和那个”
- javascript - CSV 文件的内容在 Javascript 中为空
- memcached - 外部分布式内存缓存不起作用
- javascript - 向下滚动时如何调整图像或徽标的大小
- laravel - Laravel 注册成功后跳转到上一页
- sql-server - 备份数据库的存储过程删除并创建备份(如果已存在)
- python - 映射列值的子字符串
- javascript - 点击搜索 Reactjs 打开新组件
- ruby-on-rails - Rails 自定义项目结构自动加载配置
- python - 如果我展开窗口,为什么布局都搞砸了,python tkinter