0

I downloaded the module requests in the command prompt using 'pip install requests'. It downloads fine and works from the command prompt, but when trying to import from the IDLE PyCharm I get the error message:

Traceback (most recent call last):

File "C:/Users/asher/PycharmProjects/begginer_questions/decode_a_web_page.py", line 1, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'

By copy-pasting the 'requests' file into the location:

C:/Users/asher/PycharmProjects/begginer_questions

along with the files contained in the file path:

C:/Users/asher/PycharmProjects/begginer_questions/venv/Lib/site-packages/pip-19.0.3-py3.7.egg/pip/_vendor

The code shown below finally gave no error messages.

However, this only seems like a temporary fix. Is there a permanent fix that could work or any reasons as to why this is happening?

The two common problems which often cause this to occur is that multiple versions of Python are downloaded and there is already a file called requests.py, neither are the case for me. I also don't understand why I had to copy-paste the second bunch of files considering they're already contained in begginer_questions.

I'm on Windows 10 with Python 3.7.3.

import requests

url = 'https://www.nytimes.com'
r = requests.get(url)
r_html = r.text
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • I see that you are using PyCharm. Is the virtual environment same as that you use in your PyCharm? – bumblebee Jul 22 '19 at 13:26
  • Is which virtual environment the same as the one in PyCharm, sorry? I think the one in PyCharm is located in: C:\User\asher\PycharmProjects\begginer_questions\venv\Scripts\python.exe, found in the project interpreter settings. – Asher Leask Jul 22 '19 at 14:17
  • \**[beginner](https://en.wiktionary.org/wiki/beginner#Noun)* – Peter Mortensen Jul 02 '23 at 15:19
  • There are a lot of Stack Overflow questions with this error message. See for example *[ImportError: No module named requests](https://stackoverflow.com/questions/17309288/importerror-no-module-named-requests)*. It has [46 linked questions](https://stackoverflow.com/questions/linked/17309288?sort=votes). – Peter Mortensen Jul 02 '23 at 15:38
  • Wasn't there a supply chain attack (typo attack?) in 2019 involving GitHub, Python 'requests' vs. 'request'? – Peter Mortensen Jul 02 '23 at 15:48
  • The 'pip' [typosquatting](https://en.wikipedia.org/wiki/Typosquatting) attack in 2019? – Peter Mortensen Jul 02 '23 at 15:54
  • OK, [in 2017](https://stackoverflow.com/questions/76440240/getting-this-error-while-installing-the-request-module-of-python?noredirect=1&lq=1#comment134785588_76440240): *[Several malicious typosquatted Python libraries found on PyPI repository](https://thehackernews.com/2021/07/several-malicious-typosquatted-python.html)*. That is, '`requests`' (the real library) vs. '`request`' (the malicious one. Without "`s`") – Peter Mortensen Jul 02 '23 at 16:55
  • The now removed blog post was: *[What is the story behind RussianIdiot on PyPI?](https://stackoverflow.com/questions/51748121/i-am-trying-to-install-setupfiles-package-from-pip/52032994#52032994)* – Peter Mortensen Jul 02 '23 at 16:55

1 Answers1

0

My confusion came from the fact that when a project is created with an up-to-date pip and Python. A virtual environment (venv) is automatically created and is a primary place where the project will look for modules/packages. In the command prompt, navigate to your project. In my case it would be:

> cd User\asher\PycharmProjects\begginer_questions

Then enter the following command the activate the virtual environment "venv" pre-made by Python or PyCharm (note this is different for Mac):

venv\Scripts\activate.bat

In this virtual environment, enter the command:

pip install requests

The solution seems (and probably is) very obvious, but this should work because sys.path will always look for packages in this virtual environment instead of where ever it was installed when I tried before. Don't forget to deactivate your virtual environment in the command prompt when you're finished installing whatever packages you want by entering:

deactivate

Note: I am reposting this as a copy of the solution added into the question since it should really be an answer. I am making it CW as it’s not my solution exactly, but OPs are unlikely to post it after four years.

After a long while, I found the answer so for anyone struggling with this problem I hope this helps.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131