虚拟环境
-
作用
解决相同库不同版本问题
-
安装软件
-
方式一
-
安装虚拟环境的命令
sudo pip install virtualenv sudo pip install virtualenvwrapper
-
配置环境变量
# 1、创建目录用来存放虚拟环境 mkdir $HOME/.virtualenvs # 2、打开~/.bashrc文件,并添加如下: export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh # 3、运行 source ~/.bashrc
-
-
方式二(安装miniconda)
-
在linxu中通过该链接下载得到脚本
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda-3.16.0-Linux-x86_64.sh
-
执行脚本,进行安装, 在安装过程中,会要你进行几次选择
/bin/bash Miniconda-3.16.0-Linux-x86_64.sh Do you approve the license terms? [yes|no] [no] >>> yes Miniconda will now be installed into this location: /root/miniconda - Press ENTER to confirm the location - Press CTRL-C to abort the installation - Or specify a different location below [/root/miniconda] >>> /miniconda Do you wish the installer to prepend the Miniconda install location to PATH in your /root/.bashrc ? [yes|no] [no] >>> no You may wish to edit your .bashrc or prepend the Miniconda install location: $ export PATH=/miniconda/bin:$PATH Thank you for installing Miniconda!
-
编辑
~/.bash_profile
, 参照第3步执行命令后的提示,把export PATH=/miniconda/bin:$PATH
添加到~/.bash_profile
文件末尾,最后执行source ~/.bash_profile
让其生效。[root@localhost ~]# vim ~/.bash_profile [root@localhost ~]# cat ~/.bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/bin export PATH export PATH=/miniconda/bin:$PATH [root@localhost ~]# source ~/.bash_profile
-
测试
conda -V
查看conda版本conda -V
-
-
-
创建虚拟环境
-
方式一
-
创建虚拟环境的命令
# 不指定python版本 mkvirtualenv 虚拟环境名称 mkvirtualenv py # 在python3中,创建虚拟环境 mkvirtualenv -p python3 虚拟环境名称 mkvirtualenv -p python3 py3
-
查看虚拟环境的命令
workon
-
使用虚拟环境的命令
workon 虚拟环境名称 例 :使用py3的虚拟环境 workon py3
-
退出虚拟环境的命令
deactivate
-
删除虚拟环境的命令
rmvirtualenv 虚拟环境名称 例 :删除虚拟环境py3 先退出:deactivate 再删除:rmvirtualenv py3
-
-
方式二(使用conda)
-
创建虚拟环境的命令
# 不指定python版本 conda create 虚拟环境名称 conda create py # 在python3中,创建虚拟环境 conda create -n 虚拟环境名称 python=3.6 conda create -n py36 python=3.6
-
查看虚拟环境的命令
conda list
-
使用虚拟环境的命令
source activate 虚拟环境名称 例 :使用py3的虚拟环境 source activate py36
-
退出虚拟环境的命令
deactivate
-
删除虚拟环境的命令
conda rm 虚拟环境名称 例 :删除虚拟环境py3 先退出:deactivate 再删除:conda rm py3
-
-
-
在虚拟环境中安装工具包
# pip pip install # 安装依赖包 pip uninstall # 卸载依赖包 pip list # 查看已安装的依赖包 # 因为pip , wheel , setuptools 等包,是自带的而无法(un)install的。 # 考虑到pip freeze的用途,所以这些包并没有显示。 # 如果一定要用pip freeze来显示所有包,可以加上参数-all,即pip freeze -all pip freeze # 冻结当前环境的依赖包 例 : 安装django-1.11.11的包 pip install django==1.11.11 # 输出本地包环境至文件 pip freeze | tee requirements.txt pip freeze > requirements.txt # 安装requirements.txt 所有依赖包 pip install -r ./requirements.txt