Domanda

I have problems running py.test over packages that imports mysql. The package mysql was installed with pip in a virtualenv.

# test_mysql.py

import mysql

def foo():
  pass

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

>   import mysql
E   ImportError: No module named mysql

Any idea what's the problem?

È stato utile?

Soluzione

It seems like you're using system-wide version of py.test which use non-virtualenv-version of python. (In which, mysql package is not installed).

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


SIDE NOTE

To make sure you're using the newly-installed py.test, clear the command path cache used by shell.

For example, if you use bash, issue the following command:

hash -r

Altri suggerimenti

You might find @falsetru's answer doesn't work if you installed pytest system wide AFTER you installed in in the virtualenv, as I did.

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

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

As you can see, my pytest points one that is not in the 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 proof that pytest IS installed in the 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 makes no difference (as no new installation happened):

(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 the 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 can't reinstall, one way to get round this is by calling pytest as a module via the python in your virtualenv:

python -m pytest
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top