python 3.6
我已经编写了一些组件,并且正在尝试将其中一个组件导入到另一个组件中。
下面是我的项目结构:
.
└── components
├── __init__.py
├── extract
│ └── python3
| ├── __init__.py
│ └── extract.py
└── transform
└── python3
├── __init__.py
└── preprocess.py
提取.py
from components.transform.python3.preprocess import my_function
if __name__ == '__main__':
my_function()
预处理.py
def my_function():
print("Found me")
当我运行 python components/extract/python3/extract.py
我看到以下错误:
ModuleNotFoundError: No module named 'components'
我在包含模块的目录以及顶级包目录中添加了一个空的 __init__.py 文件。
请您参考如下方法:
好的,导入要求顶级包在 Python PATH (sys.path) 中可用。
因此,要使其正常工作,您应该:
- cd 到包含
组件的目录 将
.添加到 Python PATH:export PYTHONPATH='.'启动您的脚本:
python components/extract/python3/extract.py
在我的系统上,它成功显示:
Found me
