Py.test: ImportError: no module named mysql

I have problems running py.test on packages that import mysql. The mysql package was installed using pip in virtualenv.

# test_mysql.py import mysql def foo(): pass 

I can run python test_mysql.py without problems, but when I execute py.test test_mysql.py , I get:

 > import mysql E ImportError: No module named mysql 

Any idea what the problem is?

+8
python mysql virtualenv
source share
2 answers

It looks like you are using the system-wide version of py.test , which uses the non-virtualenv version of python. (In which the mysql package is not installed).

Install py.test inside virtualenv, then your problem will be solved.


FEEDBACK

To ensure that you are using the newly installed py.test , clear the cache command line file used by the shell.

For example, if you are using bash , run the following command:

 hash -r 
+15
source share

You can find the answer @falsetru does not work if you installed pytest system wide AFTER you installed in virtualenv, just like me.

(Quick note: py.test is an old version of the command, pytest is recommended, but what applies to one refers to another)

You can find out where pytest is loaded by calling which pytest .

As you can see, my pytest points to one that is not in virtualenv:

 (env) ~/projects/test/website $ which python /home/andrew/projects/test/website/env/bin/python (env) ~/projects/test/website $ which pytest /home/andrew/.local/bin/pytest 

Here's the proof that pytest IS is installed in virtualenv:

 (env) ~/projects/test/website $ pip install pytest Requirement already satisfied: pytest in ./env/lib/python3.5/site-packages Requirement already satisfied: py>=1.4.29 in ./env/lib/python3.5/site-packages (from pytest) 

And hash -r does not matter (since the new installation did not complete):

 (env) ~/projects/test/website $ hash -r (env) ~/projects/test/website $ which pytest /home/andrew/.local/bin/pytest 

The solution is to uninstall and reinstall pytest in virtualenv,

 (env) ~/projects/test/website $ pip uninstall pytest ... (env) ~/projects/test/website $ pip install pytest ... (env) ~/projects/test/website $ which pytest /home/andrew/projects/test/website/env/bin/pytest 

If for some reason you cannot reinstall, one way around this is to call pytest as a module through python in your virtualenv:

 python -m pytest 
0
source share

All Articles