DavidR
DavidR

Reputation: 63

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?

Upvotes: 6

Views: 12679

Answers (2)

Haifeng Zhang
Haifeng Zhang

Reputation: 31925

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

Upvotes: 5

gr1zzly be4r
gr1zzly be4r

Reputation: 2162

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.

Upvotes: 0

Related Questions