You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a Python project that is comprised of several small individual projects. The directory structure looks something like this.
01/
code.py
test_code.py
02/
code.py
test_code.py
If I work on this project in VSCode, I would like three things to be true.
If I go to the terminal and pass a file to the Python interpreter, it should execute properly. e.g., $ python test_code.py
If I use the VSCode debugger to launch the current Python file (pressing F5), it should execute properly.
If I use the VSCode Python test module (the flask icon), it should discover all the tests in the test_code.py files and run them properly.
Here is the problem.
The test_code.py files must necessarily import their accompanying code.py files in order to call and test the code they contain.
If I write the imports in the form import code then everything works fine if I run Python from the terminal or launch the VSCode debugger. However, the Python test module does not work. It fails to discover the tests. There is an error ModuleNotFoundError: No module named 'code'
If I change the imports to the relative form from . import code then the VSCode Python test module works. It is able to discover and run all the tests properly. However, trying to run the test file using the debugger or using Python in the terminal no longer works. It gives an error ImportError: attempted relative import with no know parent package.
How can I structure the imports to make all of these things work at the same time?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have a Python project that is comprised of several small individual projects. The directory structure looks something like this.
If I work on this project in VSCode, I would like three things to be true.
$ python test_code.py
test_code.py
files and run them properly.Here is the problem.
The
test_code.py
files must necessarily import their accompanyingcode.py
files in order to call and test the code they contain.If I write the imports in the form
import code
then everything works fine if I run Python from the terminal or launch the VSCode debugger. However, the Python test module does not work. It fails to discover the tests. There is an errorModuleNotFoundError: No module named 'code'
If I change the imports to the relative form
from . import code
then the VSCode Python test module works. It is able to discover and run all the tests properly. However, trying to run the test file using the debugger or using Python in the terminal no longer works. It gives an errorImportError: attempted relative import with no know parent package
.How can I structure the imports to make all of these things work at the same time?
Beta Was this translation helpful? Give feedback.
All reactions