stockersky
stockersky

Reputation: 1571

pytest fails to import module installed via pip and containing underscores

I am used to see import errors of my own modules. But this time, this is about modules installed via pip and located in site-packages.

Here is a test file expurged from everything but just the imports :

import flask
import pygments
from flask_restful import Resource, Api
#from weather_tool import *
#from flask_restless import *

while running pytest :

Traceback:
test_MonAPPflaskRest.py:3: in <module>
from flask_restful import Resource, Api

E ModuleNotFoundError: No module named 'flask_restful'

Actually, every module with an underscore will fail !!!

flask_restless will fail to import to.

But it work when executed outside of pytest, or simply on the python shell...

Upvotes: 2

Views: 6531

Answers (3)

Vinay Namadi
Vinay Namadi

Reputation: 99

I also faced the same issue when installed in virtual environment. I've deactivated virtual environment, installed flask-restful from PIP and then activated virtual environment. The issue is resolved.

Upvotes: 1

Bhrigu Bhanot
Bhrigu Bhanot

Reputation: 41

I got a similar error while importing stocker (Unable to find stocker module). Try adding your 'code directory' to 'sys.path' where python looks for modules and it should be able to import then.

For more details: https://bic-berkeley.github.io/psych-214-fall-2016/sys_path.html

I used the code:

 >>> import sys
   >>> sys.path.append('code')    # code = the directory where your module is saved
   >>> # Now the import will work
   >>> import a_module           # a_module = the module you are trying to import

Upvotes: 1

stockersky
stockersky

Reputation: 1571

Ok, I went through it.

Actually, Anaconda was installed. From here, no problem with that.

I installed Python from source as I'm used to do on a Linux platform and it works normally as expected.

I found out that pytest was not is the list of packages installed via pip. Seems Anaconda provides a default pytest install. Maybe something is wrong with this version. (which pytest will return a pystest file in the python bin directory.

Actually, a simple pip install pytest in your virtualenv will 'overwrite' this - maybe messy - pytest version.

However calling pytest won't still work. You have to user py.test.

I know this is pretty much empirical. But that's it...

Upvotes: 1

Related Questions