python-centos7下安装python3虚拟环境

1.创建python的虚拟环境

[root@python ~]# mkdir -p /virtualenvs

  • 安装虚拟环境virtualenv

    [root@python ~]#   pip3 install virtualenv -i https://pypi.tuna.tsinghua.edu.cn/simple/     
    
  • 安装虚拟环境扩展包

    [root@python   virtualenvs]# pip3 install virtualenvwrapper    -i https://pypi.tuna.tsinghua.edu.cn/simple/      
    

2. 修改用户家目录下的配置文件.bashrc,添加如下内容

  • 修改配置文件

    #查找一下安装的虚拟环境扩展包文件
    [root@python virtualenvs]# find / -name virtualenvwrapper.sh
    /usr/local/python3/bin/virtualenvwrapper.sh
    
    #配置环境变量
    [root@python virtualenvs]# vim /etc/profile
    [root@python virtualenvs]# tail -n 4 /etc/profile
    export WORKON_HOME=/virtualenvs
    export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 
    source /usr/local/python3/bin/virtualenvwrapper.sh
    
    
  • 执行环境文件,重新加载文件,使配置配置文件生效

    [root@python virtualenvs]# source /etc/profile
    virtualenvwrapper.user_scripts creating /virtualenvs/premkproject
    virtualenvwrapper.user_scripts creating /virtualenvs/postmkproject
    virtualenvwrapper.user_scripts creating /virtualenvs/initialize
    virtualenvwrapper.user_scripts creating /virtualenvs/premkvirtualenv
    virtualenvwrapper.user_scripts creating /virtualenvs/postmkvirtualenv
    virtualenvwrapper.user_scripts creating /virtualenvs/prermvirtualenv
    virtualenvwrapper.user_scripts creating /virtualenvs/postrmvirtualenv
    virtualenvwrapper.user_scripts creating /virtualenvs/predeactivate
    virtualenvwrapper.user_scripts creating /virtualenvs/postdeactivate
    virtualenvwrapper.user_scripts creating /virtualenvs/preactivate
    virtualenvwrapper.user_scripts creating /virtualenvs/postactivate
    virtualenvwrapper.user_scripts creating /virtualenvs/get_env_details
    

3. 创建python3虚拟环境的命令如下

  • 创建虚拟环境,mkvirtualenv “虚拟环境名称”

    • 创建命令:

      mkvirtualenv 环境名称   
      
  • 执行创建虚拟环境命令

    [root@python virtualenvs]# mkvirtualenv  python_3.8_base
    which: no virtualenv in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
    ERROR: virtualenvwrapper could not find virtualenv in your path
    
    #提示报错
    我们需要链接一下virtualenv
    [root@python virtualenvs]# ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv
    
    
  • 重新执行创建虚拟环境命令

    root@python virtualenvs]# mkvirtualenv  python_3.8_base
    created virtual environment CPython3.7.4.final.0-64 in 969ms
      creator CPython3Posix(dest=/virtualenvs/python_3.8_base, clear=False, global=False)
      seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
        added seed packages: pip==20.1.1, setuptools==49.2.0, wheel==0.34.2
      activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
    virtualenvwrapper.user_scripts creating /virtualenvs/python_3.8_base/bin/predeactivate
    virtualenvwrapper.user_scripts creating /virtualenvs/python_3.8_base/bin/postdeactivate
    virtualenvwrapper.user_scripts creating /virtualenvs/python_3.8_base/bin/preactivate
    virtualenvwrapper.user_scripts creating /virtualenvs/python_3.8_base/bin/postactivate
    virtualenvwrapper.user_scripts creating /virtualenvs/python_3.8_base/bin/get_env_details
    
  • 切换虚拟环境:使用"workon 虚拟环境名称" 切换虚拟环境

    • 命令示例:

      (Flask_cmdb)   [root@python virtualenvs]# workon python_3.8_base   
      (python_3.8_base)  [root@python virtualenvs]# 
      
  • 退出虚拟环境:使用 “deactivate”命令退出虚拟环境

    • 命令示例

      (python_3.8_base)   [root@python virtualenvs]# deactivate   
      
  • 删除虚拟环境:使用“rmvirtualenv 虚拟环境名称”

    • 命令示例:

      [root@python   virtualenvs]# rmvirtualenv python_3.8_base   Removing   python_3.8_base...  
      
  • 改为阿里云的pip库

    [root@python virtualenvs]# mkdir -p ~/.pip
    [root@python virtualenvs]# vim ~/.pip/pip.conf
    [root@python virtualenvs]# cat ~/.pip/pip.conf 
    [global]
    index-url = http://mirrors.aliyun.com/pypi/simple/
    
    [install]
    trusted-host=mirrors.aliyun.com
    
    
posted @ 2021-04-29 10:21  七月流星雨  阅读(258)  评论(0编辑  收藏  举报