'Why am i unable to run urllib in python
This is the code that seems to be correct:
import urllib.error, urllib.request, urllib.parse
import json
target='http://py4e-data.dr-chuck.net/json?'
local=input('Enter loction: ')
url=target+urllib.parse.urlencode({'address':local,'key':42})
print('retreiving',url)
data=urllib.request.urlopen(url).read()
print('retreived',len(data),'character')
js=json.loads(data)
print(json.dumps(js,indent=4))
print('Place id',js['results'][0]['place id'])
Error:
Enter file name: South Federal University
Traceback (most recent call last):
File "c:\Users\this pc\Desktop\Course\dude.py", line 1, in <module>
import urllib.error, urllib.request, urllib.parse
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 86, in <module>
import email
File "c:\Users\this pc\Desktop\Course\email.py", line 4, in <module>
fh = open(fname)
FileNotFoundError: [Errno 2] No such file or directory: 'South Federal University'
Solution 1:[1]
As evident from c:\Users\this pc\Desktop\Course\email.py appearing in the traceback, you have a file named email.py in the same directory as your script, and Python's import machinery makes it think it needs to import that (via this line in urllib.request).
Rename that email.py to e.g. email_test.py or whatnot - in any case, a name that's not a Python standard module - and you're good to go.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AKX |
