靳闯博客 记录是一种习惯,分享是一种态度
  • 波浪
  • 波浪
  • 波浪
  • 波浪
centos7 安装使用virtualenvwrapper
发表于: | 分类: 技术积累 | 标签: Python Linux | 评论:0 | 阅读: 1180

virtualenvwrapper是Ian Bicking的virtualenv工具的一组扩展。扩展包括用于创建和删除虚拟环境以及管理开发工作流程的包装程序,从而使一次处理多个项目变得更加容易,而不会在其依赖项中引入冲突。


特征
1、在一个地方组织所有虚拟环境
2、包装程序,用于管理您的虚拟环境(创建,删除,复制)
3、使用单个命令在环境之间切换
4、使用虚拟环境作为参数的命令的制表符完成
5、所有操作的用户可配置钩子
6、用于创建更多可共享扩展的插件系统


如果用virtualenv 每次切换虚拟环境就要source之前虚拟环境bin目录,如果有很多就需要都记住这些目录,所以就用virtualenvwrapper来统一管理,而且省去了每次都要切换的麻烦


安装使用(python2版本)

#centos默认没有pip命令,需要安装
[root@localhost ~]# wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost ~]# yum install python-pip -y
[root@localhost ~]# pip install --upgrade pip

#安装virtualenvwrapper
[root@localhost ~]# pip install virtualenvwrapper

#确认下文件路径
[root@localhost ~]# which python
/usr/bin/python
[root@localhost ~]# find / -name virtualenvwrapper.sh
/usr/bin/virtualenvwrapper.sh

#添加配置
[root@localhost ~]# vim ~/.bashrc
······
#添加配置
WORKON_HOME=~/.virtualenvs  #虚拟环境统一管理目录
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python #虚拟环境对应的python版本
source /usr/bin/virtualenvwrapper.sh #执行virtualenvwrapper安装脚本

#生效配置
[root@localhost ~]# source ~/.bashrc 
virtualenvwrapper.user_scripts creating /root/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /root/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/get_env_details
virtualenvwrapper.user_scripts creating /root/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postmkproject

#创建虚拟环境
[root@localhost ~]# mkvirtualenv test
created virtual environment CPython2.7.5.final.0-64 in 695ms
  creator CPython2Posix(dest=/root/.virtualenvs/test, clear=False, global=False)
  seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/root/.local/share/virtualenv/seed-app-data/v1.0.1)
  activators PythonActivator,CShellActivator,FishActivator,PowerShellActivator,BashActivator
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/get_env_details
(test) [root@localhost ~]# deactivate 

[root@localhost ~]# mkvirtualenv test-1
created virtual environment CPython2.7.5.final.0-64 in 379ms
  creator CPython2Posix(dest=/root/.virtualenvs/test-1, clear=False, global=False)
  seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/root/.local/share/virtualenv/seed-app-data/v1.0.1)
  activators PythonActivator,CShellActivator,FishActivator,PowerShellActivator,BashActivator
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test-1/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test-1/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test-1/bin/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test-1/bin/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test-1/bin/get_env_details
(test-1) [root@localhost ~]# deactivate

#切换虚拟环境
[root@localhost ~]# workon test
(test) [root@localhost ~]# deactivate 
[root@localhost ~]# workon test-1
(test-1) [root@localhost ~]# deactivate 

virtual.png


安装使用(python3版本 Linux下Python3.x版本安装

#安装virtualenvwrapper
[root@localhost ~]# pip3 install --upgrade pip
[root@localhost ~]# pip3 install virtualenvwrapper

#确认下文件路径
[root@localhost ~]# which python3
/usr/local/python38/bin/python3
[root@localhost ~]# find / -name virtualenvwrapper.sh
/usr/local/python38/bin/virtualenvwrapper.sh


#添加配置
[root@localhost ~]# vim ~/.bashrc
······
#添加配置
WORKON_HOME=~/.virtualenvs  #虚拟环境统一管理目录
VIRTUALENVWRAPPER_PYTHON=/usr/local/python38/bin/python3 #虚拟环境对应的python版本
source /usr/local/python38/bin/virtualenvwrapper.sh #执行virtualenvwrapper安装脚本

#生效配置
[root@localhost ~]# source ~/.bashrc
virtualenvwrapper.user_scripts creating /root/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /root/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /root/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/get_env_details

创建虚拟环境

[root@localhost ~]# mkvirtualenv test
created virtual environment CPython3.8.2.final.0-64 in 343ms
  creator CPython3Posix(dest=/root/.virtualenvs/test, clear=False, global=False)
  seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/root/.local/share/virtualenv/seed-app-data/v1.0.1)
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/get_env_details
(test) [root@localhost ~]# deactivate 
[root@localhost ~]# mkvirtualenv test-1
created virtual environment CPython3.8.2.final.0-64 in 339ms
  creator CPython3Posix(dest=/root/.virtualenvs/test-1, clear=False, global=False)
  seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/root/.local/share/virtualenv/seed-app-data/v1.0.1)
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test-1/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test-1/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test-1/bin/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test-1/bin/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test-1/bin/get_env_details
(test-1) [root@localhost ~]# deactivate

#切换虚拟环境
[root@localhost ~]# workon test
(test) [root@localhost ~]# deactivate 
[root@localhost ~]# workon test-1
(test-1) [root@localhost ~]# deactivate 
[root@localhost ~]# 

virtual3.png


一些命令操作

#创建并激活虚拟环境
mkvirtualenv

#切换虚拟环境
workon
#退出虚拟环境
deactivate

#删除虚拟环境
rmvirtualenv

#查看所有的虚拟环境
lsvirtualenv


↓↓↓进入虚拟环境后的操作 ↓↓↓
#切换到虚拟环境的目录
cdvirtualenv

#切换到虚拟环境的site-packages目录
cdsitepackages

#查看虚拟环境site-packages目录的文件
lssitepackages

商业转载请联系作者获得授权,非商业转载请注明出处,谢谢合作。


如果这篇文章帮助到了你,我感到十分荣幸!


📬 评论留言

icon_mrgreen.pngicon_neutral.pngicon_twisted.pngicon_arrow.pngicon_eek.pngicon_smile.pngicon_confused.pngicon_cool.pngicon_evil.pngicon_biggrin.pngicon_idea.pngicon_redface.pngicon_razz.pngicon_rolleyes.pngicon_wink.pngicon_cry.pngicon_surprised.pngicon_lol.pngicon_mad.pngicon_sad.pngicon_exclaim.pngicon_question.png2018new_aini_org.png2018new_baobao_thumb.png2018new_erha_org.png2018new_kuxiao_thumb.png2018new_yun_thumb.png2018new_hufen_thumb.png2018new_gui_org.png2018new_xiaoerbuyu_org.png2018new_heixian_thumb.png2018new_wabi_thumb.png2018new_tianping_thumb.png

站点地图 网站地图
豫ICP备17003270号-1 | Copyright © 2016-2020 靳闯博客
Typecho🍹Sgreen
TOP