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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23417941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:57:12  来源:igfitidea点击:

import error: 'No module named' *does* exist

pythonpython-import

提问by hughdbrown

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 treeoutput 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 PYTHONPATHis set to the root of this tree:

MyPYTHONPATH设置为这棵树的根:

% 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__.pyseems to be okay with someimports that come just before:

我在使用 python 2.7 的 virtualenv 中运行它。我今天断断续续地进行了这项工作,但我无法弄清楚问题出在哪里。一方面,之前出现的一些进口__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

  3. Using absolute paths in code

  4. Looking for circular imports

  1. 重建 virtualenv

  2. 环境 PYTHONPATH

  3. 在代码中使用绝对路径

  4. 寻找循环进口

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.

所以我犯的错误是只看源代码树。问题确实出在运行时环境中,在我的 virtualenv 中。当我查看那里时,我发现没有安装所需的文件。问题的根源在于setup.py.

采纳答案by antont

My usual trick is to simply print sys.pathin 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..

我通常的技巧是sys.path在发生导入问题的实际上下文中简单地打印。在您的情况下,打印的位置似乎在/home/hughdbrown/.local/bin/pserve. 然后在路径显示的地方检查目录和文件..

You do that by first having:

你首先要做到这一点:

import sys

and in python 2 with print expression:

并在带有打印表达式的python 2中:

print sys.path

or in python 3 with the print function:

或在带有打印功能的python 3中:

print(sys.path)

回答by javabeangrinder

I set the PYTHONPATHto '.'and that solved it for me.

我设置PYTHONPATH'.'并为我解决了它。

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

这些命令应该在终端中运行

回答by Hawker65

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

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

回答by JinSnow

They are several ways to run python script:

它们有几种运行python脚本的方法:

  • 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)
  • 通过双击 file.py 运行(它打开 python 命令行)
  • 从 cmd 提示符 (cmd) 运行您的 file.py(例如,将您的文件拖放到它上面)
  • 在 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. Change python version for .py

如果 cmd 中的版本正常但 .py 中的版本不正确,那是因为与 .py 关联的默认程序不正确。更改 .py 的 python 版本

To change the python version associated with cmd:

要更改与 cmd 关联的 python 版本:

Control Panel\All Control Panel Items\System\Advanced system setting\Environnement variableIn SYSTEM variable set the pathvariable 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 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:

要更改与 .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). 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).

写入:ftype Python.File="C:\Python35\python.exe" "%1" %*它将设置最后一个python版本(例如python3.6)。如果您的上一个版本是 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...)

“无模块错误”也可能来自语法错误 btw python et 3(例如,缺少打印函数的括号......)

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

¤ 因此他们每个人都有自己的 pip 版本

回答by joest

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 xxxwill cause ImportError: No module named b.

在文件中a/__init__.py,使用from test.b import xxx将导致ImportError: No module named b.

回答by Mohammed Shareef C

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.

我遇到过同样的问题。我通过在不同的 python 版本中运行命令来解决它。我试过了python3 filename.py。早些时候我使用的是 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).

另一种可能性是从中导入某些内容的文件可能包含 BOM(字节顺序标记)。可以通过在某些支持多种编码(如 VSCode(Notepad++))的编辑器中打开文件并保存为不同的编码标准(如 ANSI、UTF-8(无 BOM))来解决此问题。

回答by Karin Lahyani

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.dirnamedepends on where is the file located your project hierarchy. For instance, in my case the project root is three levels up.

的重复次数os.path.dirname取决于文件位于项目层次结构的位置。例如,在我的情况下,项目根目录向上三层。

回答by S.N.

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

如果有人对此感兴趣,我在 Cygwin 中运行 Python 时遇到了同样的问题,在我的情况下,它抱怨没有安装 Pandas,即使它安装了。问题是我安装了 2 个 python - 一个在 windows 中,另一个在 cygwin 中(使用 cygwin 安装程序),尽管两者都是相同版本的 Python,但 Cygwin 安装对 Pandas 的安装位置感到困惑。当我卸载 cygwin 的 Python 并将 Cygwin 指向 Windows 安装时,一切都很好

回答by Arcsector

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 被使用,而不是第二个。

回答by SaGeSpidy

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

PYTHONPATH设置不正确。使用export PYTHONPATH=$PYTHONPATH:/path/to/your/modules.