Hướng dẫn how do i install multiple versions of python on centos 7? - làm cách nào để cài đặt nhiều phiên bản python trên centos 7?

Hôm nay, tôi sẽ cho bạn biết cách cài đặt các phiên bản Python 2.7 và 3,5 trên Centos 6.4. Và chúng tôi sẽ làm điều này mà không phá vỡ cài đặt Python hiện có [phiên bản cài đặt hệ thống mặc định là 2.6.6 trên Centos 6.4] và các công cụ hệ thống khác như YUM. Chà, lý do chính đằng sau hướng dẫn này là - Gần đây tôi đã cài đặt Python 3 và phá vỡ mọi thứ - Python, Pip, Yum, v.v.

# yum
 There was a problem importing one of the Python modules
 required to run yum. The error leading to this problem was:
 No module named rpm
 Please install a package which provides this module, or
 verify that the module is installed correctly.

Python không thể nhập YUM sau khi cài đặt/nâng cấp Python:

# python
 Python 2.6 [r26:66714, Dec 16 2015, 11:33:08]
 [GCC 4.4.7 20120313 [Red Hat 4.4.7-11]] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import yum
 Traceback [most recent call last]:
 File "", line 1, in
 ImportError: No module named yum
 >>>

và Pip cũng bị phá vỡ:

# pip
 Traceback [most recent call last]:
 File "/usr/bin/pip", line 5, in
 from pkg_resources import load_entry_point
 ImportError: No module named pkg_resources

Vì vậy, bài học rút ra là - đừng bao giờ gây rối với Python !! [Một khi python bị phá vỡ, nó rất khó sửa]. Điều đó có nghĩa là, nếu bạn muốn cài đặt nhiều phiên bản Python trên một máy, thì hãy thực hiện đúng cách!Never mess up with Python!! [Once the Python is broken, it’s very hard to fix]. It means, if you ever want to install multiple versions of Python on a single machine, then do it in a right way!

Lưu ý: Luôn cài đặt phiên bản Python mới ở vị trí không chuẩn [chẳng hạn như/usr/local/]

Prerequisites: You need root access or sudo privilege.

Chuẩn bị sẵn sàng thiết lập Python bằng cách cài đặt các công cụ phát triển trên mạng và các thư viện cần thiết.

Cài đặt các công cụ phát triển:

#yum groupinstall "Development tools"
 :::::::::::::::::::::::::
 :::::::::::::::::::::::::
 rpm-libs.x86_64 0:4.8.0-47.el6
 rpm-python.x86_64 0:4.8.0-47.el6
 xz-libs.x86_64 0:4.999.9-0.5.beta.20091007git.el6
 Complete!

Bạn cũng có thể yêu cầu các thư viện này:

Các thư viện này không phải cài đặt Python, nhưng bạn có thể cần chúng ở giai đoạn sau.

#yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
 :::::::::::::::::::::::::::
 :::::::::::::::::::::::::::
 ncurses-libs.x86_64 0:5.7-4.20090207.el6
 openssl.x86_64 0:1.0.1e-42.el6_7.2
 sqlite.x86_64 0:3.6.20-1.el6_7.2
 Complete!

Biên dịch và cài đặt Python 2.7 trên Centos 6.4

Tải xuống Python 2.7:

# wget //python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz
# tar xf Python-2.7.6.tar.xz

Lưu ý: Phiên bản mới nhất của tar là & nbsp; có khả năng trích xuất & nbsp; .xz. Trong trường hợp, nếu bạn không thể trích xuất bằng TAR, thì đây là một hướng dẫn để giúp bạn ra ngoài. The latest version of tar is capable of extracting .xz file. In case, if you couldn’t extract using tar, then here’s a guide to help you out.

# cd Python-2.7.6
#./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
 :::::::::::::::::::::::::::::::
 :::::::::::::::::::::::::::::::
 configure: creating ./config.status
 config.status: creating Makefile.pre
 config.status: creating Modules/Setup.config
 config.status: creating Misc/python.pc
 config.status: creating Modules/ld_so_aix
 config.status: creating pyconfig.h
 creating Modules/Setup
 creating Modules/Setup.local
 creating Makefile

Lưu ý: Tôi sẽ cài đặt Python trong /usr /local. Điều đó có nghĩa là, thực thi sẽ được cài đặt trong/usr/local/bin và thư viện trong/usr/local/lib. Nhìn ra LDFLAGS là tốt. I’ll be installing Python in /usr/local. It means, the executable will be installed in /usr/local/bin and libraries in /usr/local/lib. Lookout for the LDFLAGS as well.

# python
 Python 2.6 [r26:66714, Dec 16 2015, 11:33:08]
 [GCC 4.4.7 20120313 [Red Hat 4.4.7-11]] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import yum
 Traceback [most recent call last]:
 File "", line 1, in
 ImportError: No module named yum
 >>>
0

LƯU Ý: Lookout cho Make Altinstall-Bạn không bao giờ nên sử dụng ‘Tạo Cài đặt vì nó sẽ cài đặt cả hai phiên bản trên cùng một hệ thống tệp và sẽ làm rối tung Python. Lookout for make altinstall – you should never use ‘make install’ as it will install both the versions on the same file-system and will mess up python.

Python 2.7 được cài đặt dưới:

# python
 Python 2.6 [r26:66714, Dec 16 2015, 11:33:08]
 [GCC 4.4.7 20120313 [Red Hat 4.4.7-11]] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import yum
 Traceback [most recent call last]:
 File "", line 1, in
 ImportError: No module named yum
 >>>
1

Biên dịch và cài đặt Python 3,5 trên Centos

# python
 Python 2.6 [r26:66714, Dec 16 2015, 11:33:08]
 [GCC 4.4.7 20120313 [Red Hat 4.4.7-11]] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import yum
 Traceback [most recent call last]:
 File "", line 1, in
 ImportError: No module named yum
 >>>
2
# python
 Python 2.6 [r26:66714, Dec 16 2015, 11:33:08]
 [GCC 4.4.7 20120313 [Red Hat 4.4.7-11]] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import yum
 Traceback [most recent call last]:
 File "", line 1, in
 ImportError: No module named yum
 >>>
3
# python
 Python 2.6 [r26:66714, Dec 16 2015, 11:33:08]
 [GCC 4.4.7 20120313 [Red Hat 4.4.7-11]] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import yum
 Traceback [most recent call last]:
 File "", line 1, in
 ImportError: No module named yum
 >>>
4
# python
 Python 2.6 [r26:66714, Dec 16 2015, 11:33:08]
 [GCC 4.4.7 20120313 [Red Hat 4.4.7-11]] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import yum
 Traceback [most recent call last]:
 File "", line 1, in
 ImportError: No module named yum
 >>>
5
# python
 Python 2.6 [r26:66714, Dec 16 2015, 11:33:08]
 [GCC 4.4.7 20120313 [Red Hat 4.4.7-11]] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import yum
 Traceback [most recent call last]:
 File "", line 1, in
 ImportError: No module named yum
 >>>
6
# python
 Python 2.6 [r26:66714, Dec 16 2015, 11:33:08]
 [GCC 4.4.7 20120313 [Red Hat 4.4.7-11]] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import yum
 Traceback [most recent call last]:
 File "", line 1, in
 ImportError: No module named yum
 >>>
7

Bây giờ, hãy để cài đặt setuptools và pip & nbsp; cho & nbsp; cả hai phiên bản Python [2.7 và 3.5] chúng tôi đã cài đặt ở trên:

# python
 Python 2.6 [r26:66714, Dec 16 2015, 11:33:08]
 [GCC 4.4.7 20120313 [Red Hat 4.4.7-11]] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import yum
 Traceback [most recent call last]:
 File "", line 1, in
 ImportError: No module named yum
 >>>
8
# python
 Python 2.6 [r26:66714, Dec 16 2015, 11:33:08]
 [GCC 4.4.7 20120313 [Red Hat 4.4.7-11]] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import yum
 Traceback [most recent call last]:
 File "", line 1, in
 ImportError: No module named yum
 >>>
9

Cập nhật: Lệnh trên sẽ không tìm thấy lỗi không tìm thấy. Thay vào đó, sử dụng lệnh dưới đây để cài đặt PIP2.7. The above command will fail with not found error. Instead use the below command to install pip2.7.

# pip
 Traceback [most recent call last]:
 File "/usr/bin/pip", line 5, in
 from pkg_resources import load_entry_point
 ImportError: No module named pkg_resources
0

Cài đặt setuptools cho Python 2.7:

# pip
 Traceback [most recent call last]:
 File "/usr/bin/pip", line 5, in
 from pkg_resources import load_entry_point
 ImportError: No module named pkg_resources
1

Cài đặt setuptools cho Python 3.5:

# pip
 Traceback [most recent call last]:
 File "/usr/bin/pip", line 5, in
 from pkg_resources import load_entry_point
 ImportError: No module named pkg_resources
2

Cài đặt PIP cho Python 2.7:

# pip
 Traceback [most recent call last]:
 File "/usr/bin/pip", line 5, in
 from pkg_resources import load_entry_point
 ImportError: No module named pkg_resources
3

Cài đặt PIP cho Python 3.5:

# pip
 Traceback [most recent call last]:
 File "/usr/bin/pip", line 5, in
 from pkg_resources import load_entry_point
 ImportError: No module named pkg_resources
4

Vì vậy, chúng tôi & nbsp; có hai phiên bản Python, Pip và Setuptools. Bây giờ chúng tôi sẽ cô lập các môi trường python đó [2.7 và 3.5].

Cách thiết lập môi trường Python bị cô lập

Để làm điều đó, chúng tôi cần một gói gọi là Virtualenv.

Cài đặt VirtualEnv cho Python 2.7:

# pip
 Traceback [most recent call last]:
 File "/usr/bin/pip", line 5, in
 from pkg_resources import load_entry_point
 ImportError: No module named pkg_resources
5

Sau khi được cài đặt, hãy tìm các lệnh VirtualEnv như hiển thị bên dưới:

# pip
 Traceback [most recent call last]:
 File "/usr/bin/pip", line 5, in
 from pkg_resources import load_entry_point
 ImportError: No module named pkg_resources
6

Bây giờ, chúng tôi đã tạo ra môi trường bị cô lập bằng VirtualEnv cho Python 2.7 và tôi sẽ đặt tên cho môi trường ảo đó là PY27ENV [bạn có thể đặt một tên khác].py27env [you may set a different name].

# pip
 Traceback [most recent call last]:
 File "/usr/bin/pip", line 5, in
 from pkg_resources import load_entry_point
 ImportError: No module named pkg_resources
7

Môi trường bị cô lập & nbsp; [được gọi là hộp cát] sẽ được tạo trong thư mục hiện tại nơi bạn thực hiện lệnh trên. Xem ra thư mục có tên là PY27ENV [có thể là một tên khác trong trường hợp của bạn].py27env [might be a different name in your case].

# pip
 Traceback [most recent call last]:
 File "/usr/bin/pip", line 5, in
 from pkg_resources import load_entry_point
 ImportError: No module named pkg_resources
8

Bạn cần kích hoạt hộp cát PY27ENV:activate the sandbox py27env:

# pip
 Traceback [most recent call last]:
 File "/usr/bin/pip", line 5, in
 from pkg_resources import load_entry_point
 ImportError: No module named pkg_resources
9

Bây giờ, hãy kiểm tra phiên bản Python như hình dưới đây:

Prerequisites: You need root access or sudo privilege.
0

Đầu ra trên cho thấy bạn đang ở trong hộp cát Python 2.7.6. Để chuyển trở lại phiên bản Python đã cài đặt hệ thống, bạn chỉ cần hủy kích hoạt nó như dưới đây:

Prerequisites: You need root access or sudo privilege.
1

Kiểm tra phiên bản ngay bây giờ:

Prerequisites: You need root access or sudo privilege.
2

Tạo môi trường hộp cát cho Python 3.5

Chúng tôi đã sử dụng VirtualEnv để tạo hộp cát cho Python 2.5, nhưng đối với Python 3.5, chúng tôi sẽ sử dụng lệnh tích hợp có tên & NBSP; Pyvenv-3.5.pyvenv-3.5.

Prerequisites: You need root access or sudo privilege.
3

Bạn có thể tìm thấy một thư mục có tên ‘PY35En môi trường trong thư mục hiện tại. Bây giờ, hãy kích hoạt môi trường Python 3.5 như hình dưới đây:

Prerequisites: You need root access or sudo privilege.
4
Prerequisites: You need root access or sudo privilege.
5

Vô hiệu hóa nó như dưới đây:

Prerequisites: You need root access or sudo privilege.
6
Prerequisites: You need root access or sudo privilege.
7

Đó là nó! Bây giờ bạn có hai môi trường python khác nhau và bạn đã làm điều đó mà không làm rối tung bất cứ điều gì. Hy vọng hướng dẫn này đã giúp bạn.

Tôi có thể có nhiều phiên bản Linux được cài đặt Python không?

Nó có thể cài đặt nhiều phiên bản Python, chỉ định phiên bản sử dụng toàn hệ thống và chỉ định phiên bản được sử dụng trong các thư mục cụ thể.Nó cũng có thể tạo và quản lý môi trường ảo bằng các phiên bản cụ thể., specify the version that's used system-wide, and specify the version that's used in specific directories. It can also create and manage virtual environments using specific versions.

Bạn có thể có 2 phiên bản Python khác nhau không?

Với Navigator, bạn có thể có các phiên bản Python khác nhau trên máy tính của mình và dễ dàng chuyển đổi giữa chúng bằng cách giữ chúng trong các môi trường khác nhau.Để sử dụng một phiên bản khác của Python so với phiên bản bạn hiện đang sử dụng, hãy thiết lập một môi trường mới: bắt đầu điều hướng. and easily switch between them by keeping them in different environments. To use a different version of Python than the one you are currently using, set up a new environment: Start Navigator.

Tôi có thể cài đặt các phiên bản Python khác nhau không?

Nếu bạn muốn sử dụng nhiều phiên bản Python trên một máy, thì PyenV là một công cụ thường được sử dụng để cài đặt và chuyển đổi giữa các phiên bản.Điều này không được nhầm lẫn với tập lệnh Pyvenv khấu hao được đề cập trước đó.Nó không đi kèm với Python và phải được cài đặt riêng.pyenv is a commonly used tool to install and switch between versions. This is not to be confused with the previously mentioned depreciated pyvenv script. It does not come bundled with Python and must be installed separately.

Bài Viết Liên Quan

Chủ Đề