Ubuntu下Python的安装及管理

Ubuntu下Python的安装及管理

1.概述

Ubuntu下python的安装及配置。
在这里插入图片描述

2.安装

  1. 安装python2.7:
python --version #或python2.7 --version检查检查python是否存在,有则无需继续安装python2.7
sudo apt-get update
sudo apt-get install python #或者sudo apt-get install python2.7
python --version #检查python是否存在
  1. 安装python3.6:
python3 --version #或python3.6 --version检查python3.6是否存在,有则无需继续安装python3.6
sudo apt-get update
sudo apt-get install python3 #或者sudo apt-get install python3.6
python3 --version #或者python3.6 --version检查python3.6是否存在  

小贴士:如何使用apt-get安装指定版本的Package?
sudo apt-get install package=version
例如安装autoconf命令如下

sudo apt-get install autoconf=2.50
#查看源中可用版本
sudo apt-cache madison autoconf

3.python多版本管理

查看安装了哪些python:ls /usr/bin/python*,执行相应python版本的命令:
python xxx、python2 xxx、python2.7;python3 xxx、python3m xxx、python3.6

推荐方式二或者方式三

  1. 方式一:
    - 1)用which python命令查看默认安装python的目录(一般是该目录:/usr/bin/python),然后删除默认python
 which python
 cd /usr/bin
 sudo rm python
  • 2)用 which python3.6 查看安装python3.6的目录(如/usr/bin/python3.6),然后用python3.6替换python2.7
which python3.6
sudo ln -s /usr/bin/python3.6 /usr/bin/python
  • 3)最后python --version就可以看到版本是python3.6了。同理切换到python2.7一样的操作。
  1. 方式二:
  • 1)列出所有Python版本:sudo update-alternatives --list python,这里可能会报错update-alternatives: 错误: 无 python 的候选项,所以需要将Python注册到中,即alternatives install(第2步)。
  • 2) alternatives注册 python:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 300
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 301
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 302

命令执行后,sudo update-alternatives --list python验证一下是否成功

  • 3)管理切换
    开启配置:sudo update-alternatives --config python
    切换:窗口光标出输 python列表对应的开头编号,然后回车即可完成切换。
    验证:python -version查看是否已经切换。
  1. 方式三:
    使用pyenv管理Python多版本
    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

环境安装

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

安装任意版本:
{替换搜狐源, 下载更快, 修改要下载的版本配置文件, 例如:~/.pyenv/plugins/python-build/share/python-build/2.7.8, 将https://www.python.org/ftp替换成http://mirrors.sohu.com}

pyenv install 2.7.8 -v

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

pyenv rehash

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

pyenv versions

设置全局的python版本

pyenv global 2.7.8

pyenv选项

cl@CL:/usr/local/libiconv$ pyenv -h
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   commands    List all available pyenv commands
   commands    List all available pyenv commands
   exec        Run an executable with the selected Python version
   global      Set or show the global Python version
   help        Display help for a command
   hooks       List hook scripts for a given pyenv command
   init        Configure the shell environment for pyenv
   install     Install a Python version using python-build
   local       Set or show the local application-specific Python version
   prefix      Display prefix for a Python version
   rehash      Rehash pyenv shims (run this after installing executables)
   root        Display the root directory where versions and shims are kept
   shell       Set or show the shell-specific Python version
   shims       List existing pyenv shims
   uninstall   Uninstall a specific Python version
   version     Show the current Python version and its origin
   --version   Display the version of pyenv
   version-file   Detect the file that sets the current pyenv version
   version-name   Show the current Python version
   version-origin   Explain how the current Python version is set
   versions    List all Python versions available to pyenv
   whence      List all Python versions that contain the given executable
   which       Display the full path to an executable

See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme


欢迎关注我的公众号“虎哥LoveDroid”,原创技术文章第一时间推送。
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Calvin880828

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值