我有以下文件结构:
├───common
│ hdfs.py
│ impala.py
│ pandasUtils.py
│ proxy.py
│ stringUtils.py
│ __init__.py
│
├───tests
│ └───unitTests
│ test_stringUtils.py
│ __init__.py
从 tests/unitTests/test_stringUtils.py
中,我想导入文件 common/stringUtils.py
我尝试了以下方法:
import unittest
from common.stringUtils import StringUtils
但我有以下错误:无法导入“common.stringUtils”。
有没有一种方法可以导入文件而不处理系统路径或执行与“导入自”不同的任何操作?(30 年了,仍然看起来像早期访问游戏)
最佳答案
使用PYTHONPATH
。例如
PYTHONPATH=. python tests/unitTests/test_stringUtils.py
或
PYTHONPATH=.. python unitTests/test_stringUtils.py
关于python - 如何从不同文件夹中的文件导入Python类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66354319/