pythonpippython-modulepython-packagingpythonpath

Pip installs packages in the wrong directory


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:\users\leo westerburg burr\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-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:\Users\Leo Westerburg Burr\AppData\Local\Programs\Python\Python311\Lib\idlelib
C:\Users\Leo Westerburg Burr\AppData\Local\Programs\Python\Python311\python311.zip
C:\Users\Leo Westerburg Burr\AppData\Local\Programs\Python\Python311\DLLs
C:\Users\Leo Westerburg Burr\AppData\Local\Programs\Python\Python311\Lib
C:\Users\Leo Westerburg Burr\AppData\Local\Programs\Python\Python311
C:\Users\Leo Westerburg Burr\AppData\Local\Programs\Python\Python311\Lib\site-packages

Which are all in the AppData/Local/Programs directory, however the packages are stored in appdata\local\packages\pythonsoftwarefoundation.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 programs\python ?

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 Programs\Python\Python311\..., rather than Packages\... ?

Do I have to add something to my PATH?


Solution

  • 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.