无法导入明确安装的模块

问题:无法导入明确安装的模块

安装机械化后,我似乎无法导入它。

我已经尝试从pip,easy_install和通过python setup.py install此仓库通过https://github.com/abielr/mechanize安装。所有这些都无济于事,因为每次我输入Python交互式代码时,都会得到:

Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mechanize
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named mechanize
>>> 

我之前运行的安装报告说它们已成功完成,因此我希望导入工作正常。是什么导致此错误?

After installing mechanize, I don’t seem to be able to import it.

I have tried installing from pip, easy_install, and via python setup.py install from this repo: https://github.com/abielr/mechanize. All of this to no avail, as each time I enter my Python interactive I get:

Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mechanize
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named mechanize
>>> 

The installations I ran previously reported that they had completed successfully, so I expect the import to work. What could be causing this error?


回答 0

就我而言,这是权限问题。该软件包以某种方式仅具有root rw权限安装,其他用户则无法rw!

In my case, it is permission problem. The package was somehow installed with root rw permission only, other user just cannot rw to it!


回答 1

我遇到了同样的问题:脚本import colorama被抛出并且导入错误,但是sudo pip install colorama告诉我“软件包已安装”。

我的解决方法:在不使用sudo:的情况下运行pip pip install colorama。然后pip同意需要安装它,然后安装它,然后我的脚本运行了。

我的环境是32位Ubuntu 14.04;我想我在激活virtualenv之前和之后都看到了这个。

更新:甚至更好,使用python -m pip install <package>。这样做的好处是,由于您正在执行要在其中安装软件包的特定版本的python,因此pip会明确将软件包安装到“正确的” python中。同样,在这种情况下,请不要使用sudo …然后您将软件包放在正确的位置,但可能具有(不需要的)root权限。

I had the same problem: script with import colorama was throwing and ImportError, but sudo pip install colorama was telling me “package already installed”.

My fix: run pip without sudo: pip install colorama. Then pip agreed it needed to be installed, installed it, and my script ran.

My environment is Ubuntu 14.04 32-bit; I think I saw this before and after I activated my virtualenv.

UPDATE: even better, use python -m pip install <package>. The benefit of this is, since you are executing the specific version of python that you want the package in, pip will unequivocally install the package in to the “right” python. Again, don’t use sudo in this case… then you get the package in the right place, but possibly with (unwanted) root permissions.


回答 2

这是python路径问题。

就我而言,我在以下位置安装了python:

/Library/Frameworks/Python.framework/Versions/2.6/bin/python,

并且python2.6中没有site-packages目录。

我通过pip安装的包(SOAPpy)位于

/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/

而且site-package不在python路径中,我所做的就是将site-packages永久添加到PYTHONPATH中。

  1. 打开终端
  2. 输入open .bash_profile
  3. 在弹出的文本文件中,在最后添加以下行:

    导出PYTHONPATH = $ PYTHONPATH:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/

  4. 保存文件,重新启动终端,然后完成

It’s the python path problem.

In my case, I have python installed in:

/Library/Frameworks/Python.framework/Versions/2.6/bin/python,

and there is no site-packages directory within the python2.6.

The package(SOAPpy) I installed by pip is located

/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/

And site-package is not in the python path, all I did is add site-packages to PYTHONPATH permanently.

  1. Open up Terminal
  2. Type open .bash_profile
  3. In the text file that pops up, add this line at the end:

    export PYTHONPATH=$PYTHONPATH:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/

  4. Save the file, restart the Terminal, and you’re done

回答 3

实际上,Python导入机制可以运行:

  1. 您的PYTHONPATH错误,
  2. 您的库未安装在您认为的位置
  3. 您有另一个同名的库掩盖了这个库

The Python import mechanism works, really, so, either:

  1. Your PYTHONPATH is wrong,
  2. Your library is not installed where you think it is
  3. You have another library with the same name masking this one

回答 4

我一直把头撞在显示器上,直到一位年轻的实习生告诉我,秘密是在模块目录中 “ python setup.py install”

由于某些原因,从那里运行安装程序使其可以正常工作。

要明确的是,如果模块的名称是“ foo”:

[burnc7 (2016-06-21 15:28:49) git]# ls -l
total 1
drwxr-xr-x 7 root root  118 Jun 21 15:22 foo
[burnc7 (2016-06-21 15:28:51) git]# cd foo
[burnc7 (2016-06-21 15:28:53) foo]# ls -l
total 2
drwxr-xr-x 2 root root   93 Jun 21 15:23 foo
-rw-r--r-- 1 root root  416 May 31 12:26 setup.py
[burnc7 (2016-06-21 15:28:54) foo]# python setup.py install
<--snip-->

如果尝试通过调出路径从任何其他目录运行setup.py,最终将导致安装失败。

不起作用:

python /root/foo/setup.py install

运作方式:

cd /root/foo
python setup.py install

I have been banging my head against my monitor on this until a young-hip intern told me the secret is to “python setup.py install” inside the module directory.

For some reason, running the setup from there makes it just work.

To be clear, if your module’s name is “foo”:

[burnc7 (2016-06-21 15:28:49) git]# ls -l
total 1
drwxr-xr-x 7 root root  118 Jun 21 15:22 foo
[burnc7 (2016-06-21 15:28:51) git]# cd foo
[burnc7 (2016-06-21 15:28:53) foo]# ls -l
total 2
drwxr-xr-x 2 root root   93 Jun 21 15:23 foo
-rw-r--r-- 1 root root  416 May 31 12:26 setup.py
[burnc7 (2016-06-21 15:28:54) foo]# python setup.py install
<--snip-->

If you try to run setup.py from any other directory by calling out its path, you end up with a borked install.

DOES NOT WORK:

python /root/foo/setup.py install

DOES WORK:

cd /root/foo
python setup.py install

回答 5

我能够通过组合方法解决此问题。首先,我遵循Chris的建议,打开了一个命令行,然后键入“ pip show packagename”。这提供了已安装软件包的位置。

接下来,我打开python并键入“ import sys”,然后键入“ sys.path”以显示我的python在哪里搜索我导入的任何软件包。las,第一步中显示的位置不在列表中。

最后一步,我输入了“ sys.path.append(’package_location_seen_in_step_1’)。您可以选择重复第二步,以查看该位置现在在列表中。

测试步骤,尝试再次导入包…。

不足之处?它是临时的,您需要每次将其添加到列表中。

I was able to correct this issue with a combined approach. First, I followed Chris’ advice, opened a command line and typed ‘pip show packagename’ This provided the location of the installed package.

Next, I opened python and typed ‘import sys’, then ‘sys.path’ to show where my python searches for any packages I import. Alas, the location shown in the first step was NOT in the list.

Final step, I typed ‘sys.path.append(‘package_location_seen_in_step_1’). You optionally can repeat step two to see the location is now in the list.

Test step, try to import the package again… it works.

The downside? It is temporary, and you need to add it to the list each time.


回答 6

我在尝试使用通过安装的密钥环时遇到了此问题sudo pip install keyring。如其他答案所述,在我看来,这是一个权限问题。

对我有用的是:

  1. 卸载密钥环:
    • sudo pip uninstall keyring
  2. 我使用了sudo的-H选项并重新安装了密钥环:
    • sudo -H pip install keyring

希望这可以帮助。

I encountered this while trying to use keyring which I installed via sudo pip install keyring. As mentioned in the other answers, it’s a permissions issue in my case.

What worked for me:

  1. Uninstalled keyring:
    • sudo pip uninstall keyring
  2. I used sudo’s -H option and reinstalled keyring:
    • sudo -H pip install keyring

Hope this helps.


回答 7

我是python的新手。我通过更改项目解释器路径来解决此问题。
File -> Settings -> Project -> Project Interpreter

I am new to python. I fixed this issue by changing the project interpreter path.
File -> Settings -> Project -> Project Interpreter


回答 8

我的PYTHONPATH无法正常工作。我意识到添加export解决了这个问题:

(工作)

export PYTHONPATH=$PYTHONPATH:~/test/site-packages

(不工作)

PYTHONPATH=$PYTHONPATH:~/test/site-packages

I couldn’t get my PYTHONPATH to work properly. I realized adding export fixed the issue:

(did work)

export PYTHONPATH=$PYTHONPATH:~/test/site-packages

vs.

(did not work)

PYTHONPATH=$PYTHONPATH:~/test/site-packages

回答 9

就我而言,我已经运行了pip install Django==1.11,并且不会从python解释器导入。

浏览pip的命令后发现pip show

> pip show Django
Name: Django
Version: 1.11
...
Location: /usr/lib/python3.4/site-packages
...

注意该位置显示为“ 3.4”。我发现python-command链接到python2.7

/usr/bin> ls -l python
lrwxrwxrwx 1 root root 9 Mar 14 15:48 python -> python2.7

在它旁边,我找到了一个链接,python3所以我使用了它。您也可以将链接更改为python3.4。那也可以解决。

In my case I had run pip install Django==1.11 and it would not import from the python interpreter.

Browsing through pip’s commands I found pip show which looked like this:

> pip show Django
Name: Django
Version: 1.11
...
Location: /usr/lib/python3.4/site-packages
...

Notice the location says ‘3.4’. I found that the python-command was linked to python2.7

/usr/bin> ls -l python
lrwxrwxrwx 1 root root 9 Mar 14 15:48 python -> python2.7

Right next to that I found a link called python3 so I used that. You could also change the link to python3.4. That would fix it, too.


回答 10

就我而言,这是一个模块缺少init .py文件的问题,我想在Python 2.7环境中导入该文件。

Python 3.3+具有隐式命名空间包,允许它创建没有init .py文件的包。

In my case it was a problem with a missing init.py file in the module, that I wanted to import in a Python 2.7 environment.

Python 3.3+ has Implicit Namespace Packages that allow it to create a packages without an init.py file.


回答 11

如果提到的其他答案对您不起作用,请尝试删除您的点子缓存并重新安装软件包。我的机器运行Ubuntu14.04,它位于下~/.cache/pip。删除此文件夹对我有用。

If the other answers mentioned do not work for you, try deleting your pip cache and reinstalling the package. My machine runs Ubuntu14.04 and it was located under ~/.cache/pip. Deleting this folder did the trick for me.


回答 12

另外,请确保您不要pip3与混淆pip。我发现安装的软件包pip无法正常工作,python3反之亦然。

Also, make sure that you do not confuse pip3 with pip. What I found was that package installed with pip was not working with python3 and vice-versa.


回答 13

通过easy_install或安装时pip,是否成功完成?全部输出是多少?您正在使用哪个python安装?sudo如果要将模块安装到系统目录中,则可能需要在安装命令前使用(如果使用的是系统python安装)。您的问题中没有很多有用的信息,但是一些可能会有所帮助的工具包括:

  • echo $PYTHONPATH和/或echo $PATH:导入模块时,Python在这些环境变量之一(目录列表,带:分隔符)中搜索所需的模块。导入问题通常是由于这些列表中缺少正确的目录

  • which pythonwhich pipwhich easy_install:这些将告诉您每个可执行文件的位置。可能会有所帮助。

  • 使用virtualenv,就像@JesseBriggs建议的那样。它可以很好pip地帮助您隔离和管理单独的Python项目的模块和环境。

When you install via easy_install or pip, is it completing successfully? What is the full output? Which python installation are you using? You may need to use sudo before your installation command, if you are installing modules to a system directory (if you are using the system python installation, perhaps). There’s not a lot of useful information in your question to go off of, but some tools that will probably help include:

  • echo $PYTHONPATH and/or echo $PATH: when importing modules, Python searches one of these environment variables (lists of directories, : delimited) for the module you want. Importing problems are often due to the right directory being absent from these lists

  • which python, which pip, or which easy_install: these will tell you the location of each executable. It may help to know.

  • Use virtualenv, like @JesseBriggs suggests. It works very well with pip to help you isolate and manage the modules and environment for separate Python projects.


回答 14

我遇到了这个确切的问题,但以上答案均无济于事。这使我发疯,直到我从父项目中导入后发现sys.path有所不同。原来,我已经使用importlib编写了一个小功能,以便导入不在项目层次结构中的文件。坏主意:我忘记了这样做。更糟糕的是,导入过程与sys.path混为一谈,并以这种方式保留了下来。非常糟糕的主意。

解决方案是停止此操作,只需将需要导入的文件放入项目中即可。另一种方法是将文件放入其自己的项目中,因为需要不时地对其进行重建,并且该重建与主项目的重建可能会也可能不会同时进行。

I had this exact problem, but none of the answers above worked. It drove me crazy until I noticed that sys.path was different after I had imported from the parent project. It turned out that I had used importlib to write a little function in order to import a file not in the project hierarchy. Bad idea: I forgot that I had done this. Even worse, the import process mucked with the sys.path–and left it that way. Very bad idea.

The solution was to stop that, and simply put the file I needed to import into the project. Another approach would have been to put the file into its own project, as it needs to be rebuilt from time to time, and the rebuild may or may not coincide with the rebuild of the main project.


回答 15

我在系统上安装了2.7和3.5时遇到了这个问题,试图用Python-Telegram-Bot测试电报机器人。

使用pip和pip3(使用sudo或不使用sip)安装后,我无法正常工作。我总是得到:

Traceback (most recent call last):
  File "telegram.py", line 2, in <module>
    from telegram.ext import Updater
  File "$USER/telegram.py", line 2, in <module>
    from telegram.ext import Updater
ImportError: No module named 'telegram.ext'; 'telegram' is not a package

正确阅读错误消息告诉我python正在当前目录中查找telegram.py。正确的是,我那里有一个名为telegram.py的脚本,当我调用时,该脚本由python加载import

结论,请确保package.py在尝试导入时当前目录中没有任何内容。(并仔细阅读错误消息)。

I had this problem with 2.7 and 3.5 installed on my system trying to test a telegram bot with Python-Telegram-Bot.

I couldn’t get it to work after installing with pip and pip3, with sudo or without. I always got:

Traceback (most recent call last):
  File "telegram.py", line 2, in <module>
    from telegram.ext import Updater
  File "$USER/telegram.py", line 2, in <module>
    from telegram.ext import Updater
ImportError: No module named 'telegram.ext'; 'telegram' is not a package

Reading the error message correctly tells me that python is looking in the current directory for a telegram.py. And right, I had a script lying there called telegram.py and this was loaded by python when I called import.

Conclusion, make sure you don’t have any package.py in your current working dir when trying to import. (And read error message thoroughly).


回答 16

我有类似的问题(在Windows上),根本原因是ANTIVIRUS软件!它具有“自动包含”功能,该功能将运行过程与某种虚拟机进行包装。症状是:pip install somemodule在一个cmd-line窗口中工作正常,并且import somemodule在从另一个进程执行时出现错误而失败

ModuleNotFoundError: No module named 'somemodule'

我希望这可以节省一些时间:)

I had similar problem (on Windows) and the root cause in my case was ANTIVIRUS software! It has “Auto-Containment” feature, that wraps running process with some kind of a virtual machine. Symptoms are: pip install somemodule works fine in one cmd-line window and import somemodule fails when executed from another process with the error

ModuleNotFoundError: No module named 'somemodule'

I hope it will save some time to somebody :)


回答 17

也许有点题外话,但我遇到了一些问题import PyYAML。指出您需要import yaml。(猜测这是经典的rtfm …)

Maybe a bit off-topic, but i had issues to import PyYAML. Points out that you need to import yaml. (guess it’s a classical rtfm…)


回答 18

我在使用Django时遇到类似的问题。就我而言,我可以从Django Shell导入模块,但不能从导入模块的.py导入。
问题是我从安装模块的另一个virtualenv运行Django服务器(因此,执行.py)。

相反,shell实例是在正确的virtualenv中运行的。因此,它为什么起作用。

I had a similar problem using Django. In my case, I could import the module from the Django shell, but not from a .py which imported the module.
The problem was that I was running the Django server (therefore, executing the .py) from a different virtualenv from which the module had been installed.

Instead, the shell instance was being run in the correct virtualenv. Hence, why it worked.


回答 19

这有效!!!

当模块安装到旧版本的python或另一个目录时,通常会发生这种情况,因为解决方案很简单,所以不用担心。-从安装模块的目录中导入模块。您可以先导入python sys模块,然后从模块安装路径导入

import sys
sys.path.append("directory in which module is installed")

import <module_name>

This Works!!!

This often happens when module is installed to an older version of python or another directory, no worries as solution is simple. – import module from directory in which module is installed. You can do this by first importing the python sys module then importing from the path in which the module is installed

import sys
sys.path.append("directory in which module is installed")

import <module_name>

回答 20

解决方案中已经涵盖了大多数可能的情况,只是分享我的情况,我碰巧在一个环境(例如X)中安装了一个软件包,而在另一个环境(例如)中导入了该软件包Y。因此,请始终确保从安装软件包的环境中导入软件包。

Most of the possible cases have been already covered in solutions, just sharing my case, it happened to me that I installed a package in one environment (e.g. X) and I was importing the package in another environment (e.g. Y). So, always make sure that you’re importing the package from the environment in which you installed the package.


回答 21

对我有用的是:

python -m pip install -user {package name}

该命令不需要sudo。这已在OSX Mojave上进行了测试。

Something that worked for me was:

python -m pip install -user {package name}

The command does not require sudo. This was tested on OSX Mojave.


回答 22

就我而言,我也必须为超级用户安装模块。

sudo su
pip install <module>

显然,在某些情况下,superuse无法访问普通用户文件。

In my case I had to also install the module(s) for the superuser, too.

sudo su
pip install <module>

Apparently the superuse cannot access the normal users files under certain circumstances.


回答 23

对我来说,这是确保模块的版本与我使用的Python版本保持一致。.我在装有Python 3.6的盒子上构建了映像,然后注入恰好安装了3.7的Docker映像中,然后敲了敲我的头当Python告诉我模块未安装时…

36m 适用于Python 3.6 bsonnumpy.cpython-36m-x86_64-linux-gnu.so

37m 对于Python 3.7 bsonnumpy.cpython-37m-x86_64-linux-gnu.so

For me it was ensuring the version of the module aligned with the version of Python I was using.. I built the image on a box with Python 3.6 and then injected into a Docker image that happened to have 3.7 installed, and then banging my head when Python was telling me the module wasn’t installed…

36m for Python 3.6 bsonnumpy.cpython-36m-x86_64-linux-gnu.so

37m for Python 3.7 bsonnumpy.cpython-37m-x86_64-linux-gnu.so


回答 24

我知道这是一篇非常老的文章,但是对我来说,我遇到了安装32位python和64位python的问题。一旦我卸载了32位python,一切都会正常进行。

I know this is a super old post but for me, I had an issue with a 32 bit python and 64 bit python installed. Once I uninstalled the 32 bit python, everything worked as it should.


回答 25

我已经解决了一个问题,即相同的库在一个项目(A)中运行良好,但是将这些相同的库导入另一个项目(B)却导致错误。我在Windows OS上使用Pycharm作为IDE。因此,在尝试了许多潜在的解决方案并且未能解决问题之后,我做了这两件事(删除了“ Venv”文件夹,并重新配置了解释器):

1-在项目(B)中,位于外部库/中有一个名为(“ venv”)的文件夹。我删除了那个文件夹。

2步1(删除“ venv”文件夹)在Python解释器配置中导致错误,并且屏幕顶部显示一条消息,提示“为项目选择了无效的python解释器”和“配置python解释器”,选择该链接并打开一个新窗口。在“项目解释器”下拉列表中,有一条红色的线显示了先前的无效解释器。现在,打开此列表并选择Python解释器(在我的情况下是Python 3.7)。按下底部的“应用”和“确定”,您就可以开始了。

注意:这可能是我的项目(B)的虚拟环境无法识别已经安装且正在运行的库的问题。

I have solved my issue that same libraries were working fine in one project(A) but importing those same libraries in another project(B) caused error. I am using Pycharm as IDE at Windows OS. So, after trying many potential solutions and failing to solve the issue, I did these two things (deleted “Venv” folder, and reconfigured interpreter):

1-In project(B), there was a folder named(“venv”), located in External Libraries/. I deleted that folder.

2-Step 1 (deleting “venv” folder) causes error in Python Interpreter Configuration, and there is a message shown at top of screen saying “Invalid python interpreter selected for the project” and “configure python interpreter”, select that link and it opens a new window. There in “Project Interpreter” drop-down list, there is a Red colored line showing previous invalid interpreter. Now, Open this list and select the Python Interpreter(in my case, it is Python 3.7). Press “Apply” and “OK” at the bottom and you are good to go.

Note: It was potentially the issue where Virtual Environment of my Project(B) was not recognizing the already installed and working libraries.


回答 26

如果您使用的是虚拟环境,请使用pipenv install <module name>而不是pip install <module name>

为我工作。

If you are using a virtual environment use pipenv install <module name> instead of pip install <module name>

Worked for me.


回答 27

也有这个问题。.该软件包安装在Python 3.8.0上,但是VS Code使用较旧的版本(3.4)运行我的脚本

在终端中修复:

py .py

确保您在正确的Python版本上安装了软件包

Had this problem too.. the package was installed on Python 3.8.0 but VS Code was running my script using an older version (3.4)

fix in terminal:

py .py

Make sure you’re installing the package on the right Python Version


回答 28

就像今天朋友为我所做的那样,这对我有所帮助(我正在使用Windows):

按“设置”->“项目”->“项目解释器”。在右边的窗口中,左边有一行标题为“ Project Interpreter”的行。单击此行,它将打开其他几行。

现在按“显示全部”行。将会打开一个窗口。在此窗口中,按右上角的小“ +”号。

将打开一个新窗口。在左侧,有4个选项卡,按最上面的一个,即“ Virtualenv Environment”。现在,在右侧窗口中,标记“现有环境”选项。“翻译”行将变得清晰可见。按下该行右侧的“ …”按钮。

现在,将打开一个浏览窗口。浏览到在其中安装Python本身的目录。不是拥有PyCharm的人。当您到达那里时,选择“ python.exe”文件,然后按OK(该窗口将消失)。

再次按OK(此窗口也将消失)。

现在,在此窗口中,确保标记了您创建的新行,然后再次按OK。

现在,所有已安装的软件包都应该在项目解释器中可见,并由您的程序读取。

As a friend did for me today, here is what helped me (I am using Windows):

Press ‘Setting’ -> ‘Project’ -> ‘Project Interpreter’. Here in the window on the right, there is a line with the title ‘Project Interpreter’ on it’s left. Click this line and it will open several additional lines.

Now press the ‘Show All’ line. A window will open. In this window press the small ‘+’ sign in the upper right corner.

A new window will open. On the left there are 4 tabs, press the most upper one, which says ‘Virtualenv Environment’. Now, in the window on the right, mark the ‘Existing Environment’ option. The ‘Interpreter’ line will become well visible. Press the ‘…’ button on the right of the line.

Now, a browsing window will open. Browse to the directory that you installed Python itself in. Not the one with PyCharm. When you get there, choose the ‘python.exe’ file and press OK (the window will disappear).

Press OK again (this window will disappear too).

Now in this window make sure the new line you created is marked, and press OK again.

Now, all the installed packages should be visible in the project interpreter, and are read by your program.


回答 29

在这个线程中没有提到对我有用的最简单的解决方案:

我安装了多个版本的Python,但尝试使用Python3.7-所以我不得不使用:

sudo pip3.7 install <package>

Simplest solution that worked for me that I don’t see mentioned in this thread:

I have multiple versions of Python installed but was trying to use Python3.7 — so I had to use:

sudo pip3.7 install <package>