CentOS系统—安装Python虚拟环境

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值