I'm trying to set up pytest with my project, and the directory structure is shown below.
├──
├── README.md
├── my_code
│ ├── __init__.py
│ ├── __main__.py
│ └── target_file.py
└── tests
│ ├── __init__.py
│ └── test_one.py
In my test_one.py I'm importing the target_file.py
with
import sys
import os
sys.path.insert(0, "my_code")
if I run pytest from the command line in the root directory this works. However if I cd
into a different directory, such as into tests
I get an error saying the module my_code
has no attribute bla bla bla. I assume this is because the way I am importing, it depends on where I actually am in the directory. So if I'm in the tests
directory, then it will try to import from /root/tests/my_code
, which doesn't exist. Is there a way to import target_file.py
into test_one.py
so that no matter where I am, I can go on the command line and run pytest, so that my tests will run? As it stands now, I have to go to the root directory so that the imports work correctly.