本文主要是介绍如何正确地为Python项目安装依赖,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
a、创建Python项目,其结构如下:
b、激活虚拟环境
启动DOS窗口—>进入“Scripts”目录,这里为D:\workspace\prj_python_1\venv\Scripts—>执行activate激活虚拟环境,如下所示:
Microsoft Windows [版本 10.0.18363.535]
(c) 2019 Microsoft Corporation。保留所有权利。C:\Users\GaoHuanjie>cd /d D:\workspace\prj_python_1\venv\ScriptsD:\workspace\prj_python_1\venv\Scripts>activate(venv) D:\workspace\prj_python_1\venv\Scripts>
注意:activate命令执行后,命令执行路径前面会自动添加“(venv) ”。
c、下载Python包
虚拟环境激活后,再使用pip install命令安装Python包,Python包将被保存至该虚拟环境下“Lib\site-packages”文件夹内,而不会保存到Python根目录下的相关文件夹中,操作如下:
Microsoft Windows [版本 10.0.18363.535]
(c) 2019 Microsoft Corporation。保留所有权利。C:\Users\GaoHuanjie>cd /d D:\workspace\prj_python_1\venv\ScriptsD:\workspace\prj_python_1\venv\Scripts>activate(venv) D:\workspace\prj_python_1\venv\Scripts>pip install -i https://mirrors.aliyun.com/pypi/simple/ pymysql==1.1.0rc1
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting pymysql==1.1.0rc1
...省略...
Installing collected packages: pymysql
Successfully installed pymysql-1.1.0rc1[notice] A new release of pip is available: 23.2.1 -> 23.3.2
[notice] To update, run: python.exe -m pip install --upgrade pip(venv) D:\workspace\prj_python_1\venv\Scripts>
d、退出虚拟环境
若退出该虚拟环境,直接执行deactivate.bat命令即可,如下所示:
Microsoft Windows [版本 10.0.18363.535]
(c) 2019 Microsoft Corporation。保留所有权利。C:\Users\GaoHuanjie>cd /d D:\workspace\prj_python_1\venv\ScriptsD:\workspace\prj_python_1\venv\Scripts>activate(venv) D:\workspace\prj_python_1\venv\Scripts>pip install -i https://mirrors.aliyun.com/pypi/simple/ pymysql==1.1.0rc1
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting pymysql==1.1.0rc1
...省略...
Installing collected packages: pymysql
Successfully installed pymysql-1.1.0rc1[notice] A new release of pip is available: 23.2.1 -> 23.3.2
[notice] To update, run: python.exe -m pip install --upgrade pip(venv) D:\workspace\prj_python_1\venv\Scripts>deactivate.bat
D:\workspace\prj_python_1\venv\Scripts>
这篇关于如何正确地为Python项目安装依赖的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!