# SUSE SLES 15 SP4 安裝python 2.7 ## 1. 注意事項 :::danger 1. Python 2系列在SLES 15 SP4已全面廢棄。 3. [Python 2 2020年廢棄公告。](https://www.python.org/doc/sunset-python-2/) 4. [Drop all Python 2 packages。](https://code.opensuse.org/leap/features/issue/15) ::: ## 2. 註冊與啟用packagehub ### 2.1 註冊 ```shell= you@localhost:~> sudo SUSEConnect -r LICENSE -e your@email.com We trust you have received the usual lecture from the local System Administrator. It usually boils down to these three things: #1) Respect the privacy of others. #2) Think before you type. #3) With great power comes great responsibility. [sudo] password for root: Registering system to SUSE Customer Center Using E-Mail:your@email.com Announcing system to https://scc.suse.com ... Activating SLES 15.4 x86_64 ... -> Adding service to system ... ... ... Activating sle-module-server-applications 15.4 x86_64 ... -> Adding service to system ... -> Installing release package ... Successfully registered system ``` ### 2.2 啟用packagehub ```shell= you@localhost:~> sudo SUSEConnect -p PackageHub/15.4/x86_64 Registering system to SUSE Customer Center Updating system details on https://scc.suse.com ... Activating PackageHub 15.4 x86_64 ... -> Adding service to system ... -> Installing release package ... Successfully registered system ``` ## 3. 安裝指令 ### 3.1 安裝gcc ```shell= you@localhost:~> sudo zypper install -y gcc Refreshing service 'Basesystem_Module_15_SP4_x86_64'. Refreshing service 'SUSE_Linux_Enterprise_Server_15_SP4_x86_64'. Refreshing service 'SUSE_Package_Hub_15_SP4_x86_64'. Refreshing service 'Server_Applications_Module_15_SP4_x86_64'. Loading repository data... Reading installed packages... Resolving package dependencies... The following 15 NEW packages are going to be installed: gcc gcc7 glibc-devel libasan4 libatomic1 libcilkrts5 libgomp1 libitm1 liblsan0 libmpx2 libmpxwrappers2 libtsan0 libubsan0 libxcrypt-devel linux-glibc-devel 15 new packages to install. Overall download size: 22.7 MiB. Already cached: 0 B. After the operation, additional 83.7 MiB will be used. Continue? [y/n/v/...? shows all options] (y): y Retrieving package libasan4-7.5.0+r278197-4.30.1.x86_64 (1/15), 362.2 KiB ( 1.3 MiB unpacked) Retrieving: libasan4-7.5.0+r278197-4.30.1.x86_64.rpm .............................................................................[done] Retrieving package libcilkrts5-7.5.0+r278197-4.30.1.x86_64 (2/15), 65.7 KiB (120.1 KiB unpacked) Retrieving: libcilkrts5-7.5.0+r278197-4.30.1.x86_64.rpm ..........................................................................[done] Retrieving package libubsan0-7.5.0+r278197-4.30.1.x86_64 (3/15), 138.9 KiB (305.2 KiB unpacked) ... ... Checking for file conflicts: .....................................................................................................[done] ( 1/15) Installing: libasan4-7.5.0+r278197-4.30.1.x86_64 .........................................................................[done] ( 2/15) Installing: libcilkrts5-7.5.0+r278197-4.30.1.x86_64 ......................................................................[done] ( 3/15) Installing: libubsan0-7.5.0+r278197-4.30.1.x86_64 ........................................................................[done] ( 4/15) Installing: libatomic1-12.2.1+git416-150000.1.7.1.x86_64 .................................................................[done] ( 5/15) Installing: libgomp1-12.2.1+git416-150000.1.7.1.x86_64 ...................................................................[done] ... ... you@localhost:~> sudo gcc --version gcc (SUSE Linux) 7.5.0 Copyright (C) 2017 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ``` ### 3.2 啟用最佳化python 2.7 ```shell= you@localhost:~> curl -O https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tgz % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 16.7M 100 16.7M 0 0 7166k 0 0:00:02 0:00:02 --:--:-- 7166k rancher@localhost:~> tar -xzf Python-2.7.18.tgz rancher@localhost:~> cd Python-2.7.18/ you@localhost:~/Python-2.7.18> ./configure --enable-optimizations checking build system type... x86_64-pc-linux-gnu checking host system type... x86_64-pc-linux-gnu checking for python2.7... no checking for python3... python3 checking for --enable-universalsdk... no checking for --with-universal-archs... no checking MACHDEP... linux2 checking EXTRAPLATDIR... ... ... 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 ``` ### 3.3 進行安裝 採用altinstall模式,避免跟其他套件衝突,因此python執行指令為python2.7。 ```shell= you@localhost:~/Python-2.7.18> sudo make altinstall gcc -pthread -c -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I. -IInclude -I./Include -DPy_BUILD_CORE -o Modules/python.o ./Modules/python.c gcc -pthread -c -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I. -IInclude -I./Include -DPy_BUILD_CORE -o Parser/acceler.o Parser/acceler.c ... ... Creating directory /usr/local/share/man Creating directory /usr/local/share/man/man1 /usr/bin/install -c -m 644 ./Misc/python.man \ /usr/local/share/man/man1/python2.7.1 if test "xno" != "xno" ; then \ case no in \ upgrade) ensurepip="--altinstall --upgrade --no-default-pip" ;; \ install|*) ensurepip="--altinstall --no-default-pip" ;; \ esac; \ ./python -E -m ensurepip \ $ensurepip --root=/ ; \ fi you@localhost:~/Python-2.7.18> python2.7 --version Python 2.7.18 ``` ## 4. 基本測試 ### 4.1 測試腳本 建立一個名為test.py的檔案,將以下內容貼上並且存檔。 ```shell= message = "Hello, World!" print message numbers = [1, 2, 3, 4, 5] for num in numbers: print num def add_numbers(x, y): return x + y result = add_numbers(2, 3) print result import datetime now = datetime.datetime.now() formatted_date_time = now.strftime("%Y-%m-%d %H:%M:%S") print(formatted_date_time) ``` ### 4.2 執行腳本 ```shell= you@localhost:~> python2.7 test.py Hello, World! 1 2 3 4 5 5 2023-04-25 13:17:21 ```