Python3.7.2 +Windows 10 + visual studio 2017编译安装
在windows上玩Python编译学习,基本上没什么要说的,Python把一切工作都做得很到位,几乎没什么难度。
(如果你需要学习Python源码,那就要调试版,此时要加上--with-pydebug,更详细的过程可以参考官方说明:
https://devguide.python.org/)
另外要说明的是,这里所说的Python,是指我们一般情况下使用的CPython。
编译过程
安装好visual studio, 在安装时选定python3(64)支持和python工具。
Python的官网上下载:Python-3.7.2.tar.xz
https://www.python.org/downloads/source/
https://www.python.org/downloads/release/python-372/
https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz
解压完后, win+R启动cmd界面,直接进入PCBuild目录
D:\dir\Python372\PCbuild
输入命令build即可开始编译,过程中会下载一些文件包,请保持网络畅通。(这一步可以省掉,如果没下载那些包visual studio也会去下载,会不会有所不同就不知道了,反正我的tcltk和openssl都是自己手动配置的)
用visual studio打开pcbuild.sln文件,点击项目重新开始编译(注意选64位平台,debug模式),因为前面已经下载了相关文件,项目又比较小,很快就会编译好,此时会有一些报错,主要是tcltk-8.6.8.0和openssl的相关文件和库没有准备好(官方提供的配置不知道要到哪里去下载,总之对应不上),要去下载安装,并到相关项目下配置就OK了。
相关包
tcltk包是这个:ActiveTcl-8.6.8.0-MSWin32-x64.exe,注意配置库文件的时候需要配置tcl86t.lib和tk86t.lib,否则会报无法链接成功。
在这里下载:
https://www.activestate.com/products/activetcl/
Openssl包貌似各种版本都可以,我用的是这个:Win64OpenSSL-1_1_1a.exe
下载地址:
https://slproweb.com/products/Win32OpenSSL.html
https://slproweb.com/download/Win64OpenSSL-1_1_1a.exe
我用的上述包编译,一次全部通过,具体如何添加头文件和库文件就不用说了吧!
python开发的官网指导
我的编译过程和官网的不同,官网推荐采用git来管理代码库,原文在这里:
https://devguide.python.org/
https://cpython-devguide.readthedocs.io/index.html
其中的步骤描述摘录如下
-
Install and set up Git and other dependencies (see the Get Setup page for detailed information).
-
Fork the CPython repository to your GitHub account and get the source code using:
git clone https://github.com/<your_username>/cpython
-
Build Python, on UNIX and Mac OS use:
./configure --with-pydebug && make -j
and on Windows use:
PCbuild\build.bat -e -d
See also more detailed instructions, how to install and build dependencies, and the plaform-specific pages for UNIX, Mac OS, and Windows.
-
./python -m test -j3
On most Mac OS X systems, replace
./python
with./python.exe
. On Windows, usepython.bat
. With Python 2.7, replacetest
withtest.regrtest
. -
Create a new branch where your work for the issue will go, e.g.:
git checkout -b fix-issue-12345 master
If an issue does not already exist, please create it. Trivial issues (e.g. typo fixes) do not require any issue to be created.
-
Once you fixed the issue, run the tests, run
make patchcheck
, and if everything is ok, commit.
Linux Ubuntu18.04
现在18.04新安装的系统没有gcc,在安装Python前需要手动安装gcc
$sudo apt-get install build-essential checkinstall
然后,一般命令列一下
./configure --prefix=/usr/local --with-pydebug
make -j8 -s
make install
详情参考:《Ubuntu中编译安装多个Python版本》
https://blog.csdn.net/tanmx219/article/details/86518446
源码开发与解读
https://cpython-devguide.readthedocs.io/
https://docs.python.org/3/contents.html
https://devguide.python.org/exploring/ (这里以表格的形式列出了不少参考资料,我在下面也同样列出)
Title | Brief | Author | Version |
---|---|---|---|
A guide from parser to objects, observed using GDB | Code walk from Parser, AST, Sym Table and Objects | Louie Lu | 3.7.a0 |
Green Tree Snakes | The missing Python AST docs | Thomas Kluyver | 3.6 |
Yet another guided tour of CPython | A guide for how CPython REPL works | Guido van Rossum | 3.5 |
Python Asynchronous I/O Walkthrough | How CPython async I/O, generator and coroutine works | Philip Guo | 3.5 |
Coding Patterns for Python Extensions | Reliable patterns of coding Python Extensions in C | Paul Ross | 3.4 |
Title | Brief | Author | Version |
---|---|---|---|
Python’s Innards Series | ceval, objects, pystate and miscellaneous topics | Yaniv Aknin | 3.1 |
Eli Bendersky’s Python Internals | Objects, Symbol tables and miscellaneous topics | Eli Bendersky | 3.x |
A guide from parser to objects, observed using Eclipse | Code walk from Parser, AST, Sym Table and Objects | Prashanth Raghu | 2.7.12 |
CPython internals: A ten-hour codewalk through the Python interpreter source code | Code walk from source code to generators | Philip Guo | 2.7.8 |
附加资料:
使用visual studio2017 可同时对python源码和C/C++源码进行调试:
https://docs.microsoft.com/en-us/visualstudio/python/debugging-mixed-mode-c-cpp-python-in-visual-studio?view=vs-2017