Python 导入请求模块不起作用

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

Importing requests module does not work

pythonpython-requests

提问by Moiz Raja

I have the requests module installed on my system.

我的系统上安装了请求模块。

pip install requests

Now I am trying to import requests in the rpel

现在我正在尝试在 rpel 中导入请求

import requests

It fails with the following error

它失败并出现以下错误

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named requests

采纳答案by abarnert

The most common reason for this is that you have two versions of Python 2.x, and the pipthat comes first in your PATHdoesn't go with the pythonthat comes first in your PATH.

造成这种情况的最常见的原因是,你有Python的2.X的两个版本,并且pip在首先出现在你PATH不与走python在首先出现在你的PATH

There are two ways that can happen.

有两种方式可能发生。

First, you may have, e.g., /usr/local/binbefore /usr/binon your PATH, but your /usr/localcopy of Python doesn't have pip. So, when you run pip install requests, that's /usr/bin/pip, which installs into /usr/lib/python2.7/site-packages. But when you run python, that's /usr/local/bin/python, which looks in /usr/local/lib/python2.7/site-packages.

首先,您可能有,例如,/usr/local/bin之前/usr/bin在您的PATH,但您/usr/local的 Python 副本没有pip. 所以,当你运行pip install requests,这就是/usr/bin/pip,它安装到/usr/lib/python2.7/site-packages。但是当你运行时python,就是/usr/local/bin/python,它在/usr/local/lib/python2.7/site-packages.

Second, even though your two Python 2.x's are in different locations, they may want to install pip(and other scripts and executables) to the same place. In particular, /usr/binis usually reserved for stuff that comes with the OS or its package manager, so if you use /usr/bin/python ez_setup.pyor /usr/bin/easy_install pipor many other common ways to install pip, it may end up in /usr/local/bin. In which case it will overwrite any earlier /usr/local/bin/pipthat went with your /usr/local/bin/python. At any rate, the result is basically the same: pipnow means /usr/local/bin/pip, but it still goes with your /usrPython, not your /usr/localPython, and installs into /usr/lib/python2.7/site-packages, which /usr/local/bin/pythoncan't see.

其次,即使您的两个 Python 2.x 位于不同的位置,它们也可能希望将pip(以及其他脚本和可执行文件)安装到同一位置。特别是,/usr/bin通常为操作系统或其包管理器附带的东西保留,所以如果您使用/usr/bin/python ez_setup.py/usr/bin/easy_install pip或许多其他常见的安装方式pip,它可能最终以/usr/local/bin. 在这种情况下,它将覆盖/usr/local/bin/pip与您的/usr/local/bin/python. 无论如何,结果基本相同:pip现在意味着/usr/local/bin/pip,但它仍然与您的/usrPython,而不是您的/usr/localPython一起使用,并安装到 中/usr/lib/python2.7/site-packages/usr/local/bin/python无法看到。

If your two versions are, e.g., 2.7 and 3.4, there's no problem; per PEP 394, either the 3.x versions of everything have to be run with python3and pip3and so on, or the 2.x versions have to be run with python2and pip2and so on.

如果你的两个版本是,比如2.7和3.4,那没问题;每PEP 394,无论是3.X版本的一切都必须得到运行python3pip3等,或在2.x版本必须与运行python2pip2等。

If your two versions are, e.g., 2.6 and 2.7, there is a problem, but you can easily work around it, because you should always have pip2.6and python2.6vs. pip2.7and python2.7. You canconfuse yourself with pythonand pip, but you don't have to.

如果您的两个版本是,例如 2.6 和 2.7,则存在问题,但您可以轻松解决它,因为您应该始终拥有pip2.6and python2.6vs. pip2.7and python2.7。您可以将自己与python和混淆pip,但您不必这样做。

If your two versions are both 2.7, however, there's no way to disambiguate (except by using complete absolute paths all the time, which no one wants to do).

但是,如果您的两个版本都是 2.7,则无法消除歧义(除非一直使用完整的绝对路径,但没人愿意这样做)。

So, why would anyone ever install two copies of Python 2 without knowing what they're doing?

那么,为什么有人会在不知道他们在做什么的情况下安装两个 Python 2 副本呢?



The most common reason is that they're on a Mac, which comes with Python 2.7, but they read a blog post that told them to install another Python and didn't explain how to know what they're doing. Apple's pre-installed Python is in /usr/bin but installs scripts and binaries to /usr/local/bin. The most popular alternative Python versions are the python.org installer and Homebrew, both of which install to /usr/local/binby default. The fact that Mac users tend to be less Unix-savvy than Linux or FreeBSD users probably doesn't help, but even without that, this is a perfect way to end up with thousands of people who have a pipand a pythonthat doesn't match, and no idea why.

最常见的原因是他们使用的是带有 Python 2.7 的 Mac,但他们阅读了一篇博客文章,该文章告诉他们安装另一个 Python,但没有解释如何知道他们在做什么。Apple 预装的 Python 位于 /usr/bin 中,但会将脚本和二进制文件安装到/usr/local/bin. 最流行的替代 Python 版本是 python.org 安装程序和 Homebrew,它们都/usr/local/bin默认安装。Mac 用户往往比 Linux 或 FreeBSD 用户更不精通 Unix 的事实可能无济于事,但即使没有这一点,这是一个完美的方式来结束成千上万的人的 apip和 apython不匹配,不知道为什么。

There used to be good reasons for almost all Mac Python users to installing a second Python. Until OS X 10.6, Apple's pre-installed Python versions tended to be badly out of date, and sometimes broken. If Apple's only giving you 2.4, it makes sense to install 2.6. And doing so is no problem, because python2.4and python2.6are easy to disambiguate. But Apple has been installing a working 2.7 for years now. There are sometimes good reasons why you need a different one (you need a bug fix in 2.7.7 but Apple gave you 2.7.5, you need a 32-bit build, you need an extra-batteries version like Enthought, you need to build py2appbundles out of it, …), but these reasons do not apply to most people anymore.

过去几乎所有 Mac Python 用户都有充分的理由安装第二个 Python。在 OS X 10.6 之前,Apple 预装的 Python 版本往往严重过时,有时甚至会损坏。如果苹果只给你 2.4,安装 2.6 是有意义的。这样做是没有问题的,因为python2.4python2.6很容易消除歧义。但苹果多年来一直在安装可运行的 2.7。有时你需要一个不同的版本有很好的理由(你需要在 2.7.7 中修复错误,但 Apple 给了你 2.7.5,你需要一个 32 位版本,你需要一个像 Enthought 这样的额外电池版本,你需要py2app从中构建捆绑包,...),但这些原因不再适用于大多数人。

In fact, many people on StackOverflow seem to have threeversions of Python 2.7. I'm not sure why this is so common, but they'll use Homebrew to install Python 2.7, and then use an installer from Python.org or Enthought, and now they've got three Python 2.7 versions all fighting over ownership of /usr/local/bin.

事实上,StackOverflow 上的很多人似乎都拥有Python 2.7 的三个版本。我不确定为什么这如此普遍,但他们会使用 Homebrew 来安装 Python 2.7,然后使用来自 Python.org 或 Enthought 的安装程序,现在他们有三个 Python 2.7 版本都在争夺 /用户/本地/bin。

So, how can you fix this?

那么,你怎么能解决这个问题?

  • If you can use Python 3.x, install that and just use pip3and python3(and ipython3and so on), and paths aren't an issue anymore.
  • If you don't need a second Python 2.7, get rid of the non-Apple one and just use Apple's.
  • Otherwise, do not ever use Apple's Python, do not install things for it, do not touch it; just leave it alone for Apple's own tools. If you use Homebrew, its Python should be higher on the PATH (make sure you've got /usr/local/binbefore /usr/bin), and it should let you pip install foowithout sudo, while Apple's won't, which makes it hard to accidentally screw up and install to the wrong one.
  • 如果您可以使用 Python 3.x,请安装它并使用pip3and python3ipython3等等),路径不再是问题。
  • 如果您不需要第二个 Python 2.7,请摆脱非 Apple 的,而只使用 Apple 的。
  • 否则,永远不要使用 Apple 的 Python,不要为它安装东西,不要碰它;只需将其留给 Apple 自己的工具即可。如果你使用 Homebrew,它的 Python 在 PATH 上应该更高(确保你之前有/usr/local/bin/usr/bin),它应该让你pip install foo没有sudo,而 Apple 不会,这使得很难不小心搞砸并安装到错误的.


I've also seen at least one Windows user who had both C:\Python27and D:\Python27, both on the PATH, with the C:one first, but piponly installed for D:. This seems to be far less common than the Mac confusion (probably because Windows doesn't come with Python, and there are no package managers, so the only way you're going to get any Python is by running an installer). And the solution is even simpler here: Windows doesn't need Python, so you can delete whichever one you want.

我还看到至少有一个 Windows 用户同时拥有C:\Python27D:\Python27,并且都在PATH,第C:一个,但pip只安装了D:. 这似乎远没有 Mac 的混乱那么常见(可能是因为 Windows 没有附带 Python,并且没有包管理器,因此您获得任何 Python 的唯一方法是运行安装程序)。这里的解决方案更简单:Windows 不需要 Python,因此您可以删除任何您想要的。



Finally, on non-Mac *nix systems, especially RHEL/CentOS Linux, you may have a Python 2.6 or 2.4 that's needed by the OS plus a Python 2.7 that you installed because you needed it, or a 2.7 that's needed by the OS and a 2.5 installed as a dependency for some "compatibility" package, or similar. Either way, you can easily accidentally install the pipfor the one you don't actually use (especially if you install it with the pipbootstrap instead of your package manager).

最后,在非 Mac *nix 系统上,尤其是 RHEL/CentOS Linux,您可能拥有操作系统所需的 Python 2.6 或 2.4 加上您因需要而安装的 Python 2.7,或操作系统所需的 2.7 和2.5 作为某些“兼容性”包或类似包的依赖项安装。无论哪种方式,您都可能很容易意外地pip为您实际上不使用的安装安装(特别是如果您使用pip引导程序而不是包管理器安装它)。

The solution here is pretty simple: uninstall that pip, and use yumor aptor whatever to install the python-pipthat goes with the Python 2.7 you want to use. And get in the habit of using python2.7and pip2.7—or just add aliases to your profile so that pythonor pyor whatever you prefer runs python2.7.

此处的解决方案非常简单:卸载该pip,然后使用yumapt或其他方式安装python-pip与您要使用的 Python 2.7 配套的 。并获得使用的习惯python2.7pip2.7-or只需添加别名到您的个人资料,以便pythonpy或任何你喜欢的运行python2.7

回答by Alfred Huang

Are you using Linuxand have both python2 and python3 installed?

您是否使用Linux并安装了 python2 和 python3?

If so, you installation by:

如果是这样,您可以通过以下方式安装:

pip install requests

would install the module to python2

将模块安装到 python2

So if you run import in python3, it may cause the problem.

所以如果你在python3中运行import,可能会导致这个问题。

Try to call:

尝试调用:

pip3 install requests

to install the module in python3 environment.

在python3环境中安装模块。

回答by mmarioolive

For devs with similar problem: Intall python3 version directly from the pack file on their website.

对于有类似问题的开发人员:直接从他们网站上的包文件中安装 python3 版本。

DO NOT DELETE OR EDIT ANYTHING ON usr/local/bin!

不要删除或编辑任何东西usr/local/bin

  1. rm -rf /Library/Frameworks/Python.framework/Versions/2.7
  2. rm -rf "/Applications/Python 2.7"

  3. Install python3 directly from package on official website.

  4. Reopen VSCode, (if you don't use it, you must) re-install modules in "Not Found" state, run command: pip3 install requestsand other "not found" modules by command pip3 install xxxxxx

  1. rm -rf /Library/Frameworks/Python.framework/Versions/2.7
  2. rm -rf "/Applications/Python 2.7"

  3. 直接从官网的包安装python3。

  4. 重新打开VSCode,(如果你不使用它,你必须)重新安装处于“Not Found”状态的pip3 install requests模块,通过命令运行 command: 和其他“not found”模块pip3 install xxxxxx

Add "python.pythonPath": "/usr/local/bin/python3"to your settings.jsonfile.

添加 "python.pythonPath": "/usr/local/bin/python3"到您的settings.json文件中。