0

i am using visual studio code to run code, it is working fine for most of the imports, except when i try to import the urllib module.

consider this code:

import urllib.request
x = urllib.request.urlopen('https://www.google.com/')
print(x.read())

when i run it on idle it works just fine, but if i run it on visual studio code i get an error:

File "c:\MyPythonScripts\dictionary Python\urllib.py", line 2, in import urllib.request ModuleNotFoundError: No module named 'urllib.request'; 'urllib' is not a package

i have searched everywhere trying to find a solution to this issue, since i am also having the same problem when i import the shelve module as well.

any idea would be highly appreciated, or should i even stop using Visual studio code altogether ?

t.m.adam
  • 15,106
  • 3
  • 32
  • 52
Elias Rub
  • 99
  • 3
  • 9
  • 5
    `c:\MyPythonScripts\dictionary Python\urllib.py`: The name of your script shadows the `urllib` package. Rename your script. – shmee Mar 19 '19 at 08:20
  • yes you are correct, i changed the name and it works just fine, how can i mark your comment as the answer? – Elias Rub Mar 19 '19 at 10:16

2 Answers2

0

Try to type:

from urllib.request import urlopen
Cà phê đen
  • 1,883
  • 2
  • 21
  • 20
0

Why is using requests preferred:

import requests    
x = requests.get("https://www.google.com/")
print(x.content)

OUTPUT:

b'<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en">
  <head><meta content="Search the world\'s information, including webpages, images, 
  videos and more. Google has many special features to help you find exactly 
  what you\'re looking for." name="description"><meta content="noodp" . . .
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • your suggestion also works on idle but not on visual studio, just like my code, the issue is about running this code on visual studio, and figuring out how to handle a missing library module error, while it works just fine on IDLE. thnx for your suggestion – Elias Rub Mar 19 '19 at 08:41
  • 3
    @EliasRub Because `requests`' chain of dependencies ends up doing `import urllib.parse` when executing `import requests` in your script. And since the name of your script is `urllib.py`, the interprter looks in _your_ script for a module named `parse`, instead of the Python standard lib package `urllib`. *Do not name your scripts after standard lib or 3rd party packages!* – shmee Mar 19 '19 at 09:53
  • `ModuleNotFoundError: No module named 'requests'` :) – ashrasmun May 05 '22 at 15:49