As can be seen from the above screenshots, I’m having trouble importing the ‘requests’ package using Pip. I’ve verbatim followed the instructions pointed out in Mosh’s video titled Pip from the Python Package Index chapter. I can’t seem to figure out what could be going wrong.
Why does the terminal say: ModuleNotFoundError: No module named ‘requests’ ? Furthermore, pylint is saying: unable to import ‘requests’.
Any help would be greatly appreciated. Thanks a bunch!
Did you install the requests library on the system, for example, using the command line pip install requests?
Or maybe you’ve chosen the wrong environment, it looks like you run the script on python 3.9.13 64-bit, which is your system python interpreter, Maybe you have installed the library in a different virtual environment.
I went back to see where the requests package is installed and its path is shown in the picture above. I ran the command to install the requests package (pip install requests) from within the VS Code terminal. The only thing I can think of is the way my VS Code is configured, I think.
Also, I’m a little new to python and the terminal, so I didn’t fully understand what you meant by “choosing the wrong environment”. May you please clarify that? Thanks!
From your screen capture, I recognized that you had installed the requests library in your virtual environment named pyhton310, correct?
But you didn’t choose the Python310 environment like the left of the screen capture. check out the area I rectangle, click it, and select a proper environment; you’ll be ok.
Thank you! That made progress! Now pylint recognizes the import but I still get the ModuleNotFoundError: no module named ‘requests’.
I went to double check where my requests package is stored. It is still in the python310 folder. What could be going wrong now? If the proper environment is selected and the import is recognized, why can the requests package/module not be found?
You still have the ModuleNotFound error because you have not executed the script via the right python environment.
Normally, when you open a new terminal in vscode it would automatically activate the virtual environment, in this case, Python310, but from your screen capture, you can see you still executed the script using the system python interpreter, not the one you think.
I made a small record for you to show how this works; perhaps this might be helpful.
Wow! It works! When I follow the gif you made, I finally get the output <Response [200]>. I’ll try to run all my python files this way. I was always running the terminal by opening it up in VS Code using the alt+shift+n keys. Why is there a difference between this function and opening it with the “run python file in terminal” command? Are there two different terminals I’m not aware of? Thanks!
I’m not so sure about the differences between the two ways. Maybe it has something to do with the types of the terminal; you see, the default type of terminal is PowerShell.exe, I have encountered a lot of problems for the first time using VSCode, So I changed to cmd.exe as terminal, and everything is fine. both the way I run the python script or yours worked.
Since it works just fine and worrying about causing some misunderstanding for English is not my native language, I didn’t dig deeper. Perhaps you can do some troubleshooting and if you figure it out, let me know, I would be very appreciated!
If using cmd.exe is better than using the default PowerShell.exe, would you recommend me to switch too? I feel that my will work only if I run the python file in the terminal, but I don’t see a way to open up the terminal without having to run the file.
I’m actually a complete newbie in programming so I wanted to ask a more seasoned expert like you if I can make the same switch you’ve made. What did you do to change your terminal to cmd.exe from the Powershell.exe.
I’m bringing this up because I am now encountering problems when trying to create a virtual environment with the ‘python -m venv env’ command.
About switching terminals, it’s really up to you. If you like PowerShell.exe , then use it; if don’t, switch it. That simple.
I’m not sure that I am qualified to provide professional tutorials. because, as I said, English is not my native language, and I am not good at it right now. some of my expressions have grammatical mistakes. So I’m most certainly sure that I may cause some misinterpretation if I offer you professional tutorials.
A little advice, Google is a powerful tool for you to learn programming, make good use of it. You can find answers in every situation and problem, at least when you are a newbie. And for virtual environment management, I recommend Anaconda; take a look at it; you will like it.