ImportError: No module named 'requests' Py

2019-06-21 03:27发布

When attempting to import the requests module in Python 3.4.0 I receive the following error: ImportError: No module named 'requests'

The requests module in previous version of Python required that you use pip separately. But according to the new features in Python 3.4.0, pip is built in: https://docs.python.org/3.5/whatsnew/3.4.html#whatsnew-pep-453

My import line is simply:

import requests

I am confused as to why this is not working. All help is greatly appreciated.

3条回答
混吃等死
2楼-- · 2019-06-21 04:13

Having pip included is meant to ease retrieving new packages. It does not state that all modules reachable through pip are bundled in new Python (hopefully !).

You are still required to pip install requests before using the package requests.

Edit: following another question, it seems dependencies on requests are flawed. Then try:

pip install chardet2 urllib3

as suggested by mentioned SO question.

查看更多
再贱就再见
3楼-- · 2019-06-21 04:16

Although pip is built-in, you still have to call pip install requests. pip is just the tool for downloading, not the actual item. To download, use the following:

bash-3.2$ pip install requests
Downloading/unpacking requests
  Downloading requests-2.2.1-py2.py3-none-any.whl (625kB): 625kB downloaded
Installing collected packages: requests
Cleaning up...
...

You don't have to install pip, but you still have to use it to install other items.

Answering your comment, run the pip install again, this time with a sudo. You should see the following message:

$ sudo pip install requests
Password:
Requirement already satisfied (use --upgrade to upgrade): requests in /Library/Python/2.7/site-packages/requests-2.1.0-py2.7.egg
Cleaning up...

Or something of the likes. Then run this:

>>> import requests
>>> requests
<module 'requests' from '/Library/Python/2.7/site-packages/requests-2.1.0-py2.7.egg/requests/__init__.pyc'>
>>> 

If the import fails, go to the above directory and search for the files.

查看更多
一纸荒年 Trace。
4楼-- · 2019-06-21 04:18

There is a possible answer here.

  1. Install pip for python 3: sudo apt-get install python3-pip
  2. Use pip3 to install request module: pip3 install requests

If you get permission denied error, run the previous command in sudoers mode: sudo pip3 install requests

查看更多
登录 后发表回答