安装使用pipenv

本文最后更新于 2024年5月16日。

安装pipenv

pip install pipenv -i https://pypi.tuna.tsinghua.edu.cn/simple

提示

Defaulting to user installation because normal site-packages is not writeable
WARNING: The script virtualenv.exe is installed in 'C:\Users\weiyo\AppData\Roaming\Python\Python39\Scripts' which is not on PATH.

把提示加入系统环境变量才可以正常使用pipenv

(base) PS C:\Users\weiyo> pipenv
Usage: pipenv [OPTIONS] COMMAND [ARGS]...
C:\Users\weiyo>pip install pipenv -i https://pypi.tuna.tsinghua.edu.cn/simple
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pipenv
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/11/02/c066477d8bb6dba923f836b5e58b3fe1f9ca0196714d175967e5a69f87f4/pipenv-2022.11.5-py2.py3-none-any.whl (2.9 MB)
WARNING: The script virtualenv-clone.exe is installed in '
' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The script virtualenv.exe is installed in 'C:\Users\weiyo\AppData\Roaming\Python\Python39\Scripts' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The scripts pipenv-resolver.exe and pipenv.exe are installed in 'C:\Users\weiyo\AppData\Roaming\Python\Python39\Scripts' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed distlib-0.3.6 pipenv-2022.11.5 platformdirs-2.5.3 virtualenv-20.16.6 virtualenv-clone-0.5.7

准备使用pipenv,结果在创建虚拟环境使总是报错,权限问题,错误码13

(base) PS E:\pipenv1> pipenv shell
Creating a virtualenv for this project...
Pipfile: E:\pipenv1\Pipfile
Using C:/ProgramData/Anaconda3/python.exe (3.9.12) to create virtualenv...
[====] Creating virtual environment...Actual environment location may have moved due to redirects, links or junctions.
Requested location: "C:\Users\weiyo\.virtualenvs\pipenv1-BX4p2dhi\Scripts\python.exe"
Actual location:    "C:\ProgramData\Anaconda3\python.exe"
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\weiyo\\.virtualenvs\\pipenv1-BX4p2dhi\\Scripts\\python.exe'
Failed creating virtual environment
[pipenv.exceptions.VirtualenvCreationException]:
Failed to create virtual environment.

pipenv总是报错, 下面的解决方法无效

[pipenv.exceptions.VirtualenvCreationException]:
Failed to create virtual environment.
# 解决方法如下:
pipenv 后边增加版本号即可。
## pipenv --python 3.7

Pipenv的作用

可以利用pipenv来实现同时管理项目中的python虚拟环境和相关包依赖。

使用步骤

  1. cmd中输入命令 pip install pipenv 安装pipenv
  2. cmd切换路径到需要建立虚拟环境的项目目录下
  3. 输入命令 pipenv install 开始安装虚拟环境
  4. 安装完毕后输入命令 pipenv shell 进入虚拟环境

Pipfile文件

  • 虚拟环境安装完毕后项目目录中将创建Pipfile/Pipfile.lock两个文件
  • Pipfile:用于保存项目的python版本、依赖包等相关信息
  • Pipfile.lock:用于对Pipfile的锁定
  • Pipfile文件可以单独移放到其他项目内,用于项目虚拟环境的建立和依赖包的安装

常用命令

  • pipenv install:

  • 若项目目录中虚拟环境未创建且无Pipfile文件,将安装虚拟环境并创建Pipfile文件

  • 若项目目录中虚拟环境未创建且有Pipfile文件,将根据Pipfile文件来安装相应python版本和依赖包

  • 若项目目录中虚拟环境已创建且有Pipfile文件,将根据Pipfile文件来安装依赖包

  • pipenv install xx::安装python包

  • pipenv uninstall xx::卸载python包

  • pipenv shell:进入虚拟环境(项目目录下)

  • exit:退出虚拟环境

  • pipenv graph:显示包依赖关系

  • pipenv –venv:显示虚拟环境安装路径