pipenv 集成了pip,virtualenv两者的功能,并优化了两者的一些缺陷。
pipenv使用 pipflie和pipfile.lock,前者用来记录项目的依赖列表,后者记录了固定版本的详细依赖包列表,查看依赖关系十分方便
一 创建虚拟环境
1. mkdir env01
2. cd env01
3. pipenv install
pipenv install:目录如果没有虚拟环境会创建一个新的虚拟环境,如果已经有虚拟环境则不会创建
C:\Users\Administrator\Desktop\2022\test_env>mkdir env01
C:\Users\Administrator\Desktop\2022\test_env>cd env01
C:\Users\Administrator\Desktop\2022\test_env\env01>pipenv install
Creating a virtualenv for this project...
Pipfile: C:\Users\Administrator\Desktop\2022\test_env\env01\Pipfile
Using D:/Python38/python.exe (3.8.10) to create virtualenv...
[ =] Creating virtual environment...created virtual environment CPython3.8.10.final.0-64 in 1688ms
creator CPython3Windows(dest=D:\.env\env01-PkZYulaV, 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\Administrator\AppData\Local\pypa\virtualenv
)
added seed packages: pip==22.2.2, setuptools==65.0.2, wheel==0.37.1
activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
Successfully created virtual environment!
Virtualenv location: D:\.env\env01-PkZYulaV
Creating a Pipfile for this project...
Pipfile.lock not found, creating...
Locking [packages] dependencies...
Locking [dev-packages] dependencies...
Updated Pipfile.lock (db4242)!
Installing dependencies from Pipfile.lock (db4242)...
================================ 0/0 - 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
C:\Users\Administrator\Desktop\2022\test_env\env01>pipenv install
Installing dependencies from Pipfile.lock (db4242)...
================================ 0/0 - 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
4. 将目录中的 pipfile中的包下载地址替换成python国内源的下载地址,
[[source]]
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
verify_ssl = true
name = "pypi"
[packages]
[dev-packages]
[requires]
python_version = "3.8"
在默认包中安装 插件,pipenv install requests
C:\Users\Administrator\Desktop\2022\test_env\env01>pipenv install requests
Installing requests...
Adding requests to Pipfile's [packages]...
Installation Succeeded
Pipfile.lock (db4242) out of date, updating to (fbd99e)...
Locking [packages] dependencies...
Building requirements...
Resolving dependencies...
Success!
Locking [dev-packages] dependencies...
Updated Pipfile.lock (fbd99e)!
Installing dependencies from Pipfile.lock (fbd99e)...
================================ 0/0 - 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
默认安装的包,会在 pipfile的 [packages] 下显示
[packages]
requests = "*"
在开发环境下安装包,pipenv install --dev django
[packages]
requests = "*"
[dev-packages]
django = "*"
查看安装包的依赖关系 pipenv graph
C:\Users\Administrator\Desktop\2022\test_env\env01>pipenv graph
Django==4.1.2
- asgiref [required: >=3.5.2,<4, installed: 3.5.2]
- backports.zoneinfo [required: Any, installed: 0.2.1]
- sqlparse [required: >=0.2.2, installed: 0.4.3]
- tzdata [required: Any, installed: 2022.5]
requests==2.28.1
- certifi [required: >=2017.4.17, installed: 2022.9.24]
- charset-normalizer [required: >=2,<3, installed: 2.1.1]
- idna [required: >=2.5,<4, installed: 3.4]
- urllib3 [required: >=1.21.1,<1.27, installed: 1.26.12]
进入虚拟环境:pipenv shell,查看安装的依赖:pip list,并退出依赖:exit
C:\Users\Administrator\Desktop\2022\test_env\env01>pipenv shell
Launching subshell in virtual environment...
Microsoft Windows [版本 10.0.19042.1466]
(c) Microsoft Corporation。保留所有权利。
(env01-PkZYulaV) C:\Users\Administrator\Desktop\2022\test_env\env01>pip list
Package Version
------------------ ---------
asgiref 3.5.2
backports.zoneinfo 0.2.1
certifi 2022.9.24
charset-normalizer 2.1.1
Django 4.1.2
idna 3.4
pip 22.2.2
requests 2.28.1
setuptools 65.0.2
sqlparse 0.4.3
tzdata 2022.5
urllib3 1.26.12
wheel 0.37.1
(env01-PkZYulaV) C:\Users\Administrator\Desktop\2022\test_env\env01>exit
列出虚拟环境的路径 pipenv --venv
C:\Users\Administrator\Desktop\2022\test_env\env01>pipenv --venv
D:\.env\env01-PkZYulaV
删除虚拟环境:pipenc --rm
C:\Users\Administrator\Desktop\2022\test_env\env02>pipenv --rm
Removing virtualenv (D:\.env\env02-LxMkY2WN)...
二 虚拟环境复制
1. 将env01目录中的 pipfile和pipflie.lock 复制到env02目录中
2. 进入目录env02,执行 pipenv install,会创建一个新的虚拟环境,并把 packages下的依赖安装到虚拟环境中,但dev-packages下的依赖不会自动安装
如果想把dev-packages中的依赖也复制过来,需要执行:如果需要执行 pipenv install --dev