import error: 'No module named' *does* exist [duplicate]
Asked Answered
J

13

82

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:

  1. Rebuilding virtualenv

  2. Setting PYTHONPATH

  3. Using absolute paths in code

  4. 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.

Jett answered 1/5, 2014 at 22:44 Comment(7)
This is not a duplicate, at least since the link you provide does not duplicate this problem. There, the problem was one of: - Some weirdness with Windows scp and linux; - improperly set PYTHONPATH; - failure to use __init__.py consistently. None of those is the problem here. In my case, the setup.py did not copy files recursively so imports were missing in virtualenv that were present in dev-env. Fix was to add to MANIFEST.in, I believe. So, markedly different and not a duplicate.Jett
@EdChum, this is not a duplicate. It has the same error message, but the cause is different and the solution is very different from the item you cite as a duplicate. Can you reconsider this? I have not found a way to contact you by stackoverflow, email, or twitter.Jett
Apologies, I have reopened this post, it looked the same but you can always message anyone who closed a question using the @whateverBainite
Nice question. Shame about the answer.Principe
@avp it’s the same question but a very different answer. It’s not a duplicate in the important sense that it points at s different way of fixing the problem that is not captured in the other post.Jett
@avp More specifically, the item you reference has a problem with Windows scp and CR-LF on a linux installation. The question here is about getting only the files that you ask for in setup.py + MANIFEST.in. It's a totally different solution, notwithstanding any other similarities in description.Jett
Third time's a charm. Editors tried to close this question in 2015 and 2019 but I showed how the cited questions that this supposedly duplicates are not the same. This question has a wholly different answer not found elsewhere. Not going to fight this again.Jett
H
67

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
Hamrah answered 1/5, 2014 at 23:57 Comment(4)
Okay, so the problem is that in my virtualenv, that path does not exist. When I tree my virtualenv, I can see that the views directory is missing. And that problem starts in the setup.py I am using that names the directories to install in the packages section but does not traverse down directories. Okay, I am on my way to figuring this out. For a start, I need a better setup.py.Jett
How do you print sys.path?Venola
in python2 it is with the print expression: print sys.path and in python3 with the print function: print(sys.path). in both you need import sys before that.Hamrah
I solve adding this in settings.py of my project: `` import sys sys.path.insert(1, '{project_path}') `` Ty @M-T-AFrosted
K
73

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

Kurrajong answered 3/9, 2015 at 6:44 Comment(5)
This solved it for me in TravisCI as well--you would've thought this wouldn't be required. Thank you!Wini
Where do we need to execute this?Sikh
@ParthapratimNeog The export is expected to be run in a terminal window prior to executing your script. I also extended my answer with a one-liner solution should you not want to add an environment variable.Kurrajong
I believe it's better to add the absolute path than . because . will not follow if you change the directory.Gio
A bit better is to export PYTHONPATH=$(pwd):$PYTHONPATHProletarian
H
67

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
Hamrah answered 1/5, 2014 at 23:57 Comment(4)
Okay, so the problem is that in my virtualenv, that path does not exist. When I tree my virtualenv, I can see that the views directory is missing. And that problem starts in the setup.py I am using that names the directories to install in the packages section but does not traverse down directories. Okay, I am on my way to figuring this out. For a start, I need a better setup.py.Jett
How do you print sys.path?Venola
in python2 it is with the print expression: print sys.path and in python3 with the print function: print(sys.path). in both you need import sys before that.Hamrah
I solve adding this in settings.py of my project: `` import sys sys.path.insert(1, '{project_path}') `` Ty @M-T-AFrosted
R
11

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.

Redolent answered 2/10, 2017 at 11:47 Comment(0)
H
5

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.

Hurry answered 21/12, 2016 at 11:1 Comment(0)
J
5

The PYTHONPATH is not set properly. Export it using export PYTHONPATH=$PYTHONPATH:/path/to/your/modules .

Jaleesa answered 5/2, 2019 at 3:35 Comment(0)
P
4

They are several ways to run python script:

  • run by double click on file.py (it opens the python command line)
  • run your file.py from the cmd prompt (cmd) (drag/drop your file on it for instance)
  • run your file.py in your IDE (eg. pyscripter or Pycharm)

Each of these ways can run a different version of python (¤)


Check which python version is run by cmd: Type in cmd:

python --version 

Check which python version is run when clicking on .py:

option 1:

create a test.py containing this:

import sys print (sys.version)
input("exit")

Option 2:

type in cmd:

assoc .py
ftype Python.File

Check the path and if the module (ex: win32clipboard) is recognized in the cmd:

create a test.py containing this:

python
import sys
sys.executable
sys.path
import win32clipboard
win32clipboard.__file__

Check the path and if module is recognized in the .py

create a test.py containing this:

import sys
print(sys.executable)
print(sys.path)
import win32clipboard
print(win32clipboard.__file__)

If the version in cmd is ok but not in .py it's because the default program associated with .py isn't the right one. Change python version for .py

To change the python version associated with cmd:

Control Panel\All Control Panel Items\System\Advanced system setting\Environnement variable In SYSTEM variable set the path variable to you python version (the path are separated by ;: cmd use the FIRST path eg: C:\path\to\Python27;C:\path\to\Python35 → cmd will use python27)

To change the python version associated with .py extension:

Run cmd as admin:

Write: ftype Python.File="C:\Python35\python.exe" "%1" %* It will set the last python version (eg. python3.6). If your last version is 3.6 but you want 3.5 just add some xxx in your folder (xxxpython36) so it will take the last recognized version which is python3.5 (after the cmd remove the xxx).

Other:

"No modul error" could also come from a syntax error btw python et 3 (eg. missing parenthesis for print function...)

¤ Thus each of them has it's own pip version

Predominant answered 30/11, 2016 at 8:42 Comment(1)
Ooh, I cannot stress the importance of checking python --version enough. It is far too easy to be running Python 2.7 by accident, especially if you develop on multiple different computers with different virtualboxes :) Obviously, run python3 <spam> instead of python <spam> when your project uses Python 3 and the system uses Python 2.7 for the python alias.Heuser
A
2

I had the same issue. I solved it by running the command in a different python version. I tried python3 filename.py. Earlier i was using Python 2.7.

Another possibility is that the file from which something is imported may contain BOM (Byte Order Mark). It can be solved by opening the file in some editor which supports multiple encoding like VSCode (Notepad++) and saving in a different encoding statndard like ANSI, UTF-8(without BOM).

Appendicitis answered 27/9, 2017 at 11:35 Comment(0)
G
2

If you have a script with the same name as your module in another directory, it will use that instead. For example:

module.py

module
|
|--module
|  |
|  |--__init__.py
|  |--module.py

This will make it so that the first module.py is being used, not the second one.

Gastropod answered 2/4, 2018 at 23:57 Comment(0)
T
1

In case this is of interest to anyone, I had the same problem when I was running Python in Cygwin, in my case it was complaning that pandas wasn't installed even though it was. The problem was that I had 2 installations of python - one in windows and another one in cygwin (using the cygwin installer) and although both were the same versions of Python, the Cygwin installation was confused about where Pandas was installed. When i uninstalled cygwin's Python and pointed Cygwin at the windows installation everything was fine

Telex answered 2/11, 2017 at 3:2 Comment(0)
O
1

I got this when I didn't type things right. I had

__init.py__ 

instead of

__init__.py
Onassis answered 15/5, 2019 at 17:59 Comment(0)
F
0

I've had this problem too, I had just forgotten to type workon myproject in the terminal before executing my program.

Findley answered 25/1, 2016 at 10:1 Comment(0)
V
0

My issue is that I had two directories in my sys.path / PYTHONPATH - a project root directory, and an apps directory within Django.

I was trying to reach a Python module inside the apps directory, but by chance had an identically named folder in the root. Python was finding the directory in the root, and so not finding the directory in the apps directory.

(Duh. Although I'm partly writing this as I'll find myself back here in a few months/years..)

Vivi answered 23/6, 2022 at 13:14 Comment(0)
B
0

Make sure you aren't setting the wrong environment variable like me. It's PYTHONPATH, not PYTHON_PATH.

export PYTHONPATH=$PYTHONPATH:.
Bipartite answered 6/9, 2023 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.