python - 使用符号链接时出现 ModuleNotFoundError
问题描述
我有以下目录结构
parent_directory/
+-- utils.py
+-- child_directory/
| +-- script.py
+-- results_directory/
| +-- results1
| +-- results2
有几个脚本child_directory/
不会相互导入任何内容。然而,他们utils.py
都parent_directory/
从results_directory
. 而不是将脚本一一复制到父目录。我只想child_directory/script.py
在parent_directory
. 从内部parent_directory
我使用ln -s child_directory/script.py .
or建立链接ln -s absolute/path/to/child_directory/script.py .
。然后python script.py
用来运行脚本。
下面是一个示例代码script.py
:
from utils.py import results_reader
# Do work
当我使用符号链接运行脚本时,parent_directory
出现以下错误:
ModuleNotFoundError: No module named 'utils'
我不太明白这里发生了什么。os.getcwd()
显示当前路径为parent_directory/
. 它不应该能够读取utils.py
文件吗?当我script.py
通过将其复制到parent_directory/
它运行时运行没有错误
当我更改from utils.py import results_reader
为时from .utils.py import results_reader
,我收到以下错误:
ModuleNotFoundError: No module named '__main__.utils'; '__main__' is not a package
解决方案
推荐阅读
- node.js - 如何检查 Angular 8 中的会话
- javascript - 向填充有数据表的现有表添加额外的列
- soap - 为什么要同时使用 XSD 和 WSDL 来定义域模型和 Web 服务契约
- javascript - 长字符串中的正则表达式搜索和替换
- c++ - 如何在 c++ win 32 中使用线程
- .net - 尝试在更新中返回 NotFound() 时出现 DBConcurrencyException
- javascript - 用js在屏幕上显示记录总数
- arrays - 数组未在 for 循环中正确存储值 (C)
- angular - Angular Form 验证 From date 应该是 Gretaer than To Date
- node.js - Node Pug:使用变量时表单标签的动作属性无法正常工作