教程

https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/#:~:text=To%20create%20a%20virtual%20environment,virtualenv%20in%20the%20below%20commands.&text=The%20second%20argument%20is%20the,project%20and%20call%20it%20env%20.

依赖的 备份 与 还原(安装)

dump dependencies

1
pip freeze > requirements.txt

restore

1
pip install -r requirements.txt

创建工具

venv

python -m venv your_env_name

激活

  1. linux + zsh

    • 有效:

      • source /venv/bin/activate~
    • 无效:

      • ./venv/bin/activate
      • . ./venv/bin/activate

        • 有时有效,有时失效

特点

  • python 标准库
  • python3.3 以后

参数

  • your_env_name

    • 这是 env 的路径(实际上)

指定 python 版本

通过不同版本的 python 命令 创建不同版本的 env

  • win

    1
    
    d:/your_path/python37.exe -m venv your_env
  • linux

    1
    
    /your_path/python3.7 -m venv your_env

强制使用 copy, 不使用 symlink

--copies 参数

1
python -m venv venv --copies
修改已有 venv 为 copy 版本方法
  1. 删除 python 软连接

    1
    
    rm venv/bin/python*
  2. 升级 python 版本

    1
    
    /path/to/python -m venv venv --copies --upgrade
    • --copies --upgrade

强制使用 symlink

--symlinks

1
python -m venv venv --symlinks

升级或替换 python 版本

--upgrade 参数

virtualenv

virtualenvwrapper

激活

1
2
./venv/Script/activate
source ./venv/bin/activate

推出

  • deactivate

    • 这个是当前 virtualenv 的 deactivate 命令