1

I'm new in python programming and i've got this error:

Traceback (most recent call last): File "ultrasonicExample.py", line 7, in ? from concretesensor.hcsr04 import HCSR04 ImportError: No module named concretesensor.hcsr04 

here's my app structure:

sensorPy/ src/ abstractclass/ __init__.py ultrasonicSensor.py ... concretesensor/ __init__.py hcsr04.py ... examples/ __init__.py ultrasonicExample.py 

my __init__.py under abstractclass:

from abstractclass.ultrasonicSensor import UltrasonicSensor 

my __init__.py under concretesensor:

from concretesensor.hcsr04 import HCSR04 

I also cheched my sys.path:

>>> import sys >>> from pprint import pprint as p >>> p(sys.path) ['', ... '/home/pi/Documents/sensorPy/src', ... ] >>> 

im ultrasonicExample.py I do:

from concretesensor.hcsr04 import HCSR04 

in hcsr04.py I do:

from abstractclass.ultrasonicSensor import UltrasonicSensor 

Does anyone know what am I doing wrong?

2
  • 1
    Your __init__.pys should just contain e.g. from ultrasonicSensor import UltrasonicSensor. Commented Mar 3, 2015 at 13:47
  • Do export PYTHONPATH='/home/pi/Documents/sensorPy/src' and then run your code again. Commented Mar 3, 2015 at 14:06

1 Answer 1

1

Your concretesensor must in PYTHONPATH variable. Alternate (and better) way is add your path to sys.path:

import sys sys.path.append("/my/path") 

Try insert relative path to sys.path (read about)

sys.path.append("../..") #root of application 

and write __init__.py in root application directory.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Michael, I'll try this when I come home. Just some more doubts: should I put code sys.path.append("../..") inside my init.py (under concretesensor) ? I thought It was not necessary, since when I do : pprint(sys.path) it returns me '/home/pi/Documents/sensorPy/src'. and when you say "write init.py in root application directory." it means under src folder or under sensorPy folder?
Situate your code to PYTHONPATH e.g. site-package and use it everywhere without sys.path.append.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.