Search code examples
pythonwindowspippython-requests

Cannot import just-installed `pip` packages


System info:

Windows 11 Home Edition 21H2 (22000.2416)
Python 3.11.5 (main, Aug 28 2023, 22:05:19) [GCC 11.3.0] on cygwin
pip 23.2.1
requests 2.31.0

The terminal output:

PS> pip install requests
Requirement already satisfied: requests in c:\users\bians\appdata\local\programs\python\python311\lib\site-packages (2.31.0)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\users\bians\appdata\local\programs\python\python311\lib\site-packages (from requests) (3.2.0)
Requirement already satisfied: idna<4,>=2.5 in c:\users\bians\appdata\local\programs\python\python311\lib\site-packages (from requests) (3.4)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\bians\appdata\local\programs\python\python311\lib\site-packages (from requests) (2.0.4)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\bians\appdata\local\programs\python\python311\lib\site-packages (from requests) (2023.7.22)
PS> python
Python 3.11.5 (main, Aug 28 2023, 22:05:19) [GCC 11.3.0] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'requests'
>>>
  • There is no need to pip install as seen.
  • I don't think there are problems with multiple versions of Python as the version shown at pip is the same with Python.
  • I don't use PyCharm.
  • Restarting or reinstalling requests does not work.
  • The most confusing thing is that requests worked yesterday but error occurs today. I searched on the net with no luck.

Solution

  • You might be installing packages to different environment than currently using to run script.

    • If you are using VS Code, (on windows) press ctl + shft + P and choose "Python : Select Interpreter". Make sure you are using the correct 3.11.5 version in your current project environment (or apply it globally, however not best practice).

    • @John Gordon's point is similar I believe, in checking that the sys.path of your current project env. refers to the same installation path of requests package in your output window: "c:\users\bians\appdata\local\programs\python\python311\lib\site-packages"