简体   繁体   中英

Python 3 - ImportError: No module named

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:

from app.stamp import Timestamp 

Timestamp gets imported in main.py but not in test.py where I get this error:

ImportError: No module named 'app'

Question :
How can I in python 3.5 import functionality of stamp.py in test.py ?

make sure your folder tests contains __init__.py

Below code appends the path of your project project to sys.path in test.py

python will go through to search the modules and files in your project

import sys
sys.path.append("/path/to/project")
from app.stamp import Timestamp

确保project/位于project/目录中的PYTHONPATH ,put和__init__.py文件中,然后您就可以from project.app.stamp import Timestamp调用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM