下载
python的地址为https://www.python.org/downloads/source/,以下载版本3.11.0为例,下载后文件为Python-3.11.0.tgz。
编译
使用命令tar -zxvf Python-3.11.0.tgz
进行解压,解压完成后文件夹Python-3.11.0中目录结构如下:
[root@VM-12-15-centos Python-3.11.0]# ls -al
total 1364
drwxr-xr-x 16 lighthouse lighthouse 4096 Oct 25 01:48 .
dr-xr-x---. 7 root root 4096 Dec 29 14:12 ..
-rw-r--r-- 1 lighthouse lighthouse 24428 Oct 25 01:35 aclocal.m4
-rwxr-xr-x 1 lighthouse lighthouse 49348 Oct 25 01:35 config.guess
-rwxr-xr-x 1 lighthouse lighthouse 35276 Oct 25 01:35 config.sub
-rwxr-xr-x 1 lighthouse lighthouse 722182 Oct 25 01:35 configure
-rw-r--r-- 1 lighthouse lighthouse 219281 Oct 25 01:35 configure.ac
drwxr-xr-x 18 lighthouse lighthouse 4096 Oct 25 01:48 Doc
-rw-r--r-- 1 lighthouse lighthouse 181 Oct 25 01:35 .editorconfig
drwxr-xr-x 2 lighthouse lighthouse 4096 Oct 25 01:35 Grammar
drwxr-xr-x 4 lighthouse lighthouse 4096 Oct 25 01:35 Include
-rwxr-xr-x 1 lighthouse lighthouse 15358 Oct 25 01:35 install-sh
drwxr-xr-x 37 lighthouse lighthouse 4096 Oct 25 01:35 Lib
-rw-r--r-- 1 lighthouse lighthouse 13931 Oct 25 01:35 LICENSE
drwxr-xr-x 8 lighthouse lighthouse 4096 Oct 25 01:35 Mac
-rw-r--r-- 1 lighthouse lighthouse 97917 Oct 25 01:35 Makefile.pre.in
drwxr-xr-x 3 lighthouse lighthouse 4096 Oct 25 01:48 Misc
drwxr-xr-x 15 lighthouse lighthouse 4096 Oct 25 01:35 Modules
drwxr-xr-x 4 lighthouse lighthouse 4096 Oct 25 01:35 Objects
drwxr-xr-x 2 lighthouse lighthouse 4096 Oct 25 01:48 Parser
drwxr-xr-x 5 lighthouse lighthouse 4096 Oct 25 01:35 PC
drwxr-xr-x 2 lighthouse lighthouse 4096 Oct 25 01:35 PCbuild
drwxr-xr-x 2 lighthouse lighthouse 4096 Oct 25 01:35 Programs
-rw-r--r-- 1 lighthouse lighthouse 50603 Oct 25 01:35 pyconfig.h.in
drwxr-xr-x 5 lighthouse lighthouse 4096 Oct 25 01:35 Python
-rw-r--r-- 1 lighthouse lighthouse 9982 Oct 25 01:35 README.rst
-rw-r--r-- 1 lighthouse lighthouse 66086 Oct 25 01:35 setup.py
drwxr-xr-x 22 lighthouse lighthouse 4096 Oct 25 01:35 Tools
使用命令./configure
生成Makefile:
[root@VM-12-15-centos Python-3.11.0]# ./configure
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for Python interpreter freezing... ./_bootstrap_python
checking for python3.11... python3.11
checking Python for regen version... Python 3.11.0
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... "linux"
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
...
...
...
checking for stdlib extension module _bz2... missing
checking for stdlib extension module _lzma... yes
checking for stdlib extension module _ssl... missing
checking for stdlib extension module _hashlib... missing
checking for stdlib extension module _testcapi... yes
checking for stdlib extension module _testinternalcapi... yes
checking for stdlib extension module _testbuffer... yes
checking for stdlib extension module _testimportmultiple... yes
checking for stdlib extension module _testmultiphase... yes
checking for stdlib extension module _xxtestfuzz... yes
checking for stdlib extension module _ctypes_test... yes
checking for stdlib extension module xxlimited... yes
checking for stdlib extension module xxlimited_35... yes
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/Setup.bootstrap
config.status: creating Modules/Setup.stdlib
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
configure: creating Modules/Setup.local
configure: creating Makefile
configure:
If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure --enable-optimizations
然后使用命令make
进行编译:
[root@VM-12-15-centos Python-3.11.0]# make
gcc -pthread -c -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c11 -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -fvisibility=hidden -I./Include/internal -I. -I./Include -DPy_BUILD_CORE -o Programs/python.o ./Programs/python.c
gcc -pthread -c -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c11 -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -fvisibility=hidden -I./Include/internal -I. -I./Include -DPy_BUILD_CORE -o Parser/token.o Parser/token.c
gcc -pthread -c -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c11 -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -fvisibility=hidden -I./Include/internal -I. -I./Include -DPy_BUILD_CORE -o Parser/pegen.o Parser/pegen.c
gcc -pthread -c -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c11 -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -fvisibility=hidden -I./Include/internal -I. -I./Include -DPy_BUILD_CORE -o Parser/pegen_errors.o Parser/pegen_errors.c
gcc -pthread -c -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c11 -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -fvisibility=hidden -I./Include/internal -I. -I./Include -DPy_BUILD_CORE -o Parser/action_helpers.o Parser/action_helpers.c
gcc -pthread -c -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c11 -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -fvisibility=hidden -I./Include/internal -I. -I./Include -DPy_BUILD_CORE -o Parser/parser.o Parser/parser.c
...
...
...
The necessary bits to build these optional modules were not found:
_bz2 _curses _curses_panel
_gdbm _hashlib _ssl
_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 not
been built, they are *disabled* by configure:
_sqlite3
Failed to build these modules:
_ctypes
Could not build the ssl module!
Python requires a OpenSSL 1.1.1 or newer
running build_scripts
creating build/scripts-3.11
copying and adjusting /root/Python-3.11.0/Tools/scripts/pydoc3 -> build/scripts-3.11
copying and adjusting /root/Python-3.11.0/Tools/scripts/idle3 -> build/scripts-3.11
copying and adjusting /root/Python-3.11.0/Tools/scripts/2to3 -> build/scripts-3.11
changing mode of build/scripts-3.11/pydoc3 from 644 to 755
changing mode of build/scripts-3.11/idle3 from 644 to 755
changing mode of build/scripts-3.11/2to3 from 644 to 755
renaming build/scripts-3.11/pydoc3 to build/scripts-3.11/pydoc3.11
renaming build/scripts-3.11/idle3 to build/scripts-3.11/idle3.11
renaming build/scripts-3.11/2to3 to build/scripts-3.11/2to3-3.11
/usr/bin/install -c -m 644 ./Tools/gdb/libpython.py python-gdb.py
gcc -pthread -c -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c11 -Wextra -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 Modules/getbuildinfo.o Parser/token.o Parser/pegen.o Parser/pegen_errors.o Parser/action_helpers.o Parser/parser.o Parser/string_parser.o Parser/peg_api.o Parser/myreadline.o Parser/tokenizer.o Objects/abstract.o Objects/accu.o Objects/boolobject.o Objects/bytes_methods.o Objects/bytearrayobject.o Objects/bytesobject.o Objects/call.o Objects/capsule.o Objects/cellobject.o Objects/classobject.o Objects/codeobject.o Objects/complexobject.o Objects/descrobject.o Objects/enumobject.o Objects/exceptions.o Objects/genericaliasobject.o Objects/genobject.o Objects/fileobject.o Objects/floatobject.o Objects/frameobject.o Objects/funcobject.o Objects/interpreteridobject.o Objects/iterobject.o Objects/listobject.o Objects/longobject.o Objects/dictobject.o Objects/odictobject.o Objects/memoryobject.o Objects/methodobject.o Objects/moduleobject.o Objects/namespaceobject.o Objects/object.o Objects/obmalloc.o Objects/picklebufobject.o Objects/rangeobject.o Objects/setobject.o Objects/sliceobject.o Objects/structseq.o Objects/tupleobject.o Objects/typeobject.o Objects/unicodeobject.o Objects/unicodectype.o Objects/unionobject.o Objects/weakrefobject.o Python/_warnings.o Python/Python-ast.o Python/Python-tokenize.o Python/asdl.o Python/ast.o Python/ast_opt.o Python/ast_unparse.o Python/bltinmodule.o Python/ceval.o Python/codecs.o Python/compile.o Python/context.o Python/dynamic_annotations.o Python/errors.o Python/frame.o Python/frozenmain.o Python/future.o Python/getargs.o Python/getcompiler.o Python/getcopyright.o Python/getplatform.o Python/getversion.o Python/hamt.o Python/hashtable.o Python/import.o Python/importdl.o Python/initconfig.o Python/marshal.o Python/modsupport.o Python/mysnprintf.o Python/mystrtoul.o Python/pathconfig.o Python/preconfig.o Python/pyarena.o Python/pyctype.o Python/pyfpe.o Python/pyhash.o Python/pylifecycle.o Python/pymath.o Python/pystate.o Python/pythonrun.o Python/pytime.o Python/bootstrap_hash.o Python/specialize.o Python/structmember.o Python/symtable.o Python/sysmodule.o Python/thread.o Python/traceback.o Python/getopt.o Python/pystrcmp.o Python/pystrtod.o Python/pystrhex.o Python/dtoa.o Python/formatter_unicode.o Python/fileutils.o Python/suggestions.o Python/dynload_shlib.o Modules/config.o Modules/main.o Modules/gcmodule.o Modules/atexitmodule.o Modules/faulthandler.o Modules/posixmodule.o Modules/signalmodule.o Modules/_tracemalloc.o Modules/_codecsmodule.o Modules/_collectionsmodule.o Modules/errnomodule.o Modules/_io/_iomodule.o Modules/_io/iobase.o Modules/_io/fileio.o Modules/_io/bytesio.o Modules/_io/bufferedio.o Modules/_io/textio.o Modules/_io/stringio.o Modules/itertoolsmodule.o Modules/_sre/sre.o Modules/_threadmodule.o Modules/timemodule.o Modules/_weakref.o Modules/_abc.o Modules/_functoolsmodule.o Modules/_localemodule.o Modules/_operator.o Modules/_stat.o Modules/symtablemodule.o Modules/pwdmodule.o Modules/xxsubtype.o Python/deepfreeze/deepfreeze.o Modules/getpath.o Python/frozen.o -lpthread -ldl -lutil -lm
sed -e "s,@EXENAME@,/usr/local/bin/python3.11," < ./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
最后使用命令make install
进行安装即可。
安装完成后,使用python3 --version即可查看安装情况:
[root@VM-12-15-centos ~]# python3 --version
Python 3.11.0