zoukankan      html  css  js  c++  java
  • python笔记:学习设置Python虚拟环境+配置 virtualenvwarpper+创建Python3.6的虚拟环境+安装numpy

    虚拟环境它是一个虚拟化,从电脑独立开辟出来的环境。就是借助虚拟机docker来把一部分内容独立出来,我们把这部分独立出来的东西称作“容器”,在这个容器中,我们可以只安装我们需要的依赖包,各个容器之间互相隔离,互不影响。在什么环境下我们需要使用到虚拟环境呢?比如,我们接手一个项目的时候,这个项目之前是在Python2.7下运行的,而你接手的项目需要在python3环境中运行,想想就应该知道,如果不使用虚拟环境,这这两个项目可能无法同时使用,使用python3则公司之前的项目可能无法运行,反正则新项目运行有麻烦。而如果虚拟环境可以分别为这两个项目配置不同的运行环境,这样两个项目就可以同时运行。

    Ubuntu系统默认的Python是2.7,为了使多个Python版本共存,我们使用virtualenv/virtualenvwrapper来管理不同的Python版本和相应的软件包。
    virtualenvwrapper是virtualenv的扩展,使得管理虚拟环境更加方便。

    sudo pip install virtualenv virtualenvwrapper

    配置 virtualenvwarpper

    默认virtualenvwrapper安装在/usr/local/bin下面,实际上需要运行virtualenvwrapper.sh文件才行;所以需要先进行配置一下:

    创建虚拟环境管理目录: mkdir $HOME/.virtualenvs
    在~/.bashrc中添加行:

    export VIRTUALENV_USE_DISTRIBUTE=1        #  总是使用 pip/distribute       export WORKON_HOME=$HOME/.virtualenvs   # 所有虚拟环境存储的目录             
    if [ -e $HOME/.local/bin/virtualenvwrapper.sh ];then
       source $HOME/.local/bin/virtualenvwrapper.sh 
    else if [ -e /usr/local/bin/virtualenvwrapper.sh ];then
             source /usr/local/bin/virtualenvwrapper.sh
        fi                      
    fi                                                                                                                                                          
    export PIP_VIRTUALENV_BASE=$WORKON_HOME                                     export PIP_RESPECT_VIRTUALENV=true
    

      

    启动 virtualenvwrapper: source ~/.bashrc  得到如下输出:

    veelion@gtx:~/opencv$ source ~/.bashrc
    virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/premkproject
    virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/postmkproject
    virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/initialize
    virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/premkvirtualenv
    virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/postmkvirtualenv
    virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/prermvirtualenv
    virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/postrmvirtualenv
    virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/predeactivate
    virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/postdeactivate
    virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/preactivate
    virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/postactivate
    virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/get_env_details
    

      

    使用方法

    使用virtualenvwrapper —help 查看所有命令,常用的有:

    • 创建基本环境:mkvirtualenv [环境名]
    • 删除环境:rmvirtualenv [环境名]
    • 激活环境:workon [环境名]
    • 退出环境:deactivate
    • 列出所有环境:workon 或者 lsvirtualenv -b

    创建Python3.6的虚拟环境

    veelion@gtx:~$ mkvirtualenv -p python3.6 py3.6
    Running virtualenv with interpreter /usr/bin/python3.6
    Using base prefix '/usr'
    New python executable in /home/veelion/.virtualenvs/py3.6/bin/python3.6
    Also creating executable in /home/veelion/.virtualenvs/py3.6/bin/python
    Installing setuptools, pip, wheel...done.
    virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/py3.6/bin/predeactivate
    virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/py3.6/bin/postdeactivate
    virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/py3.6/bin/preactivate
    virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/py3.6/bin/postactivate
    virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/py3.6/bin/get_env_details

    安装numpy

    workon py3.6
    pip install numpy
    

    文章来自于我的学习笔记www.yuanrenxue.com

  • 相关阅读:
    Mysql登录错误:ERROR 1045 (28000): Plugin caching_sha2_password could not be loaded
    Docker配置LNMP环境
    Docker安装mysqli扩展和gd扩展
    Docker常用命令
    Ubuntu常用命令
    单例模式的优缺点和使用场景
    ABP 多租户数据共享
    ABP Core 后台Angular+Ng-Zorro 图片上传
    ERROR Error: If ngModel is used within a form tag, either the name attribute must be set or the form control must be defined as 'standalone' in ngModelOptions.
    AbpCore 执行迁移文件生成数据库报错 Could not find root folder of the web project!
  • 原文地址:https://www.cnblogs.com/amiza/p/10337123.html
Copyright © 2011-2022 走看看