[英]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.我正在运行 windows 10 机器,我开始玩虚拟环境。 I want to run a small fastapi application我想运行一个小型 fastapi 应用程序
I created a venv like this:我创建了一个这样的venv:
python -m venv venv
I then activated the venv, and installed my two dependencies:然后我激活了 venv,并安装了我的两个依赖项:
(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:这似乎工作正常,运行 pip 列表似乎也表明我有我需要的东西:
(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:但是当我运行我的程序时,它会因 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:此外,运行 python 我的终端也不想这样做:
(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:为了在我的机器上找到安装,我在我的普通终端中运行了“where python”:
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:运行 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 中运行它时,命令 output 什么都没有:
(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
.我绝对推荐使用python3 -m pip
而不是纯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!原因:当我使用pip
在venv
虚拟环境中安装新软件包时,它们最终出现在我的操作系统环境中! But just using python3 -m pip
did the trick.但只需使用python3 -m pip
就可以了。
To make sure that the packages are called from your virtual environment, you can use python3 -m PACKAGE_NAME
instead of PACKAGE_NAME
.为了确保从您的虚拟环境中调用包,您可以使用python3 -m PACKAGE_NAME
而不是PACKAGE_NAME
。 For example, flower
didn't work in my virtual environment, but python3 -m flower
works with no problem.例如, flower
在我的虚拟环境中不起作用,但python3 -m flower
没有问题。
You can directly ask python
where it is executed from: Try this:可以直接询问python
是从哪里执行的:试试这个:
>>> 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]'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.