问题
hi i'm trying to learn web scraping but this code gives me an error i looked it up why and tried what they said but still doesn't work. i have a windows in my mac too and i tried in there and it worked but in macOS it doesn't work.
ok this is the code i'm trying to execute:
#!/usr/bin/env python3
import bs4 as bs
import urllib.request
sauce = urllib.request.urlopen("https://pythonprogramming.net/parsememcparseface/").read()
soup = bs.BeautifulSoup(sauce, "lxml")
print(soup)
this is the error i get:
`Traceback (most recent call last):
File "/Users/aaa/Desktop/import bs4 as bs.py", line 4, in <module>
import urllib.request
ImportError: No module named request
[Finished in 0.4s with exit code 1]
[shell_cmd: python -u "/Users/aaa/Desktop/import bs4 as bs.py"]
[dir: /Users/aaa/Desktop]
[path:/Library/Frameworks/Python.framework/Versions/3.8/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin] `
I looked this up and tried what they are saying
ImportError: No module named requests
but terminal says:
Requirement already satisfied: requests in
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (2.23.0)
Requirement already satisfied: idna<3,>=2.5 in
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from
requests) (2.9)
Requirement already satisfied: certifi>=2017.4.17 in
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from
requests) (2019.11.28)
Requirement already satisfied: chardet<4,>=3.0.2 in
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from
requests) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from
requests) (1.25.8)
I deleted this #!/usr/bin/env python3
from my code and tried again and still says the same thing.
and a couple of months ago I might mess up those "path" things by an accident.(i was trying to solve a bug or smt like that i copied and pasted some things to the terminal than some guy said i shouldn't be doing that i might do smt wrong. but i haven't noticed anything yet and i'm not sure what that thing was) i'm not even sure what that means i'm that new to programming. I don't really know it feels like that might be the problem.
回答1:
It seems that you have two Python versions (3.7, 3.8) so when you install request
it may be in either of two places.
I installed and setup pyenv
and on top, virtualenvwrapper
for issues like this; it was a long run solution but it payed off.
https://realpython.com/intro-to-pyenv/
https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html
Review your .bashrc
(or similar on Mac/Windows). You may have careful when changing PATH and other environment variables.
If it is an standalone app, it is ok to have your shebang #!/usr/bin/env python3
Where is urllib
This urllib library seems builtin so it may be an issue of the Python installation.
Please find if you have the particular file:
ls /home/USER/.pyenv/versions/3.7.0/lib/python3.7/urllib/ # On Linux with pyenv
# For me, it lists "request.py" and others
On the Python command line (and my current environment), this is what I got:
>>> import urllib as ur
>>> ur.__path__
['/home/USER/.pyenv/versions/3.7.0/lib/python3.7/urllib']
>>> import urllib.request as rq
>>> dir(rq)
['AbstractBasicAuthHandler', 'AbstractDigestAuthHandler', 'AbstractHTTPHandler', ..., 'urlsplit', 'urlunparse', 'warnings']
来源:https://stackoverflow.com/questions/61961058/cant-fix-importerror-no-module-named-request-error