Python 虚拟环境创建以及管理


本文首发于个人博客: https://www.xerrors.fun/python-venv/


Python 作为一个计科的学生经常使用的一个工具会用在不同的地方,比如科学计算、Flask 后端、机器学习、CTF 等等,但是各种库安装在一起可能会出现冲突的问题,所以一个理想的办法就是使用虚拟环境的 Python 环境。

Python 2 创建虚拟环境

::: tips

Python2 即将弃用

DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won’t be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support

:::

如果默认的 Python 版本就是 Python2 的话就可以使用 virtualenv 直接安装:

# 安装
pip install virtualenv

# 使用
virtualenv name

# 激活
source name/bin/activate

# 关闭
deactivate

Python3 创建虚拟环境

Python3 依然是可以使用上面的方法去创建虚拟环境,但是,Python3.6 以上的版本都自带了 venv 所以,可以直接使用另外一个命令:

# 有时需要安装这个
apt-get install python3-venv

# 创建
python3 -m venv name

# 激活
source ctf/bin/activate

# 关闭
deactivate

猜你喜欢

转载自blog.csdn.net/jaykm/article/details/103898526