0

I am sorry I have to ask this question. It feels stupid, because I am sure the answer is somewhere and I can just not find it. All posts I read give me the feeling I did things right but obviously its not ...

I get an ImportError: No module named test_framework and I do really not understand why.

My folder structure is:

src/
 |
 -- test/
 |    |
 |    -- test_framework/
 |    |      |
 |    |      -- __init__.py
 |    |      -- device.py
 |    |
 |    -- __init__.py
 |
 -- myPackage/
     |
     -- __init__.py
     -- dbg_session.py
     -- main.py

In dbg_session.py I have the following line which produces the error:

from test.test_framework import device

I also tried to insert the following line before the import:

sys.path.append(PROJECT_ROOT) # pointing to src/
from test.test_framework import device

Also did not help:

import test.test_framework.device

the error is then:

ImportError: No module named test_framework.device

which looks to me as test was found but not the folder test_framework.

I am calling my program from src with something like python.exe myPackage/main.py

If I change the import to

sys.path.append(os.path.join(PROJECT_ROOT, 'test'))
from test_framework import device

it works fine but feels not right and pylint is complaining that the import cannot be resolved. My question looks like this one: ImportError: No module named - Python but I think I got the paths right. PROJET_ROOT is a relative path, I also tried to insert the absolute path on my disk there, but that did not help. I am pretty sure that PROJET_ROOT points correctly to src, we use this at many places.

maze
  • 789
  • 1
  • 7
  • 31
  • 1
    What happens if you import just `test` then print `test.__file__`? (or `sys.modules['test']`) You may well be conflicting with an other module somewhere and absolutely not importing what you're expecting. – Masklinn Dec 17 '19 at 08:44
  • Ahhh! Yes, of cause. Grrr. Due to some internal rules and using pytest, we need to call this folder test. But `import test`references to my python installation python27/lib/test. Thank you very much. Is there any way to tell python to use the right folder? – maze Dec 17 '19 at 08:57
  • 1
    Insert `PROJECT_ROOT` at the start of sys.path (`sys.path.insert(0, ...)`) instead of appending: Python's import machinery traverses `sys.path` in order, and the stdlib paths are in there. Interestingly / annoyingly things can seem to work when leveraging `cwd` because *that* is automatically inserted as the *first* entry in sys.path. Incidentally when you specify `PYTHONPATH` those are prepended to the list (before cwd is prepended as well), so running with something like PYTHONPATH=$PROJECT_ROOT is an other option. – Masklinn Dec 17 '19 at 09:16
  • I would like to upvote you but sadly stack overflow does not allow solutions in comments ^^ Thank you very much. – maze Dec 17 '19 at 09:28
  • I can reformat the comments as a proper answer if you want. – Masklinn Dec 17 '19 at 09:35

2 Answers2

0

That's not how import works. You need to import test.test_framework.device or if you have a specific class e.g. Device you can do from test.test_framework.device import Device.

po.pe
  • 1,047
  • 1
  • 12
  • 27
-1

you can run the code like this. python -m test.test_framework.device

https://stackoverflow.com/a/40304201/5573616

Tolga Sahin
  • 319
  • 2
  • 6