Linux下编译安装python

下载

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是在Linux编译安装OpenCV-Python的步骤: 1. 安装依赖项 在终端中输入以下命令安装OpenCV-Python所需的所有依赖项: ``` sudo apt-get update sudo apt-get install build-essential cmake git pkg-config libgtk-3-dev \ libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev \ libx264-dev libjpeg-dev libpng-dev libtiff-dev gfortran openexr libatlas-base-dev \ python3-dev python3-numpy libtbb2 libtbb-dev libdc1394-22-dev ``` 2. 下载OpenCV源代码 从OpenCV的官方网站(https://opencv.org/releases/)下载最新版本的源代码,并将其解压缩到您的系统中: ``` wget -O opencv.zip https://github.com/opencv/opencv/archive/master.zip unzip opencv.zip ``` 3. 下载OpenCV-contrib源代码 从OpenCV的官方网站(https://opencv.org/releases/)下载最新版本的OpenCV-contrib源代码,并将其解压缩到您的系统中: ``` wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/master.zip unzip opencv_contrib.zip ``` 4. 编译OpenCV 在终端中进入OpenCV源代码目录并创建一个新的构建目录: ``` cd opencv-master mkdir build cd build ``` 使用cmake配置OpenCV的构建选项: ``` cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_C_EXAMPLES=OFF \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-master/modules \ -D BUILD_EXAMPLES=ON .. ``` 请注意,上面的命令中的OPENCV_EXTRA_MODULES_PATH参数指定了OpenCV-contrib源代码目录中的modules子目录。 接下来,使用以下命令编译OpenCV: ``` make -j4 ``` 请注意,上面的命令中的-j参数指定了使用多少个CPU核心进行编译。 最后,使用以下命令安装OpenCV: ``` sudo make install ``` 5. 安装OpenCV-Python 在终端中输入以下命令安装OpenCV-Python: ``` sudo apt-get install python3-opencv ``` 6. 测试OpenCV-Python 打开Python解释器并导入OpenCV模块: ``` import cv2 ``` 如果没有任何错误消息,则说明OpenCV-Python已成功安装

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值