0

I have been attmepting to solve this for months now, and for the life of me cannot figure it out. Every solution I have tried from stack overflow has failed me so far :(

The problem is simple. Let say I have a module, utils, that contains some function. Now, lets also say I have a tests folder where I want to setup a unit test for that function.

Project
|
+—  venv
|
+— .gitignore
|
+— requirements.txt
|
+— main.py
|
+— utils/
|   +— __init__.py
|   +— test_function.py 
+— tests/
|   +— test.py

From sample projects I have seen on github, this is very simple to import. You would just say:

from utils.test_function import some_function

However, when I attempt this, I get the error

ModuleNotFoundError: No module named utils

From previous searches, I have found a temporary solution of appending the module to my path.

sys.path.append('absolute_path/utils')
import some_function

However, this seems like a terrible solution considering I need an absolute path. Furthermore, this does not seem the best practice based on every github project I have seen. Sample repo that demonstrates this. However, I cannot figure out how to do this correctly. Thank you for the help.

Edit: Please note, I am not asking the exact same questions as this: Running unittest with typical test directory structure

Another example I have is when I am running jupyter notebook in a notebooks/ folder, and want to import a function from another module in my project. Same issue as the original question.

Jeff
  • 71
  • 5

2 Answers2

0

Your structure is right.

What IDE are you using? That could be your solution, a better IDE that helps you to understand what are you importing.

I recreated that env in my local machine and I get the following result:

utils.py

pytest.py

  • I'm using vscode. So it just works for you? Possibly something wrong with how I installed python / setup the ide – Jeff Jul 13 '22 at 22:39
  • 1
    Can you execute a python file on your machine? for example, if you add a print statement in a py file can you run it and see the result in your terminal? Just to discard that is a Python problem! You should try with PyCharm community edition it is free and maybe you can solve your problem with that. – Andres Montero Jul 13 '22 at 23:06
  • Yes, running is perfectly fine as long as I am not using relative imports – Jeff Jul 13 '22 at 23:10
  • 1
    Then your problem is not Python, I guess that is how your IDE is recognizing the project structure. – Andres Montero Jul 14 '22 at 22:08
  • Hey man, was able to solve the issue finally. Looks like it is something handled by default in Pycharm but not VScode, which is actually the hint that led me down the path to the answer, so thanks! – Jeff Jul 15 '22 at 01:08
0

The standard solution to this is run from the Project root using the -m swicth as in

python -m tests.test # note no py

That is for running a __main__ block (or the script itself) - you might need to change this to something like python -m unittest -m tests.test to run the unittest framework. VScode should let you specify the -m switch in its run config

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361