zoukankan      html  css  js  c++  java
  • 腾讯云服务器ubuntu16.04系统下安装Python版本管理工具pyenv

    一、 系统环境

      腾讯云提供的系统是ubuntu 16.04 LTS,系统默认的Python版本是2.7.12,我想要安装3.6和其他的版本。
      比较方便的是腾讯云已经默认安装好了git和curl命令,可以很方便的下载安装软件

    二、 pyenv的安装

    • 第一步 下载并安装pyenv
    $ git clone git://github.com/yyuu/pyenv.git ~/.pyenv
    $ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
    $ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
    $ echo 'eval "$(pyenv init -)"' >> ~/.bashrc
    $ exec $SHELL -l    
    

    这个时候pyenv就已经安装好了,我们输入pyenv可以查看具体用法和简介

    ubuntu@VM-0-9-ubuntu:~$ pyenv
    pyenv 1.2.7-13-ge389c8d
    Usage: pyenv <command> [<args>]
    
    Some useful pyenv commands are:
       commands    List all available pyenv commands
       local       Set or show the local application-specific Python version
       global      Set or show the global Python version
       shell       Set or show the shell-specific Python version
       install     Install a Python version using python-build
       uninstall   Uninstall a specific Python version
       rehash      Rehash pyenv shims (run this after installing executables)
       version     Show the current Python version and its origin
       versions    List all Python versions available to pyenv
       which       Display the full path to an executable
       whence      List all Python versions that contain the given executable
    
    See `pyenv help <command>' for information on a specific command.
    For full documentation, see: https://github.com/pyenv/pyenv#readme
    

    我们还可以通过下面这个命令来查看可以安装的python版本:

    $ pyenv install --list
    

    在安装Python之前,必须要安装python所需要的依赖包,这个必须要安装,否则会安装Python会失败的:

    $ sudo apt-get install libc6-dev gcc
    $ sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm
    

    好了,我们可以开始安装Python了:

    $ pyenv install 3.6.3 -v
    

    需要注意的是,这种下载方法是从Python官网下载安装包,速度可能会非常慢,所以有个快速方法是先从搜狐提供的镜像网站下载指定的Python版本到~/.pyenv/cache目录下:

    wget http://mirrors.sohu.com/python/3.6.3/Python-3.6.3.tar.xz  -P ~/.pyenv/cache
    

    然后再用pyenv install命令安装,就可以很快完成了

    $ pyenv install 3.6.3
    

    安装完成之后,需要使用如下命令对数据库进行更新

    $ pyenv rehash
    

    查看当前已经安装的python版本:

    ubuntu@VM-0-9-ubuntu:~$ pyenv versions
    * system (set by /home/ubuntu/.pyenv/version)
      3.6.3
    

    其中星号代表是当前系统正在使用的python版本是系统自带的。

    三、 安装虚拟环境并切换Python版本

    按照上面步骤我们已经安装好了可用的Python版本,下面我们再下载一个pyenv插件-virtualenv

    git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
    

    但此时还不能使用。我们需要将以下命令添加到~/.bashrc文件结尾,这样就可以在命令行使用virtualenv了

    $ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
    $ source ~/.bashrc
    

    这样就可以使用了。

    然后我们创建Python虚拟环境,我们先在当前目录下创建一个文件夹:

    ubuntu@VM-0-9-ubuntu:~$ mkdir MyPython
    ubuntu@VM-0-9-ubuntu:~$ cd MyPython/
    ubuntu@VM-0-9-ubuntu:~/MyPython$ ll
    total 8
    drwxrwxr-x 2 ubuntu ubuntu 4096 Oct 30 19:06 ./
    drwxr-xr-x 8 ubuntu ubuntu 4096 Oct 30 19:06 ../
    ubuntu@VM-0-9-ubuntu:~/MyPython$
    

    好了,我们进入到MyPython文件夹下了,利用pyenv virtualenv命令创建虚拟环境

    ubuntu@VM-0-9-ubuntu:~/mypython$ pyenv virtualenv 3.6.3 env363
    
    Using base prefix '/home/ubuntu/.pyenv/versions/3.6.3'
    New python executable in /home/ubuntu/.pyenv/versions/3.6.3/envs/env363/bin/python3.6
    Also creating executable in /home/ubuntu/.pyenv/versions/3.6.3/envs/env363/bin/python
    Please make sure you remove any previous custom paths from your /home/ubuntu/.pydistutils.cfg file.
    Installing setuptools, pip, wheel...done.
    Requirement already satisfied: setuptools in /home/ubuntu/.pyenv/versions/3.6.3/envs/env363/lib/python3.6/site-packages
    Requirement already satisfied: pip in /home/ubuntu/.pyenv/versions/3.6.3/envs/env363/lib/python3.6/site-packages
    

    注意,命令中的3.6.3必须是一个安装前面步骤已经安装好的python版本, 否则会出错。env363则是自己起的名字,用来标注这个虚拟环境。
    然后我们通过pyenv versions命令来查看当前的虚拟环境

    ubuntu@VM-0-9-ubuntu:~/mypython$ pyenv versions
      system
    * 3.6.3 (set by /home/ubuntu/mypython/.python-version)
      3.6.3/envs/env363
      env363
    

    最后,我们利用pyenv的local方法来切换到创建好的虚拟环境

    ubuntu@VM-0-9-ubuntu:~/mypython$ pyenv local env363
    (env363) ubuntu@VM-0-9-ubuntu:~/mypython$ 
    

    看到命令行前面的那个括号就说明我们成功了。然后查看当前环境默认Python版本:

    (env363) ubuntu@VM-0-9-ubuntu:~/mypython$ python
    Python 3.6.3 (default, Oct 30 2018, 16:30:54)
    [GCC 5.4.0 20160609] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    

    pyenv local命令是为了保证只有当前文件夹以及往下递归的文件夹里的环境是Python3.6.3,其他文件夹以及系统Python环境不会改变

    (env363) ubuntu@VM-0-9-ubuntu:~/mypython$ cd
    ubuntu@VM-0-9-ubuntu:~$
    

    可以回到主目录下括号就消失了,说明当前没有在用虚拟环境

    ubuntu@VM-0-9-ubuntu:~$ python
    Python 2.7.12 (default, Dec  4 2017, 14:50:18)
    [GCC 5.4.0 20160609] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    

    可以看到系统默认的python版本没变。

    下面我们就可以可以在编辑器上连接远程服务器来进行Python开发了,skr~

  • 相关阅读:
    linux运维之分析系统负载及运行状况
    linux运维之分析日志相关命令(1)
    centos7修改网卡名称为eth0
    LANMP环境编译参数查看方法
    自动化部署之搭建yum仓
    浙大 PAT 乙级 1001-1075 目录索引
    更改docker服务网段分配地址
    MySQL主从复制(Replication for Backup)
    MySQL读写分离-简单思考
    NGINX负载均衡缓存配置
  • 原文地址:https://www.cnblogs.com/russellyoung/p/teng-xun-yun-fu-wu-qiubuntu1604xi-tong-xia-an-zhua.html
Copyright © 2011-2022 走看看