user2361174
user2361174

Reputation: 1932

Python Submodule is not getting imported

This is the file path for my Pydev project in Eclipse:

project
   |
   +----tests
   |      |
   |      +----subtests
   |      |       |
   |      |       +----__init__.py
   |      |       |
   |      |       +----test1.py
   |      |
   |      +----__init__.py
   |      |
   |      +----test2.py      
   |              
   +----mods
         |
         +----__init__.py
         |
         +----submods1
                 |
                 +----__init__.py
                 |
                 +----submods2
                         |
                         +----__init__.py
                         |
                         +----a.py
                         |
                         +----b.py
                         |
                        ...
                         |
                         +----z.py

test1 and test2 are exactly the same, all of the init files only have comments in them. The tests are getting the modules from the mods directory and those modules dependencies. When I run test1, all of the modules are found, but test2 always unable to find the same module (let's call it "z.py") in submods2. But somehow it's able to find the rest of the modules. It's not that it's unable to import something in z.py, it just cannot find the file at all.

test2:

>>> from mods.submod1.submod2 import z
    exec exp in global_vars, local_vars
  File "<console>", line 1, in <module>
ImportError: cannot import name z
>>> from mods.submod1 import submod2
>>> hasattr(submod2, 'z')
False

The only difference in the sys.path during the two tests are the directories that tests are located in, project/tests/subtests for test1 and project/tests for test2.

I cannot figure out why test2 is unable to import z.py but test1 can and test2 can import the rest of the modules.

Upvotes: 1

Views: 431

Answers (2)

user2361174
user2361174

Reputation: 1932

I think I found my solution to this. In my Run Configurations for test2, the Working directory in the Arguments tab had a custom path ${workspace_loc:project/tests/}, I switched it to the default path ${project_loc:/selected project name} and that seems to be fixing the issue. While I don't understand how this fixed the problem, the result is good enough for me.

Upvotes: 0

Fabio Zadrozny
Fabio Zadrozny

Reputation: 25332

To help diagnose the issue, do:

from mods.submod1 import submod2 print(submod2)

My guess is that it's not the module you're expecting.

What Python version are you using?

Upvotes: 1

Related Questions