397

I used easy_install to install pytest on a Mac and started writing tests for a project with a file structure likes so:

repo/
   |--app.py
   |--settings.py
   |--models.py
   |--tests/
          |--test_app.py

Run py.test while in the repo directory, and everything behaves as you would expect.

But when I try that same thing on either Linux or Windows (both have pytest 2.2.3 on them), it barks whenever it hits its first import of something from my application path. For instance, from app import some_def_in_app.

Do I need to be editing my PATH to run py.test on these systems?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
MattoTodd
  • 14,467
  • 16
  • 59
  • 76
  • 4
    [Here](http://docs.pytest.org/en/latest/goodpractices.html#integrating-with-setuptools-python-setup-py-test-pytest-runner) is the way to fix it with setuptools. – ederag Oct 14 '16 at 21:27
  • 4
    Please check @hoefling answer and consider changing your accepted one, if SO allows after this long: much better! – Davide Sep 20 '18 at 21:42

28 Answers28

453

I'm not sure why py.test does not add the current directory in the PYTHONPATH itself, but here's a workaround (to be executed from the root of your repository):

python -m pytest tests/

It works because Python adds the current directory in the PYTHONPATH for you.

Apteryx
  • 5,822
  • 3
  • 16
  • 18
  • 3
    It requires to rewrite relative imports to absolute ones, if you have the code for running the application not on the level, where you execute the command from. For example: `project/test/all-my-tests` and `project/src/app.py` and because of that change, one needs to call the `app.py` indirectly using a `__main__.py` file in `project/src`, so that one can use the call `python -m src`. Pretty messy stuff as far as I can tell. – Zelphir Kaltstahl Sep 26 '16 at 15:44
  • 6
    @Zelphir: Using absolute imports is a recommended practice. Habnabit's has a good article about packaging best practices: http://blog.habnab.it/blog/2013/07/21/python-packages-and-you/, and PEP8 says that "implicit relative imports should never be used and have been removed in Python 3." See: https://www.python.org/dev/peps/pep-0008/. – Apteryx Nov 04 '16 at 20:23
  • 2
    @Apteryx You mean "project-absolute" right? Because things like `/home/user/dev/projectxyz/src ...` would be really bad and not run on other machines in most cases. I think what I meant is, that I have to always write the whole project root to module path even if a module is in the same folder as the file run. I didn't know that this is considered best practice, so that's a useful bit of information, thanks. I agree with most of pep8, although it is still not perfect. – Zelphir Kaltstahl Nov 06 '16 at 19:44
  • 2
    @Zelphir, yes, that's what I meant. I believe the term absolute imports in Python always refer to "project-absolute". See: https://www.python.org/dev/peps/pep-0328/#rationale-for-absolute-imports. In fact, I'm pretty sure you can't import from random, absolute paths locations, at least using the default "import" mechanism. – Apteryx Nov 07 '16 at 18:37
  • 12
    I have added `__init__.py` in tests, that solved the problem. Now I can use `pytest` – Kiran Kumar Kotari Dec 26 '18 at 06:08
  • In my experience, current directory has to be where the script is with which you are facing the problem. – UzumakiL Aug 25 '22 at 10:08
  • Running as `python -m pytest` rather than just `pytest` fixed my import issues when running on github workflows! Thank you! – Joel Gray Nov 11 '22 at 12:10
433

Recommended approach for pytest>=7: use the pythonpath setting

Recently, pytest has added a new core plugin that supports sys.path modifications via the pythonpath configuration value. The solution is thus much simpler now and doesn't require any workarounds anymore:

pyproject.toml example:

[tool.pytest.ini_options]
pythonpath = [
  "."
]

pytest.ini example:

[pytest]
pythonpath = .

The path entries are calculated relative to the rootdir, thus . adds repo directory to sys.path in this case.

Multiple path entries are also allowed: for a layout

repo/
├── src/
|   └── lib.py
├── app.py
└── tests
     ├── test_app.py
     └── test_lib.py

the configuration

[tool.pytest.ini_options]
pythonpath = [
  ".", "src",
]

or

[pytest]
pythonpath = . src

will add both app and lib modules to sys.path, so

import app
import lib

will both work.

Original answer (not recommended for recent pytest versions; use for pytest<7 only): conftest solution

The least invasive solution is adding an empty file named conftest.py in the repo/ directory:

$ touch repo/conftest.py

That's it. No need to write custom code for mangling the sys.path or remember to drag PYTHONPATH along, or placing __init__.py into dirs where it doesn't belong (using python -m pytest as suggested in Apteryx's answer is a good solution though!).

The project directory afterwards:

repo
├── conftest.py
├── app.py
├── settings.py
├── models.py
└── tests
     └── test_app.py

Explanation

pytest looks for the conftest modules on test collection to gather custom hooks and fixtures, and in order to import the custom objects from them, pytest adds the parent directory of the conftest.py to the sys.path (in this case the repo directory).

Other project structures

If you have other project structure, place the conftest.py in the package root dir (the one that contains packages but is not a package itself, so does not contain an __init__.py), for example:

repo
├── conftest.py
├── spam
│   ├── __init__.py
│   ├── bacon.py
│   └── egg.py
├── eggs
│   ├── __init__.py
│   └── sausage.py
└── tests
     ├── test_bacon.py
     └── test_egg.py

src layout

Although this approach can be used with the src layout (place conftest.py in the src dir):

repo
├── src
│   ├── conftest.py
│   ├── spam
│   │   ├── __init__.py
│   │   ├── bacon.py
│   │   └── egg.py
│   └── eggs 
│       ├── __init__.py
│       └── sausage.py
└── tests
     ├── test_bacon.py
     └── test_egg.py

beware that adding src to PYTHONPATH mitigates the meaning and benefits of the src layout! You will end up with testing the code from repository and not the installed package. If you need to do it, maybe you don't need the src dir at all.

Where to go from here

Of course, conftest modules are not just some files to help the source code discovery; it's where all the project-specific enhancements of the pytest framework and the customization of your test suite happen. pytest has a lot of information on conftest modules scattered throughout their docs; start with conftest.py: local per-directory plugins

Also, SO has an excellent question on conftest modules: In py.test, what is the use of conftest.py files?

hoefling
  • 59,418
  • 12
  • 147
  • 194
  • 2
    This does not work, simple as that. Try putting a file at the root directory, and then importing that from a test. Setting PYTHONPATH to root works fine, this hack does not help at all. – aaa90210 Jan 15 '19 at 21:55
  • 2
    @aaa90210 although I can't reproduce your issue (importing from a conftest in a root dir works on any level), you should never import from conftest files as it's a reserved name for `pytest` and it's strongly advised not to do so. By doing that, you plant seeds for future errors. Create another module named `utils.py` and place the code for reusing in tests there. – hoefling Jan 15 '19 at 22:09
  • For some reason that lead to ModuleNotFoundError on travisCI when also installing the package. So watch out! – Jarno Jun 13 '19 at 08:46
  • @Jarno can you give a link to the affected repository? If you get a `ModuleNotFound` by changing the `sys.path`, it is an indicator of errors in project/imports layout (most probably name shadowing). – hoefling Jun 13 '19 at 08:54
  • @hoefling unfortunately the project is currently private. But then it worked locally, but failed on travis. I also had nested modules involved. I can try to come up with a minimal non working example. – Jarno Jun 13 '19 at 09:22
  • @Jarno you can also ask a new question and drop the link in the comments here, I'd be happy to help. – hoefling Jun 13 '19 at 09:28
  • This fixed it, but I also needed to remove the __init__.py file which I had in root – RS1980 Feb 20 '20 at 15:32
  • 10
    Logically, `conftest.py` does not belong to the application code and, imo, placing it under `src/` is not correct. – Nik O'Lai Apr 27 '20 at 14:01
  • Plus extra wheel of cheese for using proper file names instead of foo and bar! – ruslaniv Feb 23 '21 at 10:05
  • The configuration with pyproject.toml when using pytest > 7 was a piece of the puzzle. But what finally enabled the imports in my tests to resolve was adding this to the `[tool.pytest.ini_options]`: ```import-mode = "importlib"``` See: https://docs.pytest.org/en/7.1.x/explanation/pythonpath.html This is supposed to be the default mode in the future – cervezas Apr 29 '22 at 21:12
  • After multiple attempts, your "pythonpath = . src" for the pytest.ini solved my issue. Note, to avoid issues, place you pytest.ini under your root directory (initially not the case on my side, which caused some issues...) – Greg7000 May 05 '22 at 18:31
  • `pythonpath = .` works for me. Now I can run `pytest` or `py.test` from where I want in the project. – Morticia A. Addams May 24 '22 at 10:48
151

I had the same problem. I fixed it by adding an empty __init__.py file to my tests directory.

Aron Curzon
  • 2,524
  • 2
  • 19
  • 16
  • 104
    Note that this is *NOT* recommended by py.test: `avoid “__init__.py” files in your test directories. This way your tests can run easily against an installed version of mypkg, independently from the installed package if it contains the tests or not.` SRC: http://pytest.org/latest/goodpractises.html – K.-Michael Aye May 30 '14 at 21:52
  • 38
    I came here with the same question and found removing `__init__.py` from my tests directory solved it for me. – 101 Oct 13 '15 at 00:24
  • 3
    @K.-MichaelAye How are you supposed to import modules in your tests, if the tests directory is not a package?? – mafrosis Nov 03 '15 at 15:11
  • 7
    @mafro i don't see the problem?The tests don't have to be importable code, they are found by your test runner. Only the code TO BE TESTED should be an installed package/module, not the tests. – K.-Michael Aye Nov 03 '15 at 19:01
  • @K.-MichaelAye I believe my problem stems from using relative imports - the test code cannot find the application code which is to be tested – mafrosis Nov 04 '15 at 11:12
  • 4
    Test code should not use relative imports, yes. Import-style wise, you should consider test-code as not part of the package but just importing everything via the installed package name.But still,I fail to see how this would even theoretically be solved by making the test code a package? – K.-Michael Aye Nov 04 '15 at 14:50
  • 6
    Adding an `__init__.py` in sub-directories of `test/` makes absolute import work for running specific tests in that sub-directory against to-be installed modules. Thanks. – Bryce Guinta Sep 09 '16 at 22:01
  • 2
    @K.-MichaelAye link is deads – jmunsch Jan 11 '17 at 21:10
  • This is the most elegant solution, it makes the imports inside the test behave the same way they do in code. – Yves Dorfsman Apr 12 '18 at 04:50
  • 4
    `__init__.py` files in test paths are no longer deprecated, looking at https://docs.pytest.org/en/latest/goodpractices.html (where latest==pytest-4.0 currently). Makes a whole lot of sense to me if you have larger test bodies and want to modularize your code (yes, test code!). – ThomasH Jan 08 '19 at 17:07
132

Yes, the source folder is not in Python's path if you cd to the tests directory.

You have two choices:

  1. Add the path manually to the test files. Something like this:

     import sys, os
     myPath = os.path.dirname(os.path.abspath(__file__))
     sys.path.insert(0, myPath + '/../')
    
  2. Run the tests with the env var PYTHONPATH=../.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Not_a_Golfer
  • 47,012
  • 14
  • 126
  • 92
  • 15
    when am i `cd`ing to a directory? i am running `py.test` from my root. unless I am mistaken and you mean as pytest walks through my folders – MattoTodd Apr 20 '12 at 21:46
  • 1
    if it was a `cd` issue, wouldn't i hit it on mac as well? – MattoTodd Apr 20 '12 at 21:46
  • Oh, I misread and thought it doesn't work from the tests directory. still the trick in suggestion 1 would work. I only use Linux so I can't explain the behavior on other OSes. – Not_a_Golfer Apr 20 '12 at 21:50
  • do you have an import like that on all your test.py files? – MattoTodd Apr 20 '12 at 21:50
  • 4
    yes, but my directory structure is usually slightly different - I usually keep /src and /test under the root directory. – Not_a_Golfer Apr 20 '12 at 21:51
  • Is there a best practice to prime the tests with a modified pythonpath? I want to just write `py.test` and it uses some config file (I have seen pytest.py file in some projects roots). – atripes Aug 04 '17 at 13:13
  • I "fixed" it by changing where I created my `venv` directory. Creating my `venv/` directory right next to my `tests/` (and not simply at the root of the repo) allowed pytest to see it without having to modify the path. – Everett Oct 30 '19 at 17:13
  • Thank you so much for this answer! I've spent hours already on this. – The Grindfather Feb 08 '21 at 23:40
  • @JAWS007 this answer is 9 years old, I thought this was fixed by now :) – Not_a_Golfer Feb 09 '21 at 08:06
  • This was the only answer here that worked for me! Thank you. – Pat Jan 10 '22 at 14:56
  • This answer has worked for me @Not_a_Golfer when I have appended the first option before my imports, so I thought (and did) move that code to my __init__.py file right next to my tests, and it worked like a charm, so sweet. didn't have to repeat it again. – mr.xed Dec 28 '22 at 00:30
63

Run pytest itself as a module with: python -m pytest tests

This happens when the project hierarchy is, for example, package/src package/tests and in tests you import from src. Executing as a module will consider imports as absolute rather than relative to the execution location.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stefano Messina
  • 1,796
  • 1
  • 17
  • 22
  • 1
    This solution helped me, thanks! The cause of this was because of the conflict in Python version. pytest test works for earlier python version. In my situation, my python version is 3.7.1, python -m pytest tests works but not pytest tests. – Ruxi Zhang Jan 04 '19 at 19:16
  • 6
    From [Pytest](https://docs.pytest.org/en/latest/pythonpath.html#pythonpath) "Running pytest with python -m pytest [...] instead of pytest [...] yields nearly equivalent behaviour, except that the former call will add the current directory to sys.path." – Moad Ennagi Sep 04 '19 at 14:55
  • This is the cleanest solution so far – Debajit Jun 10 '21 at 01:53
41

You can run with PYTHONPATH in project root

PYTHONPATH=. py.test

Or use pip install as editable import

pip install -e .   # install package using setup.py in editable mode
Ford Guo
  • 957
  • 9
  • 18
  • 3
    That didn't work for me with a `test` directory not in `src` directory structure and calling from the directory containing both `test` and `src` directory. – Zelphir Kaltstahl Mar 13 '16 at 22:53
29

I had the same problem in Flask.

When I added:

__init__.py

to the tests folder, the problem disappeared :)

Probably the application couldn't recognize folder tests as a module.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user13037517
  • 299
  • 3
  • 2
24

I created this as an answer to your question and my own confusion. I hope it helps. Pay attention to PYTHONPATH in both the py.test command line and in the tox.ini.

https://github.com/jeffmacdonald/pytest_test

Specifically: You have to tell py.test and tox where to find the modules you are including.

With py.test you can do this:

PYTHONPATH=. py.test

And with tox, add this to your tox.ini:

[testenv]
deps= -r{toxinidir}/requirements.txt
commands=py.test
setenv =
    PYTHONPATH = {toxinidir}
Jeff MacDonald
  • 457
  • 4
  • 3
17

I fixed it by removing the top-level __init__.py in the parent folder of my sources.

Gonzalo
  • 3,674
  • 2
  • 26
  • 28
10

I started getting weird ConftestImportFailure: ImportError('No module named ... errors when I had accidentally added __init__.py file to my src directory (which was not supposed to be a Python package, just a container of all source).

Sociopath
  • 13,068
  • 19
  • 47
  • 75
jbasko
  • 7,028
  • 1
  • 38
  • 51
9

It is a bit of a shame that this is an issue in Python... But just adding this environment variable is the most comfortable way, IMO:

export PYTHONPATH=$PYTHONPATH:.

You can put this line in you .zshrc or .bashrc file.

tbrodbeck
  • 460
  • 1
  • 8
  • 16
  • 5
    This is indeed a shame, not a bit of a shame. – Sarye Haddadi Nov 17 '21 at 15:50
  • 1
    `python -m ` will add the current working directory (`.`) to the PYTHONPATH automatically. – niid Jul 20 '22 at 12:12
  • It may be comfortable, but what is the consequence of it? E..g, what about security? At least the concern could be addressed in the answer (but ******* ***without*** ******* "Edit:", "Update:", or similar - the answer should appear as if it was written today).. – Peter Mortensen Jan 21 '23 at 17:58
6

I was having the same problem when following the Flask tutorial and I found the answer on the official Pytest documentation. It's a little shift from the way I (and I think many others) are used to do things.

You have to create a setup.py file in your project's root directory with at least the following two lines:

from setuptools import setup, find_packages
setup(name="PACKAGENAME", packages=find_packages())

where PACKAGENAME is your app's name. Then you have to install it with pip:

pip install -e .

The -e flag tells pip to install the package in editable or "develop" mode. So the next time you run pytest it should find your app in the standard PYTHONPATH.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
3

I got this error as I used relative imports incorrectly. In the OP example, test_app.py should import functions using e.g.

from repo.app import *

However liberally __init__.py files are scattered around the file structure, this does not work and creates the kind of ImportError seen unless the files and test files are in the same directory.

from app import *

Here's an example of what I had to do with one of my projects:

Here’s my project structure:

microbit/
microbit/activity_indicator/activity_indicator.py
microbit/tests/test_activity_indicator.py

To be able to access activity_indicator.py from test_activity_indicator.py I needed to:

  • start test_activity_indicatory.py with the correct relative import:
    from microbit.activity_indicator.activity_indicator import *
  • put __init__.py files throughout the project structure:
    microbit/
    microbit/__init__.py
    microbit/activity_indicator/__init__.py
    microbit/activity_indicator/activity_indicator.py
    microbit/tests/__init__.py
    microbit/tests/test_activity_indicator.py
Oppy
  • 2,662
  • 16
  • 22
3

I had a similar issue. pytest did not recognize a module installed in the environment I was working in.

I resolved it by also installing pytest into the same environment.

nocibambi
  • 2,065
  • 1
  • 16
  • 22
  • Although I was using pytest from inside a venv, I also had it installed globally which gave me this error. After uninstalling the global version and installing inside the venv it worked. – Markus Ressel Mar 10 '20 at 03:41
3

Also if you run pytest within your virtual environment make sure pytest module is installed within your virtual environment. Activate your virtual environment and run pip install pytest.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alireza
  • 191
  • 1
  • 4
  • Exactly my problem. As I only use pytest as a test runner and since I also allow my virtualenv to "see" global packages, I thought I'd get cute and remove the pytest from the virtualenv and rely on the global one instead. The messages and errors are all over the map but I noticed my PYTHONPATH did not look at all like the virtualenvs. The other thing is that I could not `pip install pytest` in that virtualenv, it kept on telling me the requirement was already met (by the global `pytest`). Had to `deactivate`, `pip uninstall pytest` (global), `activate`, `pip install pytest`. Pytest: v6.25 – JL Peyret Jan 02 '22 at 01:31
2

For me the problem was tests.py generated by Django along with tests directory. Removing tests.py solved the problem.

Paweł Mucha
  • 226
  • 2
  • 3
2

According to a post on Medium by Dirk Avery (and supported by my personal experience) if you're using a virtual environment for your project then you can't use a system-wide install of pytest; you have to install it in the virtual environment and use that install.

In particular, if you have it installed in both places then simply running the pytest command won't work because it will be using the system install. As the other answers have described, one simple solution is to run python -m pytest instead of pytest; this works because it uses the environment's version of pytest. Alternatively, you can just uninstall the system's version of pytest; after reactivating the virtual environment the pytest command should work.

Einhaender
  • 99
  • 7
1

I was getting this error due to something even simpler (you could even say trivial). I hadn't installed the pytest module. So a simple apt install python-pytest fixed it for me.

'pytest' would have been listed in setup.py as a test dependency. Make sure you install the test requirements as well.

pbskumar
  • 1,127
  • 12
  • 14
craq
  • 1,441
  • 2
  • 20
  • 39
1

Since no one has suggested it, you could also pass the path to the tests in your pytest.ini file:

[pytest]
...
testpaths = repo/tests

See documentation: https://docs.pytest.org/en/6.2.x/customize.html#pytest-ini

Side effect for Visual Studio Code: it should pick up the unit test in the UI.

thoroc
  • 3,291
  • 2
  • 27
  • 34
0

As pointed out by Luiz Lezcano Arialdi, the correct solution is to install your package as an editable package.

Since I am using Pipenv, I thought about adding to his answer a step-by-step how to install the current path as an edible with Pipenv, allowing to run pytest without the need of any mangling code or lose files.

You will need to have the following minimal folder structure (documentation):

package/
    package/
        __init__.py
        module.py
    tests/
        module_test.py
    setup.py

setup.py mostly has the following minium code (documentation):

import setuptools

setuptools.setup(name='package', # Change to your package name
                 packages=setuptools.find_packages())

Then you just need to run pipenv install --dev -e . and Pipenv will install the current path as an editable package (the --dev flag is optional) (documentation).

Now you should be able to run pytest without problems.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

We have fixed the issue by adding the following environment variable.

PYTHONPATH=${PYTHONPATH}:${PWD}/src:${PWD}/test
SANN3
  • 9,459
  • 6
  • 61
  • 97
0

If this pytest error appears not for your own package, but for a Git-installed package in your package's requirements.txt, the solution is to switch to editable installation mode.

For example, suppose your package's requirements.txt had the following line:

git+https://github.com/foo/bar.git

You would instead replace it with the following:

-e git+https://github.com/foo/bar.git#egg=bar
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

If nothing works, make sure your test_module.py is listed under the correct src directory. Sometimes it will give ModuleNotFoundError not because modules are misplaced or export PYTHONPATH="${PWD}:${PYTHONPATH}" is not working, its because test_module.py is placed into a wrong directory under the tests folder.

it should be 1-to-1 mapping relation recursively instead of the root folder should be named as "tests" and the name of the file that include test code should starts with "test_", for example,

./nlu_service/models/transformers.py

./tests/models/test_transformers.py


This was my experience.

0

If you're trying to run via VSCode's test runner, in addition to setting PYTHONPATH env by whichever method you choose, try in settings.json:

{
  "python.testing.cwd": "${workspaceFolder}",
  // or an appropriate path to your cwd
}
brandonscript
  • 68,675
  • 32
  • 163
  • 220
0

Recently, I faced a situation where the local imports in the src (target code under test) would fail due to import errors.

We all know that the file is simple not found. So my strategy would be to the folder containing the source in sys.path in a fixture.

My folder structure looks like this:

main_folder
--- target_file.py
--- utils.py
--- sub_utils.py
--- tests
   --- test_target.py
   --- conftest.py

When I run my tests, the import in my target_file (they look like from utils.py import some_function) . Also utils.py imports functions from sub_utils.py. In all, pandemonium.

What worked for me:

In my conftest.py, I use a fixture like this:

@pytest.fixture(scope="module")
def import_target_file():
    from pathlib import Path
    import sys

    sys.path.append(str(Path(__file__).parent.parent))
    get_target_file = importlib.import_module("main_folder.target_file")
    yield get_target_file 

How does my test file looks:

class TestGetAppointments:
    def test_valid_event(self, import_target_file):
        """Test to verify correct response when valid event is provided."""
        test_event = {
            "pathParameters": {
                "start": "1",
                "end": "1",
            }
        }
        response = import_target_file.some_function(test_event)
Arindam Roychowdhury
  • 5,927
  • 5
  • 55
  • 63
0

My set-up

  1. Had a venv setup in the vs-code.
  2. vs-code was pointing to the correct executable.
  3. Even had pytest.ini setup as:
[pytest]
pythonpath = .
  1. Tried exporting the PYTHONPATH but was not somehow working in the virtual environment.
  2. My tree looked like:
root
 venv
 __init__.py
 actions
    actions.py
    __init__.py
 tests
    test_actions.py
 pytest.ini
  1. In test_actions.py I was doing:
from actions.actions import ActionFetch
  1. But was getting No action module found exception
  2. Virtual environment was also active.

Solution: I simple ran tests using:

pytest tests --import-mode=append

See https://docs.pytest.org/en/7.1.x/explanation/pythonpath.html#:~:text=%2D%2Dimport%2Dmode%3Dappend

-1

Very often the tests were interrupted due to module being unable to be imported.

After research, I found out that the system is looking at the file in the wrong place and we can easily overcome the problem by copying the file, containing the module, in the same folder as stated, in order to be properly imported.

Another solution proposal would be to change the declaration for the import and show MutPy the correct path of the unit. However, due to the fact that multiple units can have this dependency, meaning we need to commit changes also in their declarations, we prefer to simply move the unit to the folder.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Baydaa
  • 1
  • There are other answers that provide the OP's question, and they were posted some time ago. When posting an answer, please make sure you add either a new solution, or a substantially better explanation, especially when answering older questions. Sometimes it's better to post a comment on a particular answer. – help-info.de Oct 05 '19 at 17:16
  • As addition to the comment from @help-info.de: Here is the link to the guide for answering questions: https://stackoverflow.com/help/how-to-answer – the hand of NOD Oct 05 '19 at 17:30
  • What is this "MutPy" you speak of? – Peter Mortensen Jan 21 '23 at 16:40
  • OK, the OP has left the building - *"Last seen more than 3 years ago"* – Peter Mortensen Jan 21 '23 at 16:41
-1

My solution:

Create the conftest.py file in the test directory containing:

import os
import sys
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + "/relative/path/to/code/")

This will add the folder of interest to the Python interpreter path without modifying every test file, setting environment variable or messing with absolute/relative paths.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131