简体   繁体   中英

VS Code: "Import Error: No module named request" although it's installed

To build a web scraping program in python 3 I do the following

import urllib.request

I use a venv, which is selcted and shown on the bottom left in VS Code. The settings.json file has:

"python.pythonPath": "venv/bin/python"

Also the integrated terminal has been closed and reopened multiple times. Entering pip list shows request as installed for that venv.

If I run the file no problems come up but in output it keeps saying

Traceback (most recent call last):
File "/Users/filepath/filename.py", line 2, in 
<module>
import urllib.request
ImportError: No module named request

Important: The last time I had this file open, everything was fine with importing that module. Now after saving and reopening that project next day I keep getting this error message over and over again.

I really hope someone can help! Thanks!

One of two things is occurring. One is that your environment is actually Python 2 and not Python 3 as urllib.request only exists in the latter (you can check this by looking in the lower-left of your screen in the status bar). The other possibility is you created a file named urllib in your own code and that's shadowing the urllib package in the stdlib. You can verify this by changing your code to import urllib; print(urllib.__file__) import urllib; print(urllib.__file__) . If that prints out a path to one of your files then rename that file and it should fix your issue.

You don't need to change your import line from import urllib.request ; that's basically the same as from urllib import request but lets you use a different name to get at the same module.

Also note that urllib.request is included in Python 3 itself, so it won't show up in a call to pip list . I suspect you're thinking of requests which is a different package and accessible via import requests .

Firstly, welcome to Stack Overflow! This looks like an issue with how you imported request from urllib. Could you please try changing the import to this instead:

from urllib import request

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM