Pip installs packages in the wrong directory

Question:

So I want to install the opencv-python package. I typed in pip install opencv-python and got this:

Collecting opencv-python
  Downloading opencv_python-4.7.0.72-cp37-abi3-win_amd64.whl (38.2 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 38.2/38.2 MB 3.1 MB/s eta 0:00:00
Requirement already satisfied: numpy>=1.17.0 in c:usersleo westerburg burrappdatalocalpackagespythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0localcachelocal-packagespython39site-packages (from opencv-python) (1.24.2)
Installing collected packages: opencv-python
Successfully installed opencv-python-4.7.0.72

When I try and import the package (on IDLE) I get

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    import cv2
ModuleNotFoundError: No module named 'cv2'

This is the same for all the packages I have installed recently, like numpy. The thing is when I type sys.path into the IDLE I get

C:UsersLeo Westerburg BurrAppDataLocalProgramsPythonPython311Libidlelib
C:UsersLeo Westerburg BurrAppDataLocalProgramsPythonPython311python311.zip
C:UsersLeo Westerburg BurrAppDataLocalProgramsPythonPython311DLLs
C:UsersLeo Westerburg BurrAppDataLocalProgramsPythonPython311Lib
C:UsersLeo Westerburg BurrAppDataLocalProgramsPythonPython311
C:UsersLeo Westerburg BurrAppDataLocalProgramsPythonPython311Libsite-packages

Which are all in the AppData/Local/Programs directory, however the packages are stored in appdatalocalpackagespythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0... as you can see when I installed opencv-python – which I find weird; why are they installed there and not in programspython ?

I have tried reinstalling pip, and also downloading a newer version of python. What is weird is that I have Python311 and Python38 in my Python folder, but this weird folder that has the packages is python39?

So my question is: how do I get pip to install packages in ProgramsPythonPython311..., rather than Packages... ?

Do I have to add something to my PATH?

Asked By: LWB

||

Answers:

You need to use python -m pip install. Why? pip is an executable that may or may not share an installation directory with your standard python. You can verify this by comparing:

pip -V

python -m pip -V

The latter command, python -m pip install ensures that pip is the one linked to the python command you run for IDLE.

Answered By: C.Nivs

It seems that you have both python3.9 and 3.11 installed. When just typing in pip install ... you probably install your package in python 3.9 whereas you run python 3.11 in IDLE.

Try python -V in your command prompt, it will probably answer python3.11

Answered By: Maelinho