在Ubuntu 17.10上安装python 2.7的正确方法?


42

我想知道如何正确安装python2.7。在我的其他安装中,zlib无法正常工作,并且pip无法正确安装,因此我被迫从命令行使用python3。

我有一个全新的Ubuntu 17.10安装,并且希望能够使用pip和其他东西。我认为这是因为在Ubuntu中已经安装了python,并且安装了另一个版本,或者是因为诸如波动性之类的基于python的命令行工具有效。

有什么办法可以修复它,以便我可以安装模块和东西,或者从命令行使用已经安装的python?


我正在使用17.10。我想使用命令行版本并安装库,但不确定如何。
user7853796

Answers:


67

要安装Python 2.7,您只需要在终端中的Ubuntu 17.10中执行以下操作(它们可以并排工作,美观漂亮):

# refreshing the repositories
sudo apt update
# its wise to keep the system up to date!
# you can skip the following line if you not
# want to update all your software
sudo apt upgrade
# installing python 2.7 and pip for it
sudo apt install python2.7 python-pip
# installing python-pip for 3.6
sudo apt install python3-pip

注意:请勿尝试删除python 3.6,因为它将破坏您的系统

您可以通过以下方式调用python pip:

# for python 2.7
pip2 install <package>
# for python 3.6
pip install <package>

pip不带编号使用会安装python 3.6软件包。


2

我自己在安装Python和所有必需的软件包方面的经验。已在Ubuntu 18.04上测试(未在17.10上测试)。我可能是错的,因为我不是Ubuntu的专家。

最好使用aptapt-get)命令而不是pipcommand,因为:

  1. apt安装仅在Ubuntu软件包和独立性上经过测试;
  2. sudo apt update / upgrage命令使软件包保持最新;
  3. 如果您要为Ubuntu系统上的所有用户安装/更新软件包,而不仅仅是您自己的本地帐户;
  4. 如果您想要Ubuntu的软件包,那么操作系统也可以使用它们。

对于其他版本的软件包,应使用虚拟环境。或从源代码构建和测试软件包(仅适用于专家)。

不要删除当前的python3,否则Ubuntu OS会崩溃。

# Refreshing the repositories
sudo apt update
# Update software
sudo apt upgrade

# Install Python and necessary packages.

# Install pip for 2.7 and then python 2.7 itself
sudo apt install python-pip
sudo apt install python2.7

# Install pip for 3.6
sudo apt install python3-pip
# Install currently supported by Ubuntu python 3.x version.
sudo apt install python3

# Don't delete current python3, otherwise Ubuntu OS will BROKE.
# Better don't install the newest versions 3.7, 3.8, 4.0, etc. on the whole OS (globally).
# This command works, but it's a bad idea to use it -- sudo apt install python3.7
#     in this case import of numpy (import numpy) and other modules will fail for python3.7,
#     because 3.6 is the current (global) python version for Ubuntu, not 3.7.
# Use "sudo apt install python3" not "sudo apt install python3.7" command for python 3.x installation.
# If you need 3.7 or newer, use local virtual environment.
# It's a bad idea to have several versions of python 3.x globally at the same time.
# Use only currently supported by Ubuntu python 3.x version globally. At this moment it is 3.6.

# Install numpy, scipy, matplotlib, scikit-learn, scikit-image,
# opencv with contributions, pandas, pillow, psutil, spur, cython,
#ipython, jupyter, git.
sudo apt install python-numpy
sudo apt install python3-numpy
sudo apt install python-scipy
sudo apt install python3-scipy
sudo apt install python-matplotlib
sudo apt install python3-matplotlib
sudo apt install python-sklearn
sudo apt install python3-sklearn
sudo apt install python-skimage
sudo apt install python3-skimage
sudo apt install python-opencv
sudo apt install python3-opencv
sudo apt install python-pandas
sudo apt install python3-pandas
sudo apt install python-pil
sudo apt install python3-pil
sudo apt install python-pil.imagetk  # if the imageTk import doesn't work
sudo apt install python3-pil.imagetk  # if the imageTk import doesn't work
sudo apt install python-psutil
sudo apt install python3-psutil
sudo apt install python-spur
sudo apt install python3-spur
sudo apt install cython
sudo apt install cython3
sudo apt install python-ipython
sudo apt install python3-ipython
sudo apt install ipython
sudo apt install ipython3
sudo apt install jupyter
sudo apt install git

# To have both python 2 and 3 available on jupyter
sudo apt install python-ipykernel
sudo apt install python3-ipykernel

# To check installed packages use commands
python
# and
python3

# Then type in python 2 or 3 console
import numpy
import scipy
import matplotlib
import sklearn
import skimage
exit()

# To check ipython
ipython
exit
ipython3
exit

# To check jupyter run
jupyter notebook
# and check both version of python 2 and 3 in "New" menu

# To remove package (don't remove python3 -- it'll broke your Ubuntu)
sudo apt purge --auto-remove packagename
# To search for the package:
apt search packagename

# Install PyCharm Community edition
sudo snap install pycharm-community --classic
# To check PyCharm installation enter:
pycharm-community
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.