2

I getting and error when I try to import a package from another folder. I have the folowing:

project  
|  
|-- main.py  
|  
|-- lib  
|   |-- __init__.py
|   |-- main_class.py
|   |-- global_functions.py
|   |-- cg_api_simple
|   |-- cg_api_status
|  
|-- tests    
    |-- __init__.py  
    |-- test_main_class.py  

Where my lib/__init__.py has:

from lib.main_class import PyGecko

and my test/__init__.py is empty.

When I run python3 -m unittest test_main_class.py I get:

======================================================================
ERROR: test_main_class (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_main_class
Traceback (most recent call last):
  File "/usr/lib/python3.8/unittest/loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
  File "/home/someone/Documents/pythoncoingecko/tests/test_main_class.py", line 1, in <module>
    from lib import PyGecko
ModuleNotFoundError: No module named 'lib'


----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

I've already tried:

  • Add /project and /project/lib to my PYTHONPATH
  • Use sys before import
  • Add .. before the import
  • Try to remove __init__.py from both folders

The only time I don't get an error is when I move test_main_class.py to where main.py is located. How could I solve this? You can see more on github

https://github.com/SrJMaia/pythoncoingecko

Thanks

JMaia
  • 79
  • 4
  • Have you tried running your unit tests from the "project" directory, rather than from "project/tests"? I think the *usual* structure is to have `project/src/main/` and `project/src/test` directories, with no actual code in `project/src/`, and having the extra layer between `${PWD}` and the code does seem to help... but Python's import errors are definitely confusing – Sarah Messer Apr 14 '22 at 16:47
  • Looks like a similar question is answered here: https://stackoverflow.com/questions/1260792/import-a-file-from-a-subdirectory – mhega Apr 14 '22 at 16:50
  • I tried and it doesn't work. I don't want to move the tests to the same folder as `project/lib`. I also followed this: https://stackoverflow.com/questions/61151/where-do-the-python-unit-tests-go – JMaia Apr 14 '22 at 18:15

1 Answers1

1

All you need is a proper combination of your current working directory, the PYTHONPATH environment variable and the path to the test.

If you run python3 -m unittest test_main_class.py from the tests directory, you need to add the project directory to PYTHONPATH. I.e.:

$ PYTHONPATH=.. python3 -m unittest test_main_class.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
$ PYTHONPATH=/tmp/project python3 -m unittest test_main_class.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

Or you can run python3 -m unittest tests/test_main_class.py from the project directory. In that case, you do not need to modify PYTHONPATH. I.e.

$ python3 -m unittest tests/test_main_class.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
radekholy24
  • 418
  • 2
  • 12