virtualenv和virtualenvwrapper介绍、安装和使用

环境:ubuntu:18.04

 

virtualenv

安装命令 :

pip3 install virtualenv

创建目录:

mkdir myproject
cd myproject/

创建一个python独立的环境

virtualenv --no-site-packages venv

指定 python 环境

# /user/bin/python3 为 指定的Python路径
virtualenv -p /user/bin/python3 venv

注意:

        virtualenv    告诉liunx是virtualenv服务,

        参数--no-site-packages    已经安装到系统中python环境的所有第三方库都不会复制,这样是为了得到一个没有任何第三方库的python环境

virtualenv的参数

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
                        (/usr/bin/python3)
  --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.
  --unzip-setuptools    Unzip Setuptools when installing it.
  --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 preinstalled packages from PyPI.
  --no-download, --never-download
                        Do not download preinstalled 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.

启动virtualenv中python

        新建的python环境保存到当前目录下venv中

source venv/bin/activate

退出virtualenv环境

deactivate

virtualenvwrapper

virtualenv 可以创建一个虚拟的独立 Python 环境,但是 virtualenv 创建的环境相对分散不便于管理, 而Virtualenvwrapper 提供了一系列命令使得和虚拟环境工作变得轻松许多, 相对于virtualenv, Virtualenvwrapper有以下几个优点:

virtualenvwrapper是virtualenv的扩展管理包,用于更方便管理虚拟环境,它可以做:

优点:

将所有虚拟环境整合在一个目录下

管理(新增,删除,复制)虚拟环境,方便切换虚拟环境

安装

# 先看有无 python3 环境
pip3 -V

# 如果没有,则先安装 
sudo apt-get install python3-pip

# 安装 virtualenvwrapper
pip3 install virtualenvwrapper   

设置WORK_HOME环境变量

mkdir $HOME/.virtualenvs

配置 

打开 ~/.bashrc 或者 ~/.zshrc,加入

export WORKON_HOME=$HOME/.virtualenvs
# virtualenvwrapper 默认的python版本,这里默认选择 python3
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 
source ~/.local/bin/virtualenvwrapper.sh
export VIRTUALENVWRAPPER_VIRTUALENV=~/.local/bin/virtualenv

注意:ubuntu18安装的virtualenvwrapper.sh在~/.local/bin/virtualenvwrapper.sh路径,如果不确定,可以先通过 find / -name virtualenvwrapper.sh 来找

创建虚拟环境

# 选择默认 python 版本创建虚拟环境
mkvirtualenv pyenv

# 指定 Python版本创建虚拟环境
mkvirtualenv --python=/usr/bin/python3 venv

使用方法

所有的命令可使用:virtualenvwrapper --help 进行查看,这里列出几个常用的:

创建基本环境:mkvirtualenv [环境名]

删除环境:rmvirtualenv [环境名]

激活环境:workon [环境名]

退出环境:deactivate

列出所有环境:workon 或者 lsvirtualenv -b

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值