PATH issue with pytest 'ImportError: No module named YadaYadaYada'
Yes, the source folder is not in Python's path if you cd
to the tests directory.
You have 2 choices:
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 + '/../')Run the tests with the env var
PYTHONPATH=../
.
pytest cannot import module while python can
Found the answer:
DO NOT put a __init__.py
file in a folder containing TESTS if you plan on using pytest. I had one such file, deleting it solved the problem.
This was actually buried in the comments to the second answer of PATH issue with pytest 'ImportError: No module named YadaYadaYada' so I did not see it, hope it gets more visibility here.
How to solve ImportError with pytest
The self-named module testingonly
and file name of testingonly.py
may be causing some issues with the way the modules are imported.
Remove the __init__.py
from the tests
directory. Ref this answer .
Try renaming testingonly.py
to mytest.py
and then importing it into your project again.
In the cli.py
, it should be:
from testingonly.mytest import Tester
And then for your test file test_testingonly.py
:
from testingonly.mytest import Tester
You're test_testingonly.py
file should look like this:
import pytest
from testingonly.mytest import Tester # Import the Tester Class
def test_tester(capsys):
# Create the Tester Class
te = Tester()
# Get the captured output
captured = capsys.readouterr()
# Assert that the capture output is tested
assert captured.out == "Hello\n"
Finally, Run your tests with:
python -m pytest tests/
Here is a fully working example based off of your code: https://github.com/cdesch/testingonly
Pytest No module named... issue
I was having the same issues with imports, the way i fixed it was setting all the imports so that the code runs. In order to run the tests instead of just calling pytest
(this runs all tests from the folder where they are located, therefore there will be import error) it should be called from the root directory like this:
python3 -m pytest
or
python -m pytest
this way it runs all tests from the root directory, and it should not give import errors
pytest module import ImportError: No module named
you're running
myapp/tests/test_stuff.py
you need to edit the path in test_stuff.py
to point to myapp
(which contains __init__.py
)
I tested this on my system (file test_stuff.py
):
import sys,os
sys.path.append(os.path.join(os.path.dirname(__file__),os.pardir,"myapp"))
import myapplib
(getting full path of your python module, then using parent dir to add the python path to just the second level of myapp
)
works on my dirtree configured like yours:
S:\python\myapp\myapp
S:\python\myapp\tests
S:\python\myapp\myapp\myapplib
S:\python\myapp\myapp\myapplib\__init__.py
S:\python\myapp\tests\test_stuff.py
Advice: when fiddling with sys.path.append()
:
- always use absolute paths, using
__file__
to make sure that the current directory doesn't get in the way. never depend on the current directory, or this trick will only work when in a given directory. - debug: print the path you're adding to check if it's the proper one. Since it's absolute, you'll easily see if it's correct.
PATH issue with pytest 'ImportError: No module named YadaYadaYada'
Yes, the source folder is not in Python's path if you cd
to the tests directory.
You have 2 choices:
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 + '/../')Run the tests with the env var
PYTHONPATH=../
.
ModuleNotFoundError with pytest
Solution: use the PYTHONPATH
env. var
PYTHONPATH=. pytest
As mentioned by @J_H, you need to explicitly add the root directory of your project, since pytest
only adds to sys.path
directories where test files are (which is why @Mak2006's answer worked.)
Good practice: use a Makefile or some other automation tool
If you do not want to type that long command all the time, one option is to create a Makefile
in your project's root dir with, e.g., the following:
.PHONY: test
test:
PYTHONPATH=. pytest
Which allows you to simply run:
make test
Another common alternative is to use some standard testing tool, such as tox.
Py.test No module named *
So you are running py.test
from /App
. Are you sure /App/App
is in your $PYTHONPATH
?
If it's not, code that tries to import app
will fail with such a message.
EDIT0: including the info from my comment below, for completeness.
An attempt to import app will only succeed if it was executed inside /App/App
, which is not the case here. You probably want to make /App/App
a package by putting __init__.py
inside it, and change your import to qualify app as from App import app
.
EDIT1: by request, adding further explanation from my second comment below.
By putting __init__.py
inside /App/App
, that directory becomes a package. Which means you can import from it, as long as it - the directory - is visible in the $PYTHONPATH
. I.e. you can do from App import app
if /App
is in the $PYTHONPATH
. Your current working directory gets automatically added to $PYTHONPATH
, so when you run a script from /App
, the import will work.
Related Topics
How to Understand Closure in a Lambda
How to Get the Path and Name of the File That Is Currently Executing
What Does It Mean If a Python Object Is "Subscriptable" or Not
Understanding Repr( ) Function in Python
Convert String to Variable Name in Python
How to Plot Data from Multiple Two Column Text Files With Legends in Matplotlib
How to Call a Function from Another .Py File
Sort a List of Tuples by 2Nd Item (Integer Value)
How to Translate an Iso 8601 Datetime String into a Python Datetime Object
How to Install Pip on Macos or Os X
How to Get the Last Element of a List
What Is the Syntax Rule For Having Trailing Commas in Tuple Definitions
Basic Query Regarding Bindtags in Tkinter
How to Create a New Column from the Output of Pandas Groupby().Sum()