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/

https://www.activestate.com/products/activetcl/downloads/thank-you/?dl=https://downloads.activestate.com/ActiveTcl/releases/8.6.8.0/ActiveTcl-8.6.8.0-MSWin32-x64.exe

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

其中的步骤描述摘录如下

  1. Install and set up Git and other dependencies (see the Get Setup page for detailed information).

  2. Fork the CPython repository to your GitHub account and get the source code using:

    git clone https://github.com/<your_username>/cpython
    
  3. 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 instructionshow to install and build dependencies, and the plaform-specific pages for UNIXMac OS, and Windows.

  4. Run the tests:

    ./python -m test -j3
    

    On most Mac OS X systems, replace ./python with ./python.exe. On Windows, use python.bat. With Python 2.7, replace test with test.regrtest.

  5. 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.

  6. 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/ (这里以表格的形式列出了不少参考资料,我在下面也同样列出)

 

Current references
TitleBriefAuthorVersion
A guide from parser to objects, observed using GDBCode walk from Parser, AST, Sym Table and ObjectsLouie Lu3.7.a0
Green Tree SnakesThe missing Python AST docsThomas Kluyver3.6
Yet another guided tour of CPythonA guide for how CPython REPL worksGuido van Rossum3.5
Python Asynchronous I/O WalkthroughHow CPython async I/O, generator and coroutine worksPhilip Guo3.5
Coding Patterns for Python ExtensionsReliable patterns of coding Python Extensions in CPaul Ross3.4
Historical references
TitleBriefAuthorVersion
Python’s Innards Seriesceval, objects, pystate and miscellaneous topicsYaniv Aknin3.1
Eli Bendersky’s Python InternalsObjects, Symbol tables and miscellaneous topicsEli Bendersky3.x
A guide from parser to objects, observed using EclipseCode walk from Parser, AST, Sym Table and ObjectsPrashanth Raghu2.7.12
CPython internals: A ten-hour codewalk through the Python interpreter source codeCode walk from source code to generatorsPhilip Guo2.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

 

 

 

posted @ 2019-01-11 10:29  SpaceVision  阅读(88)  评论(0编辑  收藏  举报