------------ ------------ # 一、查看默认版本 查看当前CentOS 7.9 默认的Python版本 ```shell [root@192-168-1-33 ~]# python -V Python 2.7.5 [root@192-168-1-33 ~]# ``` # 二、安装依赖环境 ```shell yum install -y zlib* gcc libffi-devel ``` **同时我们需要编译安装OpenSSL 1.1.1 or newer** **具体的可以参考这篇文章进行openssl 1.1.1的编译安装** **[CentOS 7.9 编译安装 openssl-1.1.1](https://www.xiaoleizhang.com/index.php/archives/215/ "CentOS 7.9 编译安装 openssl-1.1.1")** - 如果没有安装gcc编译环境,添加配置时会有如下报错: ```shell [root@192-168-1-33 Python-3.10.4]# ./configure --prefix=/usr/local/python3 checking build system type... x86_64-pc-linux-gnu checking host system type... x86_64-pc-linux-gnu checking for python3.10... no checking for python3... no checking for python... python checking for --enable-universalsdk... no checking for --with-universal-archs... no checking MACHDEP... "linux" checking for gcc... no checking for cc... no checking for cl.exe... no configure: error: in `/root/Python-3.10.4': configure: error: no acceptable C compiler found in $PATH See `config.log' for more details [root@192-168-1-33 Python-3.10.4]# ``` no acceptable C compiler found in $PATH,代表你没有安装C编译器 - 如果没有安装libffi-devel,编译源码时(make)会有如下报错: ```shell /root/Python-3.10.4/Modules/_ctypes/_ctypes.c:107:17: 致命错误:ffi.h:没有那个文件或目录 #include ^ 编译中断。 Python build finished successfully! The necessary bits to build these optional modules were not found: _bz2 _curses _curses_panel _dbm _gdbm _hashlib _lzma _sqlite3 _ssl _tkinter _uuid readline zlib To find the necessary bits, look in setup.py in detect_modules() for the module's name. The following modules found by detect_modules() in setup.py, have been built by the Makefile instead, as configured by the Setup files: _abc pwd time Failed to build these modules: _ctypes ``` - 如果没有对openssl-1.1.1进行编译安装,会出现如下问题: *编译源码时(make)会有如下报错:* ```shell Could not build the ssl module! Python requires a OpenSSL 1.1.1 or newer ``` *pip安装模块时会出现如下报错:* ```shell [root@192-168-1-33 Python-3.10.4]# pip3 install paramiko WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/paramiko/ WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/paramiko/ WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/paramiko/ WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/paramiko/ WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/paramiko/ Could not fetch URL https://pypi.org/simple/paramiko/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/paramiko/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping ERROR: Could not find a version that satisfies the requirement paramiko (from versions: none) ERROR: No matching distribution found for paramiko WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping [root@192-168-1-33 Python-3.10.4]# ``` - 如果没有安装zlib*,在make install时则会有如下报错: ```shell zipimport.ZipImportError: can't decompress data; zlib not available ``` # 三、下载Python 3.10.4 安装包 可以从官网Python的官网下载后上传到服务器,也可以通过wget在服务器内直接下载 Python官网:https://www.python.org/downloads/release/python-3104/ ![Python官网下载3.10.4.png](https://www.xiaoleizhang.com/usr/uploads/2022/05/263692519.png) 通过wget在服务器内部下载 ```shell wget https://www.python.org/ftp/python/3.10.4/Python-3.10.4.tgz ``` # 四、安装Python 3.10.4 解压安装包 ```shell tar zxvf Python-3.10.4.tgz ``` 添加配置 ```shell cd Python-3.10.4/ ./configure --prefix=/usr/local/python3 ``` 成功后显示 ```shell checking whether OpenSSL provides required APIs... yes checking for --with-ssl-default-suites... python checking for --with-builtin-hashlib-hashes... md5,sha1,sha256,sha512,sha3,blake2 checking for --with-experimental-isolated-subinterpreters... no checking for --with-static-libpython... yes checking for --disable-test-modules... no configure: creating ./config.status config.status: creating Makefile.pre config.status: creating Misc/python.pc config.status: creating Misc/python-embed.pc config.status: creating Misc/python-config.sh config.status: creating Modules/ld_so_aix config.status: creating pyconfig.h creating Modules/Setup.local creating Makefile If you want a release build with all stable optimizations active (PGO, etc), please run ./configure --enable-optimizations [root@192-168-1-33 Python-3.10.4]# ``` # 五、编译源码 在刚才解压的目录下继续编译 ```shell [root@192-168-1-33 Python-3.10.4]# make ``` 成功后显示 ```shell Python build finished successfully! The necessary bits to build these optional modules were not found: _bz2 _curses _curses_panel _dbm _gdbm _lzma _sqlite3 _tkinter _uuid readline To find the necessary bits, look in setup.py in detect_modules() for the module's name. The following modules found by detect_modules() in setup.py, have been built by the Makefile instead, as configured by the Setup files: _abc pwd time running build_scripts creating build/scripts-3.10 copying and adjusting /root/Python-3.10.4/Tools/scripts/pydoc3 -> build/scripts-3.10 copying and adjusting /root/Python-3.10.4/Tools/scripts/idle3 -> build/scripts-3.10 copying and adjusting /root/Python-3.10.4/Tools/scripts/2to3 -> build/scripts-3.10 changing mode of build/scripts-3.10/pydoc3 from 644 to 755 changing mode of build/scripts-3.10/idle3 from 644 to 755 changing mode of build/scripts-3.10/2to3 from 644 to 755 renaming build/scripts-3.10/pydoc3 to build/scripts-3.10/pydoc3.10 renaming build/scripts-3.10/idle3 to build/scripts-3.10/idle3.10 renaming build/scripts-3.10/2to3 to build/scripts-3.10/2to3-3.10 /usr/bin/install -c -m 644 ./Tools/gdb/libpython.py python-gdb.py gcc -pthread -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -fvisibility=hidden -I./Include/internal -I. -I./Include -DPy_BUILD_CORE -o Programs/_testembed.o ./Programs/_testembed.c gcc -pthread -Xlinker -export-dynamic -o Programs/_testembed Programs/_testembed.o libpython3.10.a -lcrypt -lpthread -ldl -lutil -lm -lm sed -e "s,@EXENAME@,/usr/local/python3/bin/python3.10," < ./Misc/python-config.in >python-config.py LC_ALL=C sed -e 's,\$(\([A-Za-z0-9_]*\)),\$\{\1\},g' < Misc/python-config.sh >python-config [root@192-168-1-33 Python-3.10.4]# ``` # 六、执行安装 继续在刚才解压的目录下执行安装 ```shell [root@192-168-1-33 Python-3.10.4]# make install ``` 成功后显示 ```shell Looking in links: /tmp/tmpucqurdbe Processing /tmp/tmpucqurdbe/setuptools-58.1.0-py3-none-any.whl Processing /tmp/tmpucqurdbe/pip-22.0.4-py3-none-any.whl Installing collected packages: setuptools, pip WARNING: The scripts pip3 and pip3.10 are installed in '/usr/local/python3/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. Successfully installed pip-22.0.4 setuptools-58.1.0 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv [root@192-168-1-33 Python-3.10.4]# ``` 两个warning我查了下,可以忽略。 # 七、配置环境变量 将python3的目录复制到/usr/bin/目录下 ```shell cp -r /usr/local/python3/ /usr/bin/ ``` 对/usr/bin目录下的python3目录重命名为python3.10.4 ```shell mv /usr/bin/python3 /usr/bin/python3.10.4 ``` 建立软连接 ```shell ln -s /usr/local/python3/bin/python3.10 /usr/bin/python3 ``` 配置环境变量 ```shell vim /etc/profile ``` 编辑/etc/profile在最底下加入下面的内容 ```shell #Python3.10.4 PATH=$PATH:$HOME/bin:/usr/bin/python3/bin export PATH ``` 修改完执行下面的命令,让配置的修改生效 ```shell source /etc/profile ``` 检查验证 ```shell [root@192-168-1-33 Python-3.10.4]# python3 -V Python 3.10.4 [root@192-168-1-33 Python-3.10.4]# ``` # 八、安装模块 建立pip3的软连接 ```shell ln -s /usr/local/python3/bin/pip3.10 /usr/bin/pip3 ``` 安装模块 ```shell pip3 install paramiko ``` 成功后显示 ```shell [root@192-168-1-33 Python-3.10.4]# pip3 install paramiko Collecting paramiko Downloading paramiko-2.10.4-py2.py3-none-any.whl (212 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 212.1/212.1 KB 578.3 kB/s eta 0:00:00 Collecting cryptography>=2.5 Downloading cryptography-37.0.2-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.1/4.1 MB 351.7 kB/s eta 0:00:00 Collecting pynacl>=1.0.1 Downloading PyNaCl-1.5.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.1/1.1 MB 594.0 kB/s eta 0:00:00 Collecting six Downloading six-1.16.0-py2.py3-none-any.whl (11 kB) Collecting bcrypt>=3.1.3 Downloading bcrypt-3.2.2-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (61 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.0/61.0 KB 536.0 kB/s eta 0:00:00 Collecting cffi>=1.1 Downloading cffi-1.15.0-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (446 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 446.3/446.3 KB 705.3 kB/s eta 0:00:00 Collecting pycparser Downloading pycparser-2.21-py2.py3-none-any.whl (118 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 118.7/118.7 KB 732.1 kB/s eta 0:00:00 Installing collected packages: six, pycparser, cffi, pynacl, cryptography, bcrypt, paramiko Successfully installed bcrypt-3.2.2 cffi-1.15.0 cryptography-37.0.2 paramiko-2.10.4 pycparser-2.21 pynacl-1.5.0 six-1.16.0 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv WARNING: You are using pip version 22.0.4; however, version 22.1 is available. You should consider upgrading via the '/usr/local/python3/bin/python3.10 -m pip install --upgrade pip' command. [root@192-168-1-33 Python-3.10.4]# ``` 最后修改:2022 年 05 月 14 日 © 允许规范转载 赞 0 如果觉得我的文章对你有用,请随意赞赏