简体   繁体   English

导入错误:“没有命名模块”*确实存在

[英]import error: 'No module named' *does* exist

I am getting this stack trace when I start pyramid pserve:当我开始金字塔 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 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输出显示了我正在使用的内容:

% 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:我的PYTHONPATH设置为这棵树的根:

% echo $PYTHONPATH
/home/hughdbrown/workspace/ept/ponder/lib

I am running this in a virtualenv that uses python 2.7.我在使用 python 2.7 的 virtualenv 中运行它。 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:一方面, __init__.py似乎可以接受之前的一些导入:

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设置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.问题实际上出在运行时环境中,在我的 virtualenv 中。 And when I looked there, I found that the desired files were not being installed.当我查看那里时,我发现没有安装所需的文件。 The problem, at root, was the setup.py .问题的根源在于setup.py

I set the PYTHONPATH to '.'我将PYTHONPATH设置为'.' 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.我通常的技巧是在发生导入问题的实际上下文中简单地打印sys.path In your case it'd seem that the place for the print is in /home/hughdbrown/.local/bin/pserve .在您的情况下,打印的位置似乎在/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:在带有打印功能的python 3中:

print(sys.path)

or in python 2 with print expression:或在带有打印表达式的python 2中:

print sys.path

I had the same problem, and I solved it by adding the following code to the top of the python file:我遇到了同样的问题,我通过在 python 文件的顶部添加以下代码来解决它:

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. os.path.dirname的重复次数取决于项目层次结构中文件的位置。 For instance, in my case the project root is three levels up.例如,在我的例子中,项目根目录是向上三层。

The PYTHONPATH is not set properly. PYTHONPATH设置不正确。 Export it using export PYTHONPATH=$PYTHONPATH:/path/to/your/modules .使用export PYTHONPATH=$PYTHONPATH:/path/to/your/modules导出它。

They are several ways to run python script:它们是运行 python 脚本的几种方法:

  • run by double click on file.py (it opens the python command line)通过双击 file.py 运行(它打开 python 命令行)
  • run your file.py from the cmd prompt (cmd) (drag/drop your file on it for instance)从 cmd 提示符 (cmd) 运行您的 file.py(例如,将文件拖放到上面)
  • run your file.py in your IDE (eg. pyscripter or Pycharm)在 IDE 中运行 file.py(例如 pyscripter 或 Pycharm)

Each of these ways can run a different version of python (¤)这些方式中的每一种都可以运行不同版本的 python (¤)


Check which python version is run by cmd: Type in cmd:检查 cmd 运行的是哪个 python 版本:输入 cmd:

python --version 

Check which python version is run when clicking on .py:单击 .py 时检查运行的是哪个 python 版本:

option 1 :选项 1

create a test.py containing this:创建一个包含以下内容的 test.py:

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

Option 2 :选项 2

type in cmd:输入cmd:

assoc .py
ftype Python.File

Check the path and if the module (ex: win32clipboard) is recognized in the cmd:检查路径,如果模块(例如:win32clipboard)在 cmd 中被识别:

create a test.py containing this:创建一个包含以下内容的 test.py:

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

Check the path and if module is recognized in the .py检查路径以及 .py 中是否识别出模块

create a test.py containing this:创建一个包含以下内容的 test.py:

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.如果 cmd 中的版本可以,但 .py 中的版本没有问题,那是因为与 .py 关联的默认程序不是正确的。 Change python version for .py更改 .py 的 python 版本

To change the python version associated with cmd:要更改与 cmd 关联的 python 版本:

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) Control Panel\All Control Panel Items\System\Advanced system setting\Environnement variable在 SYSTEM 变量中将path变量设置为您的 python 版本(路径由;分隔:cmd 使用第一个路径,例如:C:\path\to\Python27 ;C:\path\to\Python35 → cmd 将使用 python27)

To change the python version associated with .py extension:要更改与 .py 扩展名关联的 python 版本:

Run cmd as admin:以管理员身份运行 cmd:

Write: ftype Python.File="C:\Python35\python.exe" "%1" %* It will set the last python version (eg. python3.6).写: ftype Python.File="C:\Python35\python.exe" "%1" %*它将设置最后一个python版本(例如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).如果您的最后一个版本是 3.6,但您想要 3.5,只需在您的文件夹(xxxpython36)中添加一些 xxx,这样它将采用最后一个识别的版本,即 python3.5(在 cmd 删除 xxx 之后)。

Other:其他:

"No modul error" could also come from a syntax error btw python et 3 (eg. missing parenthesis for print function...) “没有模块错误”也可能来自 python et 3 的语法错误(例如,缺少打印函数的括号......)

¤ Thus each of them has it's own pip version ¤ 因此,他们每个人都有自己的 pip 版本

I met the same problem, and I try the pdb.set_trace() before the error line.我遇到了同样的问题,我在错误行之前尝试了 pdb.set_trace() 。

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 .在文件a/__init__.py中,使用from test.b import xxx将导致ImportError: No module named b

I had the same issue.我遇到过同样的问题。 I solved it by running the command in a different python version.我通过在不同的 python 版本中运行命令来解决它。 I tried python3 filename.py .我试过python3 filename.py Earlier i was using Python 2.7.早些时候我使用的是 Python 2.7。

Another possibility is that the file from which something is imported may contain BOM (Byte Order Mark).另一种可能性是导入某些内容的文件可能包含 BOM(字节顺序标记)。 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).可以通过在一些支持多种编码(如 VSCode(Notepad++))的编辑器中打开文件并保存为不同的编码标准(如 ANSI、UTF-8(无 BOM))来解决此问题。

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.这将使第一个 module.py 被使用,而不是第二个。

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.万一这对任何人都感兴趣,当我在 Cygwin 中运行 Python 时,我遇到了同样的问题,在我的情况下,它抱怨说即使安装了 pandas 也没有安装。 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.问题是我安装了 2 个 python - 一个在 windows 中,另一个在 cygwin 中(使用 cygwin 安装程序),虽然两者都是相同版本的 Python,但 Cygwin 安装对 Pandas 的安装位置感到困惑。 When i uninstalled cygwin's Python and pointed Cygwin at the windows installation everything was fine当我卸载 cygwin 的 Python 并将 Cygwin 指向 Windows 安装时,一切都很好

I got this when I didn't type things right.当我没有正确输入内容时,我得到了这个。 I had我有

__init.py__ 

instead of代替

__init__.py

我也遇到过这个问题,我只是忘记在执行我的程序之前在终端中输入 workon myproject 。

My issue is that I had two directories in my sys.path / PYTHONPATH - a project root directory, and an apps directory within Django.我的问题是我的 sys.path / PYTHONPATH 中有两个目录 - 一个项目根目录和 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 模块,但偶然在根目录中有一个同名文件夹。 Python was finding the directory in the root, and so not finding the directory in the apps directory. Python 在根目录中找到该目录,因此在应用程序目录中找不到该目录。

(Duh. Although I'm partly writing this as I'll find myself back here in a few months/years..) (呃。虽然我在写这篇文章的一部分,因为几个月/几年后我会发现自己回到这里......)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM