Situation:
Given this project structure:
情况:鉴于此项目结构:
project/
app/
__init__.py (empty)
stamp.py
tests/
test.py
main.py
In main.py
and test.py
I am trying to import the functionality of stamp.py
via:
在main.py和test.py中,我试图通过以下方式导入stamp.py的功能:
from app.stamp import Timestamp
Timestamp
gets imported in main.py
but not in test.py
where I get this error:
时间戳在main.py中导入但不在test.py中导入,我收到此错误:
ImportError: No module named 'app'
Question:
How can I in python 3.5 import functionality of stamp.py
in test.py
?
问题:如何在test.py中的python 3.5导入stamp.py的功能?
2 个解决方案
#1
2
make sure your folder tests contains __init__.py
确保您的文件夹测试包含__init__.py
Below code appends the path of your project project
to sys.path
in test.py
下面的代码将项目项目的路径附加到test.py中的sys.path
python will go through to search the modules and files in your project
python将遍历搜索项目中的模块和文件
import sys
sys.path.append("/path/to/project")
from app.stamp import Timestamp
#2
0
Make sure project/
is in your PYTHONPATH, put and __init__.py
file in the project/
directory and then you will be able to call from project.app.stamp import Timestamp
.
确保项目/位于项目/目录中的PYTHONPATH,put和__init__.py文件中,然后您就可以从project.app.stamp导入时间戳调用。
#1
2
make sure your folder tests contains __init__.py
确保您的文件夹测试包含__init__.py
Below code appends the path of your project project
to sys.path
in test.py
下面的代码将项目项目的路径附加到test.py中的sys.path
python will go through to search the modules and files in your project
python将遍历搜索项目中的模块和文件
import sys
sys.path.append("/path/to/project")
from app.stamp import Timestamp
#2
0
Make sure project/
is in your PYTHONPATH, put and __init__.py
file in the project/
directory and then you will be able to call from project.app.stamp import Timestamp
.
确保项目/位于项目/目录中的PYTHONPATH,put和__init__.py文件中,然后您就可以从project.app.stamp导入时间戳调用。