CentOS系统—安装Python虚拟环境
一、使用virtualenv:
-
安装virtualenv:
pip install virtualenv
-
创建virtualenv环境名称:
virtualenv env_cow[虚拟环境名称]
-
激活创建的虚拟环境:
source env_cow[新创建虚拟环境名称]/bin/activate
# 可以看到出现个前缀虚拟环境名称 (env_cow) [root@localhost /]#
-
退出虚拟环境:
deactivate
二、使用virtualenvwrapper:
-
安装virtualenvwrapper:
pip install virtualenvwrapper
-
找到virtualenvwrapper.sh 文件所在位置:
whereis virtualenvwrapper.sh
-
安装zsh:
1.输入
cat /etc/shells
命令来查看本地安装的shell。/bin/sh /bin/bash /sbin/nologin /usr/bin/sh /usr/bin/bash /usr/sbin/nologin # 发现并没有zsh,我们使用yum来安装它
2.安装zsh:输入
yum -y install zsh
,等待下载完成。3.再次输入
cat /etc/shells
,发现zsh已经安装好 。/bin/sh /bin/bash /sbin/nologin /usr/bin/sh /usr/bin/bash /usr/sbin/nologin /bin/zsh
4.替换默认的shell:
chsh -s /bin/zsh
切换sehll至zsh。[root@localhost /]# chsh -s /bin/zsh Changing shell for root. Shell changed.
5.
reboot
重启后,查看当前使用的shell,已经改成了zsh。输入
echo $SHELL
:[root@localhost /]# echo $SHELL /bin/bash
-
安装oh-my-zsh:
一、手动安装:
1.安装git:
yum -y install git
2.克隆oh-my-zsh:git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
3.复制zshrc:
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
二、自动安装:
1.curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh
使用手动或者自动安装,完成后重启。oh-my-zsh就生效了。
-
下载完oh-my-zsh后,发现多了
~/.zshrc
文件。 -
编辑
~/.zshrc
,把virtualenvwrapper.sh绝对路径
添加该文件中:# If you come from bash you might have to change your $PATH. # export PATH=$HOME/bin:/usr/local/bin:$PATH # Path to your oh-my-zsh installation. export ZSH=$HOME/.oh-my-zsh source /usr/bin/virtualenvwrapper.sh # 添加进来 # Set name of the theme to load --- if set to "random", it will # load a random theme each time oh-my-zsh is loaded, in which case, # to know which specific one was loaded, run: echo $RANDOM_THEME # See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes ZSH_THEME="robbyrussell"
-
启动.zshrc文件:
source ~/.zshrc
➜ / source ~/.zshrc 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
-
使用virtualenvwrapper创建虚拟环境:
mkvirtualenv env_cow[虚拟环境名称]
(env_cow) ➜ / # 可以看到前缀名的变化,代表已经进入了虚拟环境
-
退出虚拟环境:
deactivate
-
激活虚拟环境:
workon env_cow[虚拟环境名称]
-
列出所有的虚拟环境:
workon
三、使用pyenv(Python全局版本):
-
下载安装:
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
-
下载完,打开
vim ~/.zshrc
,将下列信息添加该文件中:export PATH=$HOME/.pyenv/bin:$PATH eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)"
-
查看本机安装pyenv的版本:
pyenv versions
-
查看可安装的版本:
pyenv install -l
-
安装指定版本:
pyenv install 3.6.4
-
卸载指定版本:
pyenv uninstall 3.6.4
-
通过shell切换python 版本:
pyenv shell 3.6.4
-
查看安装的pyenv所在的插件:
ls -la ~/.pyenv/plugins
-
列出当前的虚拟环境:
pyenv virtualenvs
-
激活一个虚拟环境:
pyenv activate env_cow[虚拟环境]
-
创建一个虚拟环境并指定该Python版本:
pyenv virtualenv 2.7.4[指定的python版本,已经安装过] env_cow[虚拟环境名]
-
建议使用pyenv