在Python中导入请求模块在OS X上不起作用

问题描述 投票:0回答:2

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

pip install requests

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

import requests

失败并出现以下错误

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

我找到了这个问题的答案here,但它没有说明有关将 python 2.x 放在同一目录中的任何内容。

我的终端输出是:

MacBook-Pro:~ tanmaybaranwal$ which python

/usr/bin/python

MacBook-Pro:~ tanmaybaranwal$ which python2.6

/usr/bin/python2.6

MacBook-Pro:~ tanmaybaranwal$ which pip

/usr/local/bin/pip

MacBook-Pro:~ tanmaybaranwal$ sudo pip install python-firebase
Password:
Requirement already satisfied (use --upgrade to upgrade): python-firebase in /Library/Python/2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): requests>=1.1.0 in ./Library/Python/2.7/lib/python/site-packages (from python-firebase)
Cleaning up...

MacBook-Pro:~ tanmaybaranwal$ sudo pip install requests
Requirement already satisfied (use --upgrade to upgrade): requests in ./Library/Python/2.7/lib/python/site-packages
Cleaning up...

MacBook-Pro:~ tanmaybaranwal$ python
Python 2.7.10 (default, Oct 23 2015, 18:05:06) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import firebase
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/firebase/__init__.py", line 4, in <module>
    from firebase import *
  File "/Library/Python/2.7/site-packages/firebase/firebase.py", line 10, in <module>
    from .decorators import http_connection
  File "/Library/Python/2.7/site-packages/firebase/decorators.py", line 1, in <module>
    import requests
ImportError: No module named requests

列出 PIP 模块也没有任何

requests
模块。

python macos python-2.7 python-requests
2个回答
6
投票

为了确保您使用正确的

python
来通过 pip 安装某些内容,您可以通过
-m

运行它
$ python -m pip install --user requests

(如果省略

--user
标志,那么您可能需要 root 权限)


0
投票

使用 macos sonoma 和 python3.9+,这对我有用:


brew install --verbose --require-sha python-requests

© www.soinside.com 2019 - 2024. All rights reserved.