简体   繁体   中英

Python Module Not found error (Requests)

I am relatively new to Python and I was installing a couple of modules - BeautifulSoup and Requests (learning how to scrape). So I installed Requests

$ pip install requests
Requirement already satisfied: requests in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (2.19.1)
Requirement already satisfied: certifi>=2017.4.17 in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from requests) (2018.4.16)
Requirement already satisfied: idna<2.8,>=2.5 in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from requests) (2.7)
Requirement already satisfied: urllib3<1.24,>=1.21.1 in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from requests) (1.23)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from requests) (3.0.4)

But then I when I run my file, I keep getting this error message

$ python test5.py
Traceback (most recent call last):
  File "test5.py", line 1, in <module>
    import requests

Could you help me with why I am having this issue?

On a separate note, all of a sudden BBEdit started asking me when I save a .py file "BBEDIT is trying to install a helper tool" and asking for a PW. Then it asks again "BBEDIT is trying to make modifications, allow it"? WHy is it doing that? Is it safe?

Thanks everyone and sorry for basic questions

A Stack Overflow answer can only cover one specific answer, not give a complete tutorial. But you probably actually want a complete tutorial. Also, if you're reading this from the distant future, whatever is written below may be as out-of-date as laptops that only do video and sound without smellovision. So, you should read the Python Packaging Authority's tutorial on installing packages .


You have (at least) two Python installations:

  • A Python 3.6 that you probably installed with a python.org installer, which lives in /Library/Frameworks/Python.framework/Versions/3.6/ .
  • A Python 2.7 that Apple included as part of the OS, which lives in /System/Library/Frameworks/Python.framework/Versions/2.7/ .

If you just run python , you get the Apple 2.7, not your 3.6. But you installed your packages for your Python 3.6, not your 2.7. 1 (Plus, you don't want to run 2.7 anyway.)

You're supposed to run python3 , as explained in PEP 394 —or, better, use a virtual environment —when you have both Python 2.x and Python 3.x on the same system. 2

Either will solve your problem.


If you're curious why pip installed for 3.6 instead of 2.7… well, it shouldn't work that way. But Apple's Python 2.7 is a bit out of date (I mean, even out of date for 2.7), so it doesn't even include pip . Which means the only pip on your system is the 3.6 one. So, even though python runs python2.7 instead of python3.6 , pip runs pip3.6 instead of pip2.7 . Which is very confusing, and pretty much unavoidable on current Macs.

This is a part of the reason current recommendations suggest never running pip directly; instead, run it like this:

python3 -m pip install requests

That guarantees that the Python installation you're installing requests into is the same on that you get when you run your script with python3 test5.py .

As things currently stand, as long as you don't ever touch Apple's 2.7, you can get away with just using pip3 , or even pip . But that could easily change in, say, macOS 10.14, so better to get into the right habit.


1. You can tell because of, eg, the paths in those "Requirement already satisfied" lines.

2. Well, the same non-Windows system, but you don't want to learn the different ways in which Windows is messed up, just stick to macOS here…

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM