6

I am wondering how to build embeddable Python for Windows. Could not find any info or build configuration for that particular package.

Reason I ask is that I could use the same variant for Mac and Linux.

Vincent Savard
  • 34,979
  • 10
  • 68
  • 73
abergmeier
  • 13,224
  • 13
  • 64
  • 120
  • 1
    what do you mean build embeddable? You can build Python and embed it yourself, there's no notion of an embeddable build AFAIK. – Dimitris Fasarakis Hilliard Feb 16 '17 at 17:31
  • 1
    If you look at the Windows download page, there is a package called embeddable Python. This one. – abergmeier Feb 16 '17 at 17:34
  • 1
    One thing I should note is that you have to include (at least a part of) Python stdlib, including Python code, in your embedded version, because the interpreter actively uses things like codecs and import machinery internally. – 9000 Feb 16 '17 at 17:34
  • @9000 yes, specifically [these](http://stackoverflow.com/questions/40634115/which-standard-library-modules-are-required-to-run-the-python-3-5-interpreter/40740974#40740974) in `3.5` from what I had found out (On Ubuntu, at least). Could you give us the link you're talking about, abergmeier? – Dimitris Fasarakis Hilliard Feb 16 '17 at 17:36
  • See https://www.python.org/downloads/windows/ - _Windows x86-64 embeddable zip file_ – abergmeier Feb 16 '17 at 17:38
  • Looks like it's described [here](https://blogs.msdn.microsoft.com/pythonengineering/2016/04/26/cpython-embeddable-zip-file/) and [here](http://stackoverflow.com/questions/37633550/python-embeddable-zip). I don't see any useful information for building that version in the [Windows build readme](https://github.com/python/cpython/blob/master/PCbuild/readme.txt) or the [Windows build script](https://github.com/python/cpython/blob/master/PCbuild/build.bat). – user2357112 Feb 16 '17 at 17:48
  • If you want to embed Python in your application on Unix, you should probably look at the Python documentation section on "Extending and Embedding the Python Interpreter", particularly [this bit](https://docs.python.org/3/extending/embedding.html#compiling-and-linking-under-unix-like-systems). – user2357112 Feb 16 '17 at 17:54

1 Answers1

7

Recently, I had to build an embeddable zip for 64bit Python 3.5.6 (since the official download page doesn't offer them for Python greater than 3.5.4), here are the steps.

Prerequisites (one-time install)

Install

  1. git
  2. TortoiseSVN
  3. Visual Studio C++ Build Tools 2015 (you may need version 2017 when building latest Python versions)1
  4. If you haven't Sphinx installed, install it and add sphinx-build to PATH2:

    > python -m pip install --user sphinx
    > set PATH=%PATH%;C:\Users\jenkins\AppData\Roaming\Python\Python35\Scripts
    
  5. If you haven't blurb installed, install it, add blurb to PATH if necessary (if you've done the previous step, you already have the correct PATH):

    > python -m pip install --user blurb
    > set PATH=%PATH%;C:\Users\jenkins\AppData\Roaming\Python\Python35\Scripts
    

Configure build environment

  1. Clone CPython repo. Here, I clone only the tag I need:

    > git clone --depth 1 --branch v3.5.6 https://github.com/python/cpython
    
  2. In the repo dir, generate NEWS file3, 4:

    > blurb merge -f Misc\NEWS
    

Build

Now you can trigger the build:

> Tools\msi\buildrelease.bat -x64 --skip-nuget --test testout

Replace -x64 with -x32 if you target the 32bit arch. --skip-nuget will omit building the MSI installer. --test testout will trigger installer tests; you can skip them if you want.

Once the build finishes, check for build artifact PCbuild\amd64\en-us\python-3.5.6-embed-amd64.zip. This is your embeddable zip.

After the build succeeds for the first time, you can skip the doc build in future builds for further speedup as long as the doc build artifacts remain in repo:

> Tools\msi\buildrelease.bat -x64 --skip-doc --skip-nuget --test testout

1 Make sure you select "Custom" in the VC++ Build Tools installer and check all the options; otherwise, you may get build errors like The code execution cannot proceed because ucrtbased.dll was not found etc. I'm no Windows expert by all means, just installed everything possible to get rid of the errors.

2 Looking at the buildrelease.bat help, it seems like you can skip the doc build with --skip-doc flag, but when using that, I got the error

"C:\Users\jenkins\projects\cpython\Tools\msi\bundle\releaselocal.wixproj" 
(Rebuild target) (1) ->
  "C:\Users\jenkins\projects\cpython\Tools\msi\doc\doc.wixproj" (Rebuild target) (7) ->
(Link target) ->
  C:\Users\jenkins\projects\cpython\Tools\msi\doc\doc_files.wxs(8): error 
LGHT0103: The system cannot find the file 'python356.chm'. [C:\Users\jenkins\projects\cpython\Tools\msi\doc\doc.wixproj]

and the build aborts with an error. If you manage to circumvent this without an initial doc build (so the doc files are available in the build dir), you can skip Sphinx install.

3 Otherwise, I get the build error

"C:\Users\jenkins\projects\cpython\Tools\msi\bundle\releaselocal.wixproj" (Rebuild target) (1) ->
"C:\Users\jenkins\projects\cpython\Tools\msi\exe\exe.wixproj" (Rebuild target) (8) ->
  C:\Users\jenkins\projects\cpython\Tools\msi\exe\exe_files.wxs(9): error 
LGHT0103: The system cannot find the file '!(bindpath.src)Misc\NEWS'. [C:\Users\jenkins\projects\cpython\Tools\msi\exe\exe.wixproj]

4 You can replace the command with an appropriate make invocation, but I was too lazy to decipher the Doc\Makefile. After all, the news won't land in the embeddable zip anyway; this is only to make the buildrelease.bat happy.

hoefling
  • 59,418
  • 12
  • 147
  • 194
  • `LINK : fatal error LNK1104: cannot open file 'pgort.lib' [\Python-3.8.3\PCbuild\pythoncore.vcxproj]` any idea why + how to fix? – evandrix Jun 28 '20 at 03:43