Python3.7源碼編譯

  • 商洛在線
  • 2023-06-27 22:12:59
  • 來源:博客園


(相關資料圖)

1.下載Python3.7.0源碼

gitclonehttps://github.com/python/cpython.gitgitcheckoutv3.7.0
wgethttps://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz

源碼目錄結構如下所示:(1)Include目錄:包含Python提供的所有頭文件,如果用戶需要自己用C或C++來編寫自定義模塊擴展Python,那么就需要用到這里提供的頭文件。(2)Lib目錄:包含了Python自帶的所有標準庫,且都是用Python語言編寫的。(3)Modules目錄:包含了所有用C語言編寫的模塊,比如math、hashlib等。它們都是那些對速度要求非常嚴格的模塊。而相比而言,Lib目錄下則是存放一些對速度沒有太嚴格要求的模塊,比如os。(4)Parser目錄:包含了Python解釋器中的Scanner和Parser部分,即對Python源代碼進行詞法分析和語法分析的部分。除此以外,此目錄還包含了一些有用的工具,這些工具能夠根據Python語言的語法自動生成Python語言的詞法和語法分析器,與YACC(Yet Another Compiler Compiler)非常類似。(5)Objects目錄:包含了所有Python的內建對象,包括整數、list、dict等。同時,該目錄還包括了Python在運行時需要的所有的內部使用對象的實現。(6)Python目錄:包含了Python解釋器中的Compiler和執行引擎部分,是Python運行的核心所在。(7)PCbuild目錄:包含了Visual Studio 2003的工程文件,研究Python源代碼就從這里開始。(8)Programs目錄:包含了Python二進制可執行文件的源碼。

2.編譯和安裝Python3.7.0源碼libffi是Python中用來支持C擴展的庫:

sudoaptinstall-yzlib1gzlib1g-devlibffi-devopenssllibssl-dev
./configure--prefix=/home/rasa/Downloads/PythonSorceCode/Python3.7_compilemakemakeinstall

make命令后報錯如下所示:因為openssl 1.0.1存在安全問題,所以Python3.7以上建議使用libressl代替openssl,故需通過源碼編譯安裝libressl,如下所示:

#下載和編譯libresslwgethttps://ftp.openbsd.org/pub/OpenBSD/LibreSSL/libressl-3.0.2.tar.gztar-zxvflibressl-3.0.2.tar.gzsudomkdir/usr/local/libresslcdlibressl-3.0.2./configure--prefix=/usr/local/libressl&&make&&sudomake#創建軟連接代替opensslsudomv/usr/bin/openssl/usr/bin/openssl.baksudomv/usr/include/openssl/usr/include/openssl.baksudoln-s/usr/local/libressl/bin/openssl/usr/bin/opensslsudoln-s/usr/local/libressl/include/openssl/usr/include/opensslecho/usr/local/libressl/lib>>/etc/ld.so.conf.d/libressl-3.0.2.confsudoldconfig-v#驗證是否安裝完成opensslversionexportLDFLAGS="-L/usr/local/libressl/lib"exportCPPFLAGS="-I/usr/local/libressl/include"exportPKG_CONFIG_PATH="/usr/local/libressl/lib/pkgconfig"

再次執行命令編譯Python3.7.0源碼:

./configure--prefix=/home/rasa/Downloads/PythonSorceCode/Python3.7_compilemakesudomakeinstall

參考文獻:[1]Python源代碼的組織:https://flaggo.github.io/python3-source-code-analysis/preface/code-organization/[2]Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_P:https://www.cnblogs.com/apexchu/p/16294733.html

標簽:

?

推薦More