-1

Hello Python Programmers

I'm getting a weird module import error during unittest. Not able to find the root cause. Here is how my directory structure looks like

Main_folder
   |
   |
   Module_x
       |  ABC.py
   |
   |
   Module_y
        | DEF.py
   |
   |
   test
       | unit_tests
             | test_ABC.py  

In test_ABC.py I'm importing the following

from Module_x import ABC

I get the error as "No module name Module_x"

I've created __init__.py file at each folder

I don't get any error if I use the same line from DEF.py

Please help if you're aware about why I am getting this issue?

I am using Python 3.5 Anaconda Distribution

Thanks

Daksh Gupta
  • 7,554
  • 2
  • 25
  • 36

1 Answers1

1

This is not a weird problem but a common one.

You can add your root into PYTHONPATH to solve this problem: PYTHONPATH=/path/to/project_root python test_ABC.py or something else similar.

Sraw
  • 18,892
  • 11
  • 54
  • 87
  • Hi Sraw, I call it weird because I can access the same from DEF.py without coming to any path related issue? Any idea ? – Daksh Gupta Jan 28 '19 at 06:05
  • The problem is how you run that `DEF.py`, I believe you run it under `Main_folder` while not run that test cases under `Main_folder`. It should be impossible that you can get `Main_folder` as `os.getcwd()` in your case. Or you should be able to find that module. – Sraw Jan 28 '19 at 06:08
  • Yes you're right... The DEF.py called from a file in main_folder. If my tests are called from a file placed in main_folder it works – Daksh Gupta Jan 28 '19 at 07:46