【Python虚拟环境】pipenv学习指南

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

创建虚拟环境

  1. 进入要创建虚拟环境的文件夹。

  2. 运行pipenv install 来创建。文件夹中会多出来pipfile和pipfile.lock两个文件。这两个文件将会存放包的依赖关系。pipfile.lock是通过hash算法,将包名和版本、依赖关系生成哈希值,保证包的完整性。

  3. 开发完成提交项目的时候,需要将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仍然存在。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咸鱼他饼子哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值