Cách chính xác để cài đặt python 2.7 trên Ubuntu 17.10?


42

Tôi đã tự hỏi làm thế nào để cài đặt python2.7 chính xác. Trên bản cài đặt khác của tôi, zlib không hoạt động và pip không cài đặt chính xác và tôi buộc phải sử dụng python3 từ dòng lệnh.

Tôi có bản cài đặt Ubuntu 17.10 mới và muốn có thể sử dụng pip và công cụ. Tôi nghĩ rằng đó là do python đã được cài đặt trong Ubuntu và tôi đã cài đặt một phiên bản khác hoặc một cái gì đó bởi vì các công cụ dòng lệnh dựa trên python như biến động đã hoạt động.

Có cách nào để sửa nó để tôi có thể cài đặt các mô-đun và công cụ hoặc sử dụng python đã được cài đặt từ dòng lệnh không?


Tôi đang sử dụng 17.10. Tôi muốn sử dụng phiên bản dòng lệnh và cài đặt thư viện nhưng tôi không biết làm thế nào.
dùng7853796

Câu trả lời:


67

Để cài đặt Python 2.7, bạn chỉ cần thực hiện các thao tác sau trong Ubuntu 17.10 trong một thiết bị đầu cuối (chúng hoạt động rất đẹp mắt):

# 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

LƯU Ý: Đừng cố xóa python 3.6 vì nó sẽ làm hỏng hệ thống của bạn

Bạn có thể gọi python pip theo cách sau:

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

Sử dụng pipmà không có số sẽ cài đặt gói python 3.6.


2

Kinh nghiệm của riêng tôi trong việc cài đặt Python và tất cả các gói cần thiết. Đã thử nghiệm trên Ubuntu 18.04 (không được thử nghiệm vào ngày 17.10). Tôi có thể sai, vì tôi không phải là chuyên gia về Ubuntu.

Nó là tốt hơn sử dụng lệnh apt( apt-get) thay vì piplệnh, bởi vì:

  1. cài đặt apt chỉ được thử nghiệm trên các gói và phần mềm Ubuntu;
  2. lệnh sudo apt update / upgrage giúp các gói cập nhật;
  3. nếu bạn muốn cài đặt / cập nhật gói cho tất cả người dùng trên hệ thống Ubuntu của bạn, không chỉ cho tài khoản cục bộ của bạn;
  4. nếu bạn muốn các gói cho Ubuntu, thì hệ điều hành cũng có thể sử dụng chúng.

Đối với các phiên bản khác của gói, người ta nên sử dụng môi trường ảo. Hoặc xây dựng và kiểm tra các gói từ mã nguồn (chỉ dành cho chuyên gia).

Không xóa python3 hiện tại, nếu không hệ điều hành Ubuntu sẽ TẠO.

# 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
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.