2

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.

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
  • Does this answer your question? [ImportError: No module named requests](https://stackoverflow.com/questions/17309288/importerror-no-module-named-requests) – Red May 22 '20 at 17:59

1 Answers1

0

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']
Omr
  • 39
  • 4
  • can i just delete one of the versions? like 3.7, would that be ok? – Daniel Ernst May 22 '20 at 19:17
  • Before deletion, run a `pip freeze` to see what packages have you installed. The version deletion may help, but I think is more important your path configuration (back it up first). (Are you working on mac? -I work mainly on Linux) – Omr May 22 '20 at 19:34
  • yep it's mac. i deleted 3.8 before you posted haha. i'm just a beginner to programming so i hope i'm fine now? haha i hope i didn't do something wrong again. – Daniel Ernst May 22 '20 at 19:37
  • my PATH looks like this /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 – Daniel Ernst May 22 '20 at 19:47
  • If you deleted 3.8, your PATH may look like :/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin – Omr May 25 '20 at 16:59
  • no it still looks like this: /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 – Daniel Ernst May 30 '20 at 16:57