Python pip 安装成功后导入错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32680081/
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 12:04:44  来源:igfitidea点击:

ImportError after successful pip installation

pythonpip

提问by cel

I have successfully installed a library with pip install <library-name>. But when I try to import it, python raises ImportError: No module named <library-name>. Why do I get this error and how can I use the installed library?

我已经成功安装了一个带有pip install <library-name>. 但是当我尝试导入它时,python 引发ImportError: No module named <library-name>. 为什么会出现此错误以及如何使用已安装的库?

采纳答案by cel

TL;DR: There are often multiple versions of python interpreters and pip versions present. Using python -m pip install <library-name>instead of pip install <library-name>will ensure that the library gets installed into the default python interpreter.

TL;DR:通常存在多个版本的 python 解释器和 pip 版本。使用python -m pip install <library-name>而不是pip install <library-name>将确保库安装到默认的 python 解释器中。

Please also note:From my personal experience I would advice against using sudo pip installto install packages into system's default python interpreter. This can lead to a various messy issues. Whenever you are tempted to call pipwith sudo, please check first if a virtualenvis not a better option for you.

另请注意:根据我的个人经验,我建议不要使用sudo pip install将软件包安装到系统的默认 python 解释器中。这可能会导致各种混乱的问题。每当您想用 打电话pipsudo,请先检查virtualenv是否不是更好的选择。



Most modern systems ship multiple python interpreters. Each interpreter maintains its own set of installed packages. When installing new packages, it is important to understand into which interpreter those packages are actually installed.

大多数现代系统都提供多个 python 解释器。每个解释器都维护自己的一组已安装包。安装新包时,了解这些包实际安装在哪个解释器中很重要。

On unix systems the shell can be used to understand what exactly is happening.

在 unix 系统上,shell 可用于了解究竟发生了什么。

Typing which -a pythonshows all interpreters that in your PATH. The first line corresponds to the interpreter that is used when you run pythonfrom the command line.

键入会which -a python显示PATH. 第一行对应于从命令行运行时使用的解释器python

/private/tmp/py32/bin/python
/usr/local/bin/python
/usr/bin/python

Each pip version belongs to exactly one interpreter. which -a pipshows all pip versions. Again the first line is what will be called when you type pipin your shell.

每个 pip 版本都只属于一个解释器。which -a pip显示所有 pip 版本。同样,第一行是您pip在 shell 中键入时将调用的内容。

/usr/local/bin/pip
/usr/bin/python

Note that in this case pythonbelongs to the interpreter installed in /private/tmp/py32/, but pipinstalls into the interpreter /usr/local/bin. After a successful install of a library, you will not be able to import it in your default python interpreter.

请注意,在这种情况下,python属于安装在解释器中/private/tmp/py32/,但pip安装到解释器中/usr/local/bin。成功安装库后,您将无法在默认的 Python 解释器中导入它。

So how do you import the installed library?

那么如何导入已安装的库呢?

Your first option is to start the desired interpreter with its full path. So if you type /usr/local/bin/python, you will be able to import the library.

您的第一个选择是使用其完整路径启动所需的解释器。因此,如果您键入/usr/local/bin/python,您将能够导入库。

The second - often preferred - option is to specifically invoke the right version of pip. To do so, you can use python -m pip install <library-name>instead of pip install <library-name>. This will call the pip version that belongs to your default python interpreter.

第二个 - 通常是首选 - 选项是专门调用正确版本的 pip。为此,您可以使用python -m pip install <library-name>代替pip install <library-name>。这将调用属于您的默认 python 解释器的 pip 版本。

回答by Luan Nguyen

A couple more points:

还有几点:

  1. Check to see if you're installing the library into the virtualenv that you want to use.
  2. There are some libraries whose package names are different from the library's name. You could take a look at their documentation online (google with keyword python <library>would usually bring up the information) to see if you're importing the package correctly.
  1. 检查您是否正在将库安装到要使用的 virtualenv 中。
  2. 有些库的包名与库名不同。您可以在线查看他们的文档(带有关键字的谷歌python <library>通常会显示信息)以查看您是否正确导入了包。