python - 即使父文件夹在 sys.path 中,Python 也找不到本地模块
问题描述
我有一个这样的文件夹结构:
/content
/repo
/examples
__init__.py
/utils
__init__.py
helper.py
在 Jupyter 笔记本单元格中,我运行以下命令:
!pwd
我明白了:
/content
下一个:
import sys
sys.path.append("/content/repo")
sys.path
结果:
['',
'/env/python',
'/usr/lib/python36.zip',
'/usr/lib/python3.6',
'/usr/lib/python3.6/lib-dynload',
'/usr/local/lib/python3.6/dist-packages',
'/usr/lib/python3/dist-packages',
'/usr/local/lib/python3.6/dist-packages/IPython/extensions',
'/root/.ipython',
'/content/repo']
最后,我运行这个:
from examples.utils.helper import read_data
我明白了:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-19-b350ec0401b3> in <module>()
----> 1 from examples.utils.helper import read_data
ModuleNotFoundError: No module named 'examples.utils'
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------
如果有帮助,这是一个 Colab 笔记本。
[更新]
在@John Gordon 的帮助下,现在我知道问题在于机器上已经有一个examples
包了。但是考虑到我也在 . 开头添加了我的路径sys.path
,为什么问题仍然存在?
这是我sys.path
现在的样子:
['/content/repo/',
'/content/repo',
'',
'/env/python',
'/usr/lib/python36.zip',
'/usr/lib/python3.6',
'/usr/lib/python3.6/lib-dynload',
'/usr/local/lib/python3.6/dist-packages',
'/usr/lib/python3/dist-packages',
'/usr/local/lib/python3.6/dist-packages/IPython/extensions',
'/root/.ipython',
'/content/repo/',
'/content/repo']
解决方案
推荐阅读
- amazon-web-services - 在存储桶中显示 AWS S3 cli 上传后出现问题
- apache-spark - 使用 Spark / Scala 从数据框中的单个记录创建多个记录?
- google-app-engine - 用户/服务帐户、范围、AppEngine 和 GSuite(工作表 v4 api)
- html - 如何将背景图像设置为仅屏幕的左半部分
- python - 从 Django 视图中的特定论坛类别获取线程
- c - 无法使用 Visual Studio 的 Empty WDM 驱动程序模板中的 NDIS 相关功能
- jenkins - 如何处理詹金斯 http -> https 重定向?
- html - Chrome 手机上的输入按钮显示很奇怪
- metrics - 使用 Google Cloud Monitoring 跟踪常用指标
- security - Web 应用程序在 URL 中显示用户登录凭据,开发人员推荐使用 SSL 证书。这安全吗?