Python虚拟环境

  1. 确认python解释器的路径

    [root@i-h0xe1oiq ~]# which python3.9
    /usr/local/bin/python3.9
    
  2. 安装python虚拟运行环境

    pip3.9 install virtualenv
    pip3.9 install virtualenvwrapper
    
  3. 创建目录来存放虚拟环境

    mkdir $HOME/.virtualenvs
    
  4. 在~/.bashrc中配置环境变量

    export WORKON_HOME=$HOME/.virtualenvs
    VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3.9
    source /usr/local/bin/virtualenvwrapper.sh
    
  5. 加载环境变量

    source ~/.bashrc
    
  6. 创建虚拟环境

    [root@i-h0xe1oiq data]# mkvirtualenv -p /usr/local/bin/python3.9 env39
    created virtual environment CPython3.9.7.final.0-64 in 1132ms
      creator CPython3Posix(dest=/root/.virtualenvs/env39, clear=False, no_vcs_ignore=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==21.2.4, setuptools==58.1.0, wheel==0.37.0
      activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
    virtualenvwrapper.user_scripts creating /root/.virtualenvs/env39/bin/predeactivate
    virtualenvwrapper.user_scripts creating /root/.virtualenvs/env39/bin/postdeactivate
    virtualenvwrapper.user_scripts creating /root/.virtualenvs/env39/bin/preactivate
    virtualenvwrapper.user_scripts creating /root/.virtualenvs/env39/bin/postactivate
    virtualenvwrapper.user_scripts creating /root/.virtualenvs/env39/bin/get_env_details
    (env39) [root@i-h0xe1oiq data]# 		#可以看到创建之后直接进入到了虚拟环境中
    
  7. 退出虚拟环境

    (env39) [root@i-h0xe1oiq data]# deactivate 
    [root@i-h0xe1oiq data]# 
    
  8. 查看虚拟环境

    [root@i-h0xe1oiq data]# lsvirtualenv 
    env39
    =====
    
    
  9. 进入虚拟环境

    [root@i-h0xe1oiq data]# workon env39
    (env39) [root@i-h0xe1oiq data]# 
    
  10. 删除虚拟环境

    [root@i-h0xe1oiq data]# rmvirtualenv env39
    Removing env39...
    
posted @ 2021-10-20 13:45  忘川的彼岸  阅读(118)  评论(0编辑  收藏  举报