虚拟环境就是创建一个 隔离 的 python 环境,在这个环境里可以安装 python 所需的各种包,并使得这些包与 系统里的 python 不相干;
常用于版本管理;
本文以 linux 系统为例,windows 大同小异,掌握精髓
首先,安装 virtualenv
pip install virtualenv
安装完成后,virtualenv 出现在 python 的 bin 目录下
你可以建立 软连接 链接到 /usr/bin 下,也可以每次使用都切到 /usr/lib/python27/bin 下
创建虚拟环境
先看看 virtualenv 的参数
[root@hadoop10 python27]# bin/virtualenv --help Usage: virtualenv [OPTIONS] DEST_DIR 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=python3.5 will use the python3.5 interpreter to create the new environment. The default is the interpreter that virtualenv was installed with (/usr/bin/python) --clear Clear out the non-root install and start from scratch. --no-site-packages DEPRECATED. Retained only for backward compatibility. Not having access to global site-packages is now the default behavior. --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. --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 used multiple times. --download Download pre-installed packages from PyPI. --no-download, --never-download Do not download pre-installed packages from PyPI. --prompt=PROMPT Provides an alternative prompt prefix for this environment. --setuptools DEPRECATED. Retained only for backward compatibility. This option has no effect. --distribute DEPRECATED. Retained only for backward compatibility. This option has no effect. --unzip-setuptools DEPRECATED. Retained only for backward compatibility. This option has no effect.
用 virtualenv 命令创建虚拟环境,在 命令后 直接加 虚拟环境的名字 即可
bin/virtualenv pyspark-env
在当前路径创建虚拟环境,并且虚拟环境会自动安装 pip 之类的库
激活虚拟环境
虚拟环境需要先激活才能使用
[root@hadoop10 python27]# source pyspark-env/bin/activate (pyspark-env) [root@hadoop10 python27]# (pyspark-env) [root@hadoop10 python27]#
可以看到进入虚拟环境了,(pyspark-env),后面的命令都在 虚拟环境 中执行了;
也就是说你后续的所有操作都在 虚拟环境 中了,跟 系统的 python 隔绝了,随便折腾吧;
如你可以用 pip 安装包,各种操作与常规操作无异;
注意
虽然在创建 虚拟环境 的时候不包含任何 系统 py 的包,但是在 虚拟环境内 pip 安装包的时候,总是提示已经有了,且是 系统 py 内已经有了,无法安装,此时可如下操作强行安装
(pyspark-ml-env) [root@hadoop10 pyspark-ml-env]# pip install --ignore-installed numpy
关闭虚拟环境
折腾够了,就关了吧
(pyspark-env) [root@hadoop10 python27]# deactivate [root@hadoop10 python27]#
在虚拟环境中直接运行 deactivate ,就退出来了
删除虚拟环境
用完就删了吧,免得后面乱套了
rm -rf pyspark-env
还有一个工具 virtualenvwrapper,更强大,可以自己了解下
参考资料:
https://www.cnblogs.com/technologylife/p/6635631.html
https://www.jianshu.com/p/b8638271017d win 和 linux
https://www.liaoxuefeng.com/wiki/1016959663602400/1019273143120480 廖雪峰
https://virtualenv.pypa.io/en/latest/userguide/#making-environments-relocatable 官网