文章目录
pipenv教学
创建pipenv环境变量
通过设置PIPENV_VENV_IN_PROJECT = 1 的环境变量,让.venv 目录创建在要安装pipenv虚拟环境的目录,更加直观和方便管理。
以windows为例,创建虚拟环境的过程如下:
右键点击 “此电脑” , 选择属性 -> “高级"选项卡, 点击"环境变量” -> 用户环境变量中,创建 新的环境变量 -> 变量名:"
PIPENV_VENV_IN_PROJECT" , 变量值: 1 -> 重启电脑生效。
安装pipenv
windows下使用pip3 install pipenv
进行安装。
安装失败,可能会出现pip用不了的情况。
C:\Users\1>pip3 install pipenv
Traceback (most recent call last):
File "d:\python\program\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "d:\python\program\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "D:\Python\Program\Scripts\pip3.exe\__main__.py", line 4, in <module>
ModuleNotFoundError: No module named 'pip'
使用以下两个命令可以修复pip,然后再次尝试安装。
python -m ensurepip
python -m pip install --upgrade pip
创建虚拟环境
-
进入要创建虚拟环境的文件夹。
-
运行
pipenv install
来创建。文件夹中会多出来pipfile和pipfile.lock两个文件。这两个文件将会存放包的依赖关系。pipfile.lock是通过hash算法,将包名和版本、依赖关系生成哈希值,保证包的完整性。 -
开发完成提交项目的时候,需要将pipfile以及pipfile.lock文件一起提交。
收到后,使用
pipenv install --dev
来生成新的虚拟环境。
D:\project>pipenv install
Creating a virtualenv for this project...
Pipfile: D:\project\Pipfile
Using D:/Python/Program/python.exe (3.9.6) to create virtualenv...
[ ==] Creating virtual environment...created virtual environment CPython3.9.6.final.0-64 in 3040ms
creator CPython3Windows(dest=C:\Users\1\.virtualenvs\project-Vbf8eoYI, 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\1\AppData\Local\pypa\virtualenv)
added seed packages: pip==22.1.2, setuptools==62.6.0, wheel==0.37.1
activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
Successfully created virtual environment!
Virtualenv location: C:\Users\1\.virtualenvs\project-Vbf8eoYI
Creating a Pipfile for this project...
Pipfile.lock not found, creating...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Updated Pipfile.lock (16c839)!
Installing dependencies from Pipfile.lock (16c839)...
================================ 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和piplock文件,可以进入到这个文件夹后,直接运行 pipenv install
命令,pipenv会分析pipfile、pipfile.lock,将依赖包直接安装到虚拟环境中。
安装第三方包
在创建虚拟环境的目录中,使用 pipenv install 第三方包名
来安装。
安装的过程中,pipfile以及pipfile.lock文件也相应地更新。
D:\project>pipenv install requests
Installing requests...
Adding requests to Pipfile's [packages]...
Installation Succeeded
Pipfile.lock (16c839) out of date, updating to (fe5a22)...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Building requirements...
Resolving dependencies...
Success!
Updated Pipfile.lock (fe5a22)!
Installing dependencies from Pipfile.lock (fe5a22)...
================================ 0/0 - 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
显示依赖关系
pipenv graph
可以安装包的依赖关系
(.venv) D:\leo_pipenv>pipenv graph
pandas==1.4.3
- numpy [required: >=1.21.0, installed: 1.23.0]
- python-dateutil [required: >=2.8.1, installed: 2.8.2]
- six [required: >=1.5, installed: 1.16.0]
- pytz [required: >=2020.1, installed: 2022.1]
requests==2.28.1
- certifi [required: >=2017.4.17, installed: 2022.6.15]
- charset-normalizer [required: >=2,<3, installed: 2.1.0]
- idna [required: >=2.5,<4, installed: 3.3]
- urllib3 [required: >=1.21.1,<1.27, installed: 1.26.9]
包依赖 -> requirements文件
可以将pipenv的包依赖以requirements.txt文件的形式分享给其他人安装环境。
pipenv lock -r > requirements.txt
requirements文件 -> 包依赖
pipenv install -r requirements.txt
虚拟环境执行命令
pipenv run 命令
可以在虚拟环境中执行命令。
D:\leo_pipenv>pipenv run python hello.py
fidelio
D:\leo_pipenv>pipenv run pip list
Package Version
------------------ ---------
certifi 2022.6.15
charset-normalizer 2.1.0
idna 3.3
numpy 1.23.0
pandas 1.4.3
pip 22.1.2
python-dateutil 2.8.2
pytz 2022.1
requests 2.28.1
setuptools 62.6.0
six 1.16.0
urllib3 1.26.9
wheel 0.37.1
PIPENV Shell
安装在pipenv虚拟环境中的第三方包是无法在系统环境中使用的。
例如:
-
在 d:\project_pipenv 中创建了虚拟环境。
cd d:\leo_pipenv d:\leo_pipenv\pipenv install #创建虚拟环境
-
安装了requests包
d:\leo_pipenv\pipenv install requests
-
编写一个简单的脚本,import requests
test.py: import requests print('OK')
-
如果直接
python test.py
会提示找不到requests模块,这是因为模块被安装在虚拟环境中。需要使用
pip_env shell
进入虚拟环境的shell,然后在python test.py
D:\leo_pipenv>pipenv shell Launching subshell in virtual environment... Microsoft Windows [版本 10.0.19043.1766] (c) Microsoft Corporation。保留所有权利。 (.venv) D:\leo_pipenv>python hello.py fidelio
卸载虚拟环境
pipenv -rm 删除虚拟环境。
删除虚拟环境的本质是把.venv 目录删除了,但是pipfile和pipfile.lock仍然存在。