installing urllib in Python3.6
Asked Answered
F

7

32

I would like to import urllib to use the function 'request'. However, I encountered an error when trying to do so. I tried pip install urllib but still had the same error. I am using Python 3.6. Really appreciate any help.

i do import urllib.request using this code:

import urllib.request, urllib.parse, urllib.error 
fhand = urllib.request.urlopen('data.pr4e.org/romeo.txt') 
counts = dict()
for line in fhand: 
    words = line.decode().split() 
for word in words: 
    counts[word] = counts.get(word, 0) + 1 
print(counts) 

but it gives me this error: ModuleNotFoundError: No Module named 'urllib.parse'; 'urllib' is not a package

here is a screenshot for the error

Fortyfour answered 9/12, 2017 at 15:46 Comment(6)
What OS, what were the errors etc?Glee
i'm using windows. i edited the question with more details and a screenshot of the problem. please check it.Fortyfour
Did you try from urllib import request?Joeyjoffre
How did you install Python3? Please try to give all the information so people can answer your question.Glee
always put full error message (Traceback) in question (as text, not screenshot). There are other useful informations.Hemelytron
You saved your code in file urllib.py and it is your problem. import urllib first tries to load file urllib.py in local folder and it loads your file instead expected module..Hemelytron
I
57

urllib is a standard library, you do not have to install it. Simply import urllib

Impanation answered 9/12, 2017 at 15:49 Comment(4)
i did, but still not working. i edited the question with more details and a screenshot of the problem. please check it.Fortyfour
Or from urllib import requestAustralian
@mohamed You did not import exactly as stated in t answerAustralian
@ArthurTacca thank you very much man! it's working with your edit.Fortyfour
E
7

urllib is a standard python library (built-in) so you don't have to install it. just import it if you need to use request by:

import urllib.request

if it's not work maybe you compiled python in wrong way, so be kind and give us more details.

Entomostracan answered 9/12, 2017 at 16:12 Comment(4)
i do import urllib.request using this code: import urllib.request, urllib.parse, urllib.error fhand = urllib.request.urlopen('data.pr4e.org/romeo.txt') counts = dict() for line in fhand: words = line.decode().split() for word in words: counts[word] = counts.get(word, 0) + 1 print(counts) but it gives me this error: ModuleNotFoundError: No Module named 'urllib.parse'; 'urllib' is not a packageFortyfour
@ahmed i edited the question with more details and screenshot, please check it.Fortyfour
Your code above works fine with me (just replace line 2 with fhand = urllib.request.urlopen('http://data.pr4e.org/romeo.txt') ) without any ModuleNotFoundError exceptions, are you sure you have python 3.6 ? and if yes are you have python 3.6.3 or something else ?Entomostracan
i have python 3.6.3Fortyfour
O
5

The corrected code is

import urllib.request
fhand = urllib.request.urlopen('http://data.pr4e.org/romeo.txt')
counts = dict()
for line in fhand: 
    words = line.decode().split() 
for word in words: 
    counts[word] = counts.get(word, 0) + 1 
print(counts) 

running the code above produces

{'Who': 1, 'is': 1, 'already': 1, 'sick': 1, 'and': 1, 'pale': 1, 'with': 1, 'grief': 1}
Oshiro answered 3/3, 2018 at 15:41 Comment(0)
B
2

This happens because your local module named urllib.py shadows the installed requests module you are trying to use. The current directory is preapended to sys.path, so the local name takes precedence over the installed name.

An extra debugging tip when this comes up is to look at the Traceback carefully, and realize that the name of your script in question is matching the module you are trying to import.

Rename your file to something else like url.py. Then It is working fine. Hope it helps!

Bickford answered 1/7, 2019 at 17:24 Comment(0)
R
0

yu have to install the correct version for your computer 32 or 63 bits thats all

Richman answered 27/9, 2020 at 2:22 Comment(0)
P
0
import urllib.request, urllib.parse, urllib.error
import ssl
context = ssl._create_unverified_context()
fhand = urllib.request.urlopen('data.pr4e.org/romeo.txt',context) 
counts = dict()
for line in fhand:
    words = line.decode().split() 
for word in words: 
    counts[word] = counts.get(word, 0) + 1 
print(counts)
Plexiglas answered 2/11, 2022 at 6:46 Comment(2)
try this. It worked for me.Plexiglas
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Bojorquez
O
0

You need to use

import urilib3
Oletaoletha answered 26/2, 2023 at 18:51 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Truss

© 2022 - 2024 — McMap. All rights reserved.