简体   繁体   中英

venv cannot find python library even though I definitely have it installed

Im runnig a windows 10 machine, and im getting started on playing with virtual enviorments. I want to run a small fastapi application

I created a venv like this:

python -m venv venv 

I then activated the venv, and installed my two dependencies:

(venv) PS C:\Users\Ask\python_Projects\garse_dockerAPI\app> pip install fastapi

and

(venv) PS C:\Users\Ask\python_Projects\garse_dockerAPI\app> pip install unicorn

Which seemed to work fine, running pip list also seems to indicate that i have what I need:

(venv) PS C:\Users\Ask\python_Projects\garse_dockerAPI\app> pip list           
Package           Version
----------------- ---------
beautifulsoup4    4.9.3    
certifi           2020.6.20
cffi              1.14.3
chardet           3.0.4
crypto            1.4.1
cryptography      3.1.1
cycler            0.10.0
fastapi           0.63.0
idna              2.10
myModule          1.0.0
Naked             0.1.31
pip               21.0.1
pycparser         2.20
pycrypto          2.6.1
pydantic          1.8.1
PyYAML            5.3.1
requests          2.24.0
setuptools        49.2.1
shellescape       3.8.1
six               1.15.0
soupsieve         2.0.1
starlette         0.13.6
typing-extensions 3.7.4.3
unicorn           1.0.2
urllib3           1.25.10
wheel             0.35.1

But when I run my program it fails with the inmport of the fastapI:

(venv) PS C:\Users\Ask\python_Projects\garse_dockerAPI\app> python .\main.py 
Traceback (most recent call last):
  File "C:\Users\Ask\python_Projects\garse_dockerAPI\app\main.py", line 3, in <module>
    from fastapi import FastAPI

Also, running python my terminal doesnt wanna do it either:

(venv) PS C:\Users\Ask\python_Projects\garse_dockerAPI\app> python 
Python 3.9.0 (tags/v3.9.0:9cf6752, Oct  5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import fastapi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'fastapi'

What's going on? why do i not have the dependencies i need?

EDIT:

In order to find the installation on my machine, I ran 'where python' in my normal terminal:

C:\Users\Ask>where python
C:\Users\Ask\AppData\Local\Programs\Python\Python39\python.exe
C:\Users\Ask\AppData\Local\Microsoft\WindowsApps\python.exe

Which give sme two different paths. I dont really know what to do with this information? running where pip:

C:\Users\Ask>where pip
C:\Users\Ask\AppData\Local\Programs\Python\Python39\Scripts\pip.exe

When I run it in my venv, the commands output nothing at all:

(venv) PS C:\Users\Ask\python_Projects\garse_dockerAPI\app> where pip
(venv) PS C:\Users\Ask\python_Projects\garse_dockerAPI\app> where python
(venv) PS C:\Users\Ask\python_Projects\garse_dockerAPI\app> 

I definitely recommend using python3 -m pip instead of pure pip . It might save you some headache. The reason: when I used pip to install new packages in a venv virtual environment, they ended up in my OS environment! But just using python3 -m pip did the trick.

To make sure that the packages are called from your virtual environment, you can use python3 -m PACKAGE_NAME instead of PACKAGE_NAME . For example, flower didn't work in my virtual environment, but python3 -m flower works with no problem.

You can directly ask python where it is executed from: Try this:

>>> import sys

>>> print(sys.executable)
/PATH/TO/VENV/bin/python

>>> print(sys.version)
'3.10.5 (main, Jun  6 2022, 18:49:26) [GCC 12.1.0]'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM