pipenv环境搭建

发布时间 2023-04-15 09:59:38作者: 迦洛德影歌

官方网站

安装

安装pipenv

pipenv install --user pip pipenv --upgrade
pipenv --version

使用示例

mkdir pipenv_test
cd pipenv_test
pipenv_test$ pipenv --python 3
Warning: the environment variable LANG is not set!
We recommend setting this in ~/.profile (or equivalent) for proper expected behavior.
Creating a virtualenv for this project...
Pipfile: /home/book/cmakeCookBook/pipenv_test/Pipfile
Using /usr/bin/python3.8 (3.8.10) to create virtualenv...
⠴ Creating virtual environment...created virtual environment CPython3.8.10.final.0-64 in 854ms
  creator CPython3Posix(dest=/home/book/.local/share/virtualenvs/pipenv_test--n-V7Fvh, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/book/.local/share/virtualenv)
    added seed packages: pip==23.0.1, setuptools==67.4.0, wheel==0.38.4
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

✔ Successfully created virtual environment!
  • 设置LANG环境变量
sudo apt install -y locales
sudo locale-gen en_US.UTF-8
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
  • 进入虚拟环境
pipenv shell
  • 退出虚拟环境
exit
  • 查看虚拟环境管理目录
pipenv --venv
/home/book/.local/share/virtualenvs/pipenv_test--n-V7Fvh
  • 查看虚拟环境依赖库
pipenv graph
Flask==2.2.3
  - click [required: >=8.0, installed: 8.1.3]
  - importlib-metadata [required: >=3.6.0, installed: 6.1.0]
    - zipp [required: >=0.5, installed: 3.15.0]
  - itsdangerous [required: >=2.0, installed: 2.1.2]
  - Jinja2 [required: >=3.0, installed: 3.1.2]
    - MarkupSafe [required: >=2.0, installed: 2.1.2]
  - Werkzeug [required: >=2.2.2, installed: 2.2.3]
    - MarkupSafe [required: >=2.1.1, installed: 2.1.2]
  • 安装开发依赖包
pipenv_test$ pipenv install --dev requests
Installing requests...
Resolving requests...
Installing...
Adding requests to Pipfile's [dev-packages] ...
✔ Installation Succeeded
Pipfile.lock (7ec6df) out of date, updating to (9a3421)...
Locking [packages] dependencies...
Building requirements...
Resolving dependencies...
✔ Success!
Locking [dev-packages] dependencies...
Building requirements...
Resolving dependencies...
✔ Success!
Updated Pipfile.lock (bf91257fb93de95fce0cd5b2229fc0815cdb7d0ab021f1ab3cb8fdc53c9a3421)!
Installing dependencies from Pipfile.lock (9a3421)...
Installing dependencies from Pipfile.lock (9a3421)...
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
  • 宿主机环境运行虚拟环境命令
pipenv_test$ pipenv run pip list
Package            Version
------------------ ---------
certifi            2022.12.7
charset-normalizer 3.1.0
click              8.1.3
Flask              2.2.3
idna               3.4
importlib-metadata 6.1.0
itsdangerous       2.1.2
Jinja2             3.1.2
MarkupSafe         2.1.2
pip                23.0.1
requests           2.28.2
setuptools         67.4.0
urllib3            1.26.15
Werkzeug           2.2.3
wheel              0.38.4
zipp               3.15.0
  • python虚拟环境共享或迁移
mkdir other_team
cp ../pipenv_test/Pipfile .
  • 安装工作库和开发库
pipenv install --dev
Creating a virtualenv for this project...
Pipfile: /home/book/cmakeCookBook/other_team/Pipfile
Using /usr/bin/python3.8 (3.8.10) to create virtualenv...
⠙ Creating virtual environment...created virtual environment CPython3.8.10.final.0-64 in 111ms
  creator CPython3Posix(dest=/home/book/.local/share/virtualenvs/other_team-oftCZxRe, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/book/.local/share/virtualenv)
    added seed packages: pip==23.0.1, setuptools==67.4.0, wheel==0.38.4
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

⠹ Creating virtual environment...✔ Successfully created virtual environment!
Virtualenv location: /home/book/.local/share/virtualenvs/other_team-oftCZxRe
Pipfile.lock not found, creating...
Locking [packages] dependencies...
Building requirements...
Resolving dependencies...
✔ Success!
Locking [dev-packages] dependencies...
Building requirements...
Resolving dependencies...
✔ Success!
Updated Pipfile.lock (bf91257fb93de95fce0cd5b2229fc0815cdb7d0ab021f1ab3cb8fdc53c9a3421)!
Installing dependencies from Pipfile.lock (9a3421)...
Installing dependencies from Pipfile.lock (9a3421)...
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
  • 进入迁移后的虚拟环境
pipenv shell
pip list
Package            Version
------------------ ---------
certifi            2022.12.7
charset-normalizer 3.1.0
click              8.1.3
Flask              2.2.3
idna               3.4
importlib-metadata 6.1.0
itsdangerous       2.1.2
Jinja2             3.1.2
MarkupSafe         2.1.2
pip                23.0.1
requests           2.28.2
setuptools         67.4.0
urllib3            1.26.15
Werkzeug           2.2.3
wheel              0.38.4
zipp               3.15.0
exit
  • 删除虚拟环境
pipenv --rm

运行示例代码

cd pipenv_test
pipenv run python main.py

Pipfile添加脚本

tee -a Pipfile << 'EOF'
[scripts]
start = "python main.py"
test = "pytest"
list = "pip list"
  • 运行脚本
pipenv rum start
pipenv run list