第三方插件 virtualenv

安装

pip install virtualenv

创建虚拟环境

mkdir newpro
cd newpro
virtualenv venv 

# venv 是虚拟环境的名字

激活虚拟环境

# Linux / OS X 激活
venv/bin/activate

# Windows 上激活,注意斜杠和反斜杠
venv\scripts\activate

退出

deactivate

python3版本使用内置组件创建虚拟环境

创建虚拟环境

python -m venv virt
# virt 是虚拟环境的名字

激活虚拟环境

# Linux / OS X 激活
virt/scripts/activate

# Windows 上激活,注意斜杠和反斜杠
virt\scripts\activate

退出

deactivate

python环境迁移相关命令

pip freeze

会输出所有虚拟环境中的包,可以直接导出到requirements.txt

image-20230313110623573
image-20230313110623573

完整命令 pip freeze > requirements.txt

安装python工作依赖包

pip install -r requirements.txt

更换pip源到国内源 1

pip 简单使用

# 安装包
pip install some-package

# 卸载包
pip uninstall some-package

# 升级pip
python -m pip install --upgrade pip

# 如网络较差,可使用临时镜像升级
python -m pip install -i https://mirrors.bfsu.edu.cn/pypi/web/simple --upgrade pip

临时使用

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

设置默认

# 升级pip
python -m pip install --upgrade pip
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

设置多个镜像源

pip config set global.extra-index-url "<url1> <url2>..."
pip config set global.extra-index-url "https://mirror.lzu.edu.cn/pypi/web/simple  https://pypi.org/simple"
# https://mirror.lzu.edu.cn/pypi/web/simple/
# https://pypi.org/simple/