import error: 'No module named' *does* exist
I am getting this stack trace when I start pyramid pserve:
% python $(which pserve) ../etc/development.ini
Traceback (most recent call last):
File "/home/hughdbrown/.local/bin/pserve", line 9, in <module>
load_entry_point('pyramid==1.5', 'console_scripts', 'pserve')()
File "/home/hughdbrown/.virtualenvs/ponder/local/lib/python2.7/site-packages/pyramid-1.5-py2.7.egg/pyramid/scripts/pserve.py", line 51, in main
return command.run()
File "/home/hughdbrown/.virtualenvs/ponder/local/lib/python2.7/site-packages/pyramid-1.5-py2.7.egg/pyramid/scripts/pserve.py", line 316, in run
global_conf=vars)
File "/home/hughdbrown/.virtualenvs/ponder/local/lib/python2.7/site-packages/pyramid-1.5-py2.7.egg/pyramid/scripts/pserve.py", line 340, in loadapp
return loadapp(app_spec, name=name, relative_to=relative_to, **kw)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/loadwsgi.py", line 247, in loadapp
return loadobj(APP, uri, name=name, **kw)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/loadwsgi.py", line 271, in loadobj
global_conf=global_conf)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/loadwsgi.py", line 296, in loadcontext
global_conf=global_conf)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/loadwsgi.py", line 320, in _loadconfig
return loader.get_context(object_type, name, global_conf)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/loadwsgi.py", line 454, in get_context
section)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/loadwsgi.py", line 476, in _context_from_use
object_type, name=use, global_conf=global_conf)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/loadwsgi.py", line 406, in get_context
global_conf=global_conf)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/loadwsgi.py", line 296, in loadcontext
global_conf=global_conf)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/loadwsgi.py", line 337, in _loadfunc
return loader.get_context(object_type, name, global_conf)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/loadwsgi.py", line 681, in get_context
obj = lookup_object(self.spec)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/util.py", line 68, in lookup_object
module = __import__(parts)
File "/home/hughdbrown/.virtualenvs/ponder/local/lib/python2.7/site-packages/ponder-0.0.40-py2.7.egg/ponder/server/__init__.py", line 10, in <module>
from ponder.server.views import Endpoints, route
ImportError: No module named views
This works fine from a python REPL:
% python
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ponder.server.views import Endpoints, route
>>>
and from a command line import:
% python -c "from ponder.server.views import Endpoints, route"
An abridged tree
output shows what I am working with:
% tree
├── __init__.py
├── ponder
│ ├── __init__.py
│ ├── server
│ │ ├── __init__.py
│ │ └── views
│ │ ├── environment_templates.py
│ │ ├── groups.py
│ │ ├── __init__.py
│ │ ├── instances.py
│ │ ├── tasks.py
│ │ └── users.py
My PYTHONPATH
is set to the root of this tree:
% echo $PYTHONPATH
/home/hughdbrown/workspace/ept/ponder/lib
I am running this in a virtualenv that uses python 2.7. I have had this working off and on today but I can't figure out where the problem is. For one thing, the __init__.py
seems to be okay with some imports that come just before:
from .database import get_db
from .config import parser
from .views import Endpoints, route
(I changed the last line to an absolute import. No luck.)
Things that I have tried:
Rebuilding virtualenv
Setting
PYTHONPATH
Using absolute paths in code
Looking for circular imports
I am open to further suggestions in how to debug this error.
So the mistake I made was to look only at the source tree. The problem was really in the runtime environment, in my virtualenv. And when I looked there, I found that the desired files were not being installed. The problem, at root, was the setup.py
.
I set the PYTHONPATH
to '.'
and that solved it for me.
export PYTHONPATH='.'
For a one-liner you could as easily do:
PYTHONPATH='.' your_python_script
These commands are expected to be run in a terminal
My usual trick is to simply print sys.path
in the actual context where the import problem happens. In your case it'd seem that the place for the print is in /home/hughdbrown/.local/bin/pserve
. Then check dirs & files in the places that path shows..
You do that by first having:
import sys
in python 3 with the print function:
print(sys.path)
or in python 2 with print expression:
print sys.path
I had the same problem, and I solved it by adding the following code to the top of the python file:
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
Number of repetitions of os.path.dirname
depends on where is the file located your project hierarchy. For instance, in my case the project root is three levels up.
The PYTHONPATH
is not set properly. Export it using export PYTHONPATH=$PYTHONPATH:/path/to/your/modules
.
I met the same problem, and I try the pdb.set_trace() before the error line.
My problem is the package name duplicate with the module name, like:
test
├── __init__.py
├── a
│ ├── __init__.py
│ └── test.py
└── b
└── __init__.py
and at file a/__init__.py
, using from test.b import xxx
will cause ImportError: No module named b
.