Ubuntu下Python的安装及管理
1.概述
Ubuntu下python的安装及配置。
2.安装
- 安装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是否存在
- 安装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)用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)列出所有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
查看是否已经切换。
- 方式三:
使用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”,原创技术文章第一时间推送。