Python_ virtualenv 环境管理工具 使用指南

其他参考文章:

1.Anacodna之conda与 virtualenv对比使用教程,创建虚拟环境

https://www.jianshu.com/p/ab93973286cc

2.virtualenv和conda的区别及windows 10上的部署

https://www.jianshu.com/p/e767e2910ee9

  本文主要讲解下 python 中的 virtualenv 工具的使用。 virtualenv 被称为 python 的3大神器之一。

virtualenv 主要解决以下问题,python 默认的包管理工具 pip 是从全局对包进行管理的。这样就会造成一个问题,系统的包有固定的版本,但是各个项目可能需要同一组件的不同版本,这该怎么去解决呢 ? 这里就可以依靠 virtualenv 帮我们解决问题。

 virtualenv 可以为一个应用创建一套“隔离”的Python运行环境。

使用 virtualenv 十分简单,只需要 pip 安装即可。

pip install virtualenv

下面介绍下基础的使用流程,假定我们现在的操作系统是 Linux 。 我们按照如下的步骤进行讲解,

1.创建一个新项目目录

2.创建虚拟环境目录,并进入虚拟环境

3.进入/退出 虚拟环境

4.virtualenv 详解

 

1.创建一个新项目目录

[root@localhost local]# mkdir virtualenv_tmp
[root@localhost local]# ll
total 0
drwxr-xr-x. 2 root root  6 Apr 11  2018 bin
drwxr-xr-x. 2 root root  6 Apr 11  2018 etc
drwxr-xr-x. 2 root root  6 Apr 11  2018 games
drwxr-xr-x. 2 root root  6 Apr 11  2018 include
drwxr-xr-x. 2 root root  6 Apr 11  2018 lib
drwxr-xr-x. 2 root root  6 Apr 11  2018 lib64
drwxr-xr-x. 2 root root  6 Apr 11  2018 libexec
drwxr-xr-x. 2 root root  6 Apr 11  2018 sbin
drwxr-xr-x. 5 root root 49 Dec  3  2018 share
drwxr-xr-x. 2 root root  6 Apr 11  2018 src
drwxr-xr-x. 3 root root 18 Jul 10 23:50 superset_dev
drwxr-xr-x. 2 root root  6 Jul 23 08:19 virtualenv_tmp

2.创建一个虚拟环境

 

进入新创建的目录。

执行虚拟环境初始化指令

virtualenv venv 

Tips: venv 为之后要存储该项目所依赖的包的存放目录, venv 总的来说是一个约定名字,尽量不要做修改。

[root@localhost local]# cd virtualenv_tmp/
[root@localhost virtualenv_tmp]# ll
total 0
[root@localhost virtualenv_tmp]# virtualenv venv
Using base prefix '/opt/python_home/Python37'
New python executable in /usr/local/virtualenv_tmp/venv/bin/python3.7
Also creating executable in /usr/local/virtualenv_tmp/venv/bin/python
Installing setuptools, pip, wheel...
done.

3.进入/退出 虚拟环境

  我们创建虚拟目录最主要的目的是针对项目,做包管理。接下来,我们就讲解下 如何进入虚拟环境,已经如何退出虚拟环境。

进入虚拟环境:

[root@localhost virtualenv_tmp]# source venv/bin/activate
(venv) [root@localhost virtualenv_tmp]# 

进入虚拟环境 在 路径上会有提示:

(venv) [root@localhost virtualenv_tmp]# 

这时候,我们再去执行 pip list ,可以看到是跟系统的 python 隔离开的,只有默认安装的几个包。

(venv) [root@localhost virtualenv_tmp]# pip list
Package    Version
---------- -------
pip        19.2   
setuptools 41.0.1 
wheel      0.33.4 

如何退出虚拟环境:

(venv) [root@localhost virtualenv_tmp]# deactivate 
[root@localhost virtualenv_tmp]# 

再去执行 pip list :

[root@localhost virtualenv_tmp]# pip list
Package    Version
---------- -------
pip        19.1.1 
setuptools 40.8.0 
virtualenv 16.6.1 
WARNING: You are using pip version 19.1.1, however version 19.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

可以看到两个环境是互不干扰的。

4.virtualenv 详解

只知道 virtualenv 指令的基本使用只是基础,我们再去了解下 指令的参数。

详细的使用文档,可以参考:

https://virtualenv.pypa.io/en/latest/reference/

virtualenv Command

Usage

virtualenv [OPTIONS] ENV_DIR

Where ENV_DIR is an absolute or relative path to a directory to create the virtual environment in.

Options

--version

show program’s version number and exit

-h--help

show this help message and exit

-v--verbose

Increase verbosity.

-q--quiet

Decrease verbosity.

-p PYTHON_EXE--python=PYTHON_EXE

The Python interpreter to use, e.g., --python=python2.5 will use the python2.5 interpreter to create the new environment. The default is the interpreter that virtualenv was installed with (like /usr/bin/python)

--clear

Clear out the non-root install and start from scratch.

--system-site-packages

Give the virtual environment access to the global site-packages.

--always-copy

Always copy files rather than symlinking.

--relocatable

Make an EXISTING virtualenv environment relocatable. This fixes up scripts and makes all .pth files relative.

--unzip-setuptools

Unzip Setuptools when installing it.

--no-setuptools

Do not install setuptools in the new virtualenv.

--no-pip

Do not install pip in the new virtualenv.

--no-wheel

Do not install wheel in the new virtualenv.

--extra-search-dir=DIR

Directory to look for setuptools/pip distributions in. This option can be specified multiple times.

--prompt=PROMPT

Provides an alternative prompt prefix for this environment.

--download

Download preinstalled packages from PyPI.

--no-download

Do not download preinstalled packages from PyPI.

--no-site-packages

DEPRECATED. Retained only for backward compatibility. Not having access to global site-packages is now the default behavior.

--distribute

--setuptools

Legacy; now have no effect. Before version 1.10 these could be used to choose whether to install Distribute or Setuptools into the created virtualenv. Distribute has now been merged into Setuptools, and the latter is always installed.

发布了520 篇原创文章 · 获赞 1146 · 访问量 283万+

猜你喜欢

转载自blog.csdn.net/u010003835/article/details/96333268
今日推荐