自从 Ubuntu 18.04 以来,Python 2 不再是默认的 Python 版本。在最新的 Ubuntu 20.04 中,Python 2 更是被完全抛弃。但是 Python 2 依旧有用,所以需要自行安装
$ sudo apt install python2
$ python2 -V
$ ls /usr/bin/python*
/usr/bin/python /usr/bin/python3.6 /usr/bin/python3.8
/usr/bin/python2 /usr/bin/python3.6-config /usr/bin/python3.8-config
/usr/bin/python2.7 /usr/bin/python3.6m /usr/bin/python3-config
/usr/bin/python3 /usr/bin/python3.6m-config /usr/bin/python-mkdebian
因为我是由 18.04 升级到 20.04,所以还有 Python 3.6 版本
$ sudo update-alternatives --list python
update-alternatives: error: no alternatives for python
此时,显示没有配置方案
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 1
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2
$ sudo update-alternatives --list python
/usr/bin/python2
/usr/bin/python3
$ sudo update-alternatives --config python
有 2 个候选项可用于替换 python (提供 /usr/bin/python)。
选择 路径 优先级 状态
------------------------------------------------------------
0 /usr/bin/python3 2 自动模式
* 1 /usr/bin/python2 1 手动模式
2 /usr/bin/python3 2 手动模式
要维持当前值[*]请按<回车键>,或者键入选择的编号:1
如果想默认 Python 2 选 1
,想默认 Python 3 选2
$ python -V
pip is the package installer for Python. You can use pip to install packages from the Python Package Index and other indexes. PIP
% Python 2:
$ sudo apt install python-pip
% Python 3:
$ sudo apt install python3-pip
$ pip --version
$ pip3 --version
更新为国内的源,更新更快
% 进入 .config
$ cd .config
% 创建文件夹 pip
$ mkdir pip
% 创建文件 pip.conf
$ touch pip.conf
% 编辑 pip.conf
$ sudo emacs pip.conf
% 填入并保存
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
也可以填入其他源:
% Python 2
$ python2 -m pip install --upgrade pip
% Python 3
$ python3 -m pip install --upgrade pip
NumPy SciPy Pandas Matplotlib
% Python 2:
$ pip install numpy scipy pandas matplotlib
% Python 3:
$ pip3 install numpy scipy pandas matplotlib
$ python -c "import numpy; print(numpy.__version__)"
1.16.6
$ python3 -c "import numpy; print(numpy.__version__)"
1.18.1
% Python 2:
$ pip install --upgrade numpy
% Python 3:
$ pip3 install --upgrade numpy
IPython provides a rich architecture for interactive computing with:
- A powerful interactive shell.
- A kernel for Jupyter.
- Support for interactive data visualization and use of GUI toolkits.
- Flexible, embeddable interpreters to load into your own projects.
- Easy to use, high performance tools for parallel computing.
IPython
% python 3
$ pip3 install ipython
% or
$ sudo apt install ipython3
如果遇到权限问题,可以尝试在命令后加 --user
$ pip uninstall <package name>
% or
$ pip3 uninstall <package name>
原文及更新链接: matnoble.me/posts/ubuntu/python/configure-python-environment-in-ubuntu/