Search code examples
pythonpippipenvpipenv-installpipx

What causes Python packages installed with pipenv to not be found when called in script?


On a Windows 10 machine with Python 3.9.5 and pipenv 2021.5.29. In a pipenv shell:

Creating a virtualenv for this project...
Pipfile: C:\Users\jp\Documents\programming\imseg\Pipfile
Using C:/OSGeo4W/bin/python.exe (3.9.5) to create virtualenv...
[ ===] Creating virtual environment...created virtual environment CPython3.9.5.final.0-64 in 612ms
  creator CPython3Windows(dest=C:\Users\jp\.virtualenvs\imseg-cT2t34Fc, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\jp\AppData\Local\pypa\virtualenv)
    added seed packages: pip==21.2.2, setuptools==57.4.0, wheel==0.36.2
  activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator

Successfully created virtual environment!
Virtualenv location: C:\Users\jp\.virtualenvs\imseg-cT2t34Fc
Launching subshell in virtual environment...
Microsoft Windows [Version 10.0.19043.1165]
(c) Microsoft Corporation. All rights reserved.

I used pipenv install. This returns:

(imseg-cT2t34Fc) C:\Users\jp\Documents\programming\imseg>pipenv install
Installing dependencies from Pipfile.lock (684440)...
  ================================ 19/19 - 00:01:02

pipenv graph:

(imseg-cT2t34Fc) C:\Users\jp\Documents\programming\imseg>pipenv graph
Cython==0.29.24
GitPython==3.1.18
  - gitdb [required: >=4.0.1,<5, installed: 4.0.7]
    - smmap [required: >=3.0.1,<5, installed: 4.0.0]
matplotlib==3.4.2
  - cycler [required: >=0.10, installed: 0.10.0]
    - six [required: Any, installed: 1.16.0]
  - kiwisolver [required: >=1.0.1, installed: 1.3.1]
  - numpy [required: >=1.16, installed: 1.21.1]
  - pillow [required: >=6.2.0, installed: 8.3.1]
  - pyparsing [required: >=2.2.1, installed: 2.4.7]
  - python-dateutil [required: >=2.7, installed: 2.8.2]
    - six [required: >=1.5, installed: 1.16.0]
opencv-python==4.5.3.56
  - numpy [required: >=1.19.3, installed: 1.21.1]
pandas==1.3.1
  - numpy [required: >=1.17.3, installed: 1.21.1]
  - python-dateutil [required: >=2.7.3, installed: 2.8.2]
    - six [required: >=1.5, installed: 1.16.0]
  - pytz [required: >=2017.3, installed: 2021.1]
rawpy==0.16.0
  - numpy [required: Any, installed: 1.21.1]
torchvision==0.10.0
  - numpy [required: Any, installed: 1.21.1]
  - pillow [required: >=5.3.0, installed: 8.3.1]
  - torch [required: ==1.9.0, installed: 1.9.0]
    - typing-extensions [required: Any, installed: 3.10.0.0]

Yet, when I try to call the script for which the environment was created, I get:

(imseg-cT2t34Fc) C:\Users\jp\Documents\programming\imseg\src>python3 model.py
Traceback (most recent call last):
  File "C:\Users\jp\Documents\programming\imseg\src\model.py", line 14, in <module>
    import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'

I read and tried

pip install pipx
pipx install pipenv

and there was no change in the problem. What is wrong with my pipenv environment?

EDIT: where python3 returns:

C:\OSGeo4W\bin\python3.exe
C:\OSGeo4W\apps\Python39\python3.exe
C:\Users\jp\AppData\Local\Microsoft\WindowsApps\python3.exe

EDIT (#2): It's unclear to me where the python executable is supposed to exist? I guess I don't get the virtual environment thing. If the python executable is supposed to exist in virtual env., why isn't the python executable installed inside the virtual env. to begin? The pipenv website just says to have python and I installed as pip3 install --user pipenv, so it seems like it should work. I'm still wondering how to fix this problem.


Solution

  • The answer is quite simple. The line that calls the Python executable should be

    pipenv run python model.py

    instead of

    python model.py

    as pointed out in the accepted answer to this question.