Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 261
DavidR Avatar asked Apr 05 '16 18:04

DavidR


1 Answers

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
like image 132
Haifeng Zhang Avatar answered Oct 23 '22 05:10

Haifeng Zhang