Skip to content

Commit cdadef2

Browse files
committed
fix: use system Python with --target for Windows embeddable builds
The embeddable Python distribution lacks development headers (Python.h) required to compile native extensions. When using the embeddable Python's pip directly, packages with native code fail to build with: fatal error C1083: Cannot open include file: 'Python.h' This fix uses the system Python (which has headers) to compile packages, while directing the output to the embeddable Python's site-packages directory using pip's --target flag. Changes: - build-windows-executable-app.yaml: Use system Python with --target, remove unnecessary pip installation in embeddable Python - test-win-exe-w-embed-py.yaml: Add setup-python action, use system Python with --target, remove pip installation step - docs/win_exe_with_embed_py.md: Add prerequisites section explaining the need for system Python, update install command to use --target https://claude.ai/code/session_0152zcfBS5dWLtM12ubuYrKC
1 parent 8cb8b57 commit cdadef2

File tree

3 files changed

+30
-35
lines changed

3 files changed

+30
-35
lines changed

.github/workflows/build-windows-executable-app.yaml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,18 +235,15 @@ jobs:
235235
cp $PYTHON_DIR/DLLs/tcl86t.dll $EMBED_DIR/
236236
cp $PYTHON_DIR/DLLs/tk86t.dll $EMBED_DIR/
237237
238-
- name: Install pip
239-
run: |
240-
curl -O https://bootstrap.pypa.io/get-pip.py
241-
./python-${{ env.PYTHON_VERSION }}/python get-pip.py --no-warn-script-location
242-
rm get-pip.py
243-
244238
- name: Uncomment 'import site' in python311._pth file
245239
run: |
246240
sed -i 's/#import site/import site/' python-${{ env.PYTHON_VERSION }}/python311._pth
247241
248242
- name: Install Required Packages
249-
run: .\python-${{ env.PYTHON_VERSION }}\python -m pip install --force-reinstall -r requirements.txt --no-warn-script-location
243+
# Use system Python (which has development headers) to compile packages,
244+
# installing into the embeddable Python's site-packages directory.
245+
# The embeddable Python lacks Python.h headers needed for native extensions.
246+
run: python -m pip install -r requirements.txt --target python-${{ env.PYTHON_VERSION }}/Lib/site-packages --upgrade --no-warn-script-location
250247

251248
- name: Set to offline deployment
252249
run: |

.github/workflows/test-win-exe-w-embed-py.yaml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,31 @@ jobs:
1717
- name: Checkout code
1818
uses: actions/checkout@v4
1919

20+
- name: Set up Python (regular distribution)
21+
uses: actions/setup-python@v5
22+
with:
23+
python-version: ${{ env.PYTHON_VERSION }} # Use the same version as the embeddable version
24+
2025
- name: Download python embeddable version
2126
run: |
2227
mkdir python-${{ env.PYTHON_VERSION }}
2328
curl -O https://www.python.org/ftp/python/${{ env.PYTHON_VERSION }}/python-${{ env.PYTHON_VERSION }}-embed-amd64.zip
2429
unzip python-${{ env.PYTHON_VERSION }}-embed-amd64.zip -d python-${{ env.PYTHON_VERSION }}
2530
rm python-${{ env.PYTHON_VERSION }}-embed-amd64.zip
2631
27-
- name: Install pip
28-
run: |
29-
curl -O https://bootstrap.pypa.io/get-pip.py
30-
./python-${{ env.PYTHON_VERSION }}/python get-pip.py --no-warn-script-location
31-
rm get-pip.py
32-
3332
- name: Uncomment 'import site' in python311._pth file
3433
run: |
3534
sed -i 's/#import site/import site/' python-${{ env.PYTHON_VERSION }}/python311._pth
36-
35+
3736
- name: Print content of python311._pth file
3837
run: |
3938
cat python-${{ env.PYTHON_VERSION }}/python311._pth
4039
4140
- name: Install Required Packages
42-
run: .\python-${{ env.PYTHON_VERSION }}\python -m pip install -r requirements.txt --no-warn-script-location
41+
# Use system Python (which has development headers) to compile packages,
42+
# installing into the embeddable Python's site-packages directory.
43+
# The embeddable Python lacks Python.h headers needed for native extensions.
44+
run: python -m pip install -r requirements.txt --target python-${{ env.PYTHON_VERSION }}/Lib/site-packages --upgrade --no-warn-script-location
4345

4446
- name: Create .bat file
4547
run: |

docs/win_exe_with_embed_py.md

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
To create an executable for Streamlit app on Windows, we'll use an embeddable version of Python.</br>
44
Here's a step-by-step guide:
55

6+
### Prerequisites
7+
8+
You need a **system Python installation** (the regular Python installer from python.org) of the same version as the embeddable Python you'll download. This is required because the embeddable Python lacks development headers (`Python.h`) needed to compile native extensions.
9+
10+
Install Python 3.11.9 from https://www.python.org/downloads/ if you don't have it already.
11+
612
### Download and Extract Python Embeddable Version
713

814
1. Download a suitable Python embeddable version. For example, let's download Python 3.11.9:
@@ -22,24 +28,6 @@ Here's a step-by-step guide:
2228
rm python-3.11.9-embed-amd64.zip
2329
```
2430

25-
### Install pip
26-
27-
1. Download `get-pip.py`:
28-
29-
```bash
30-
# use curl command or manually download
31-
curl -O https://bootstrap.pypa.io/get-pip.py
32-
```
33-
34-
2. Install pip:
35-
36-
```bash
37-
./python-3.11.9/python get-pip.py --no-warn-script-location
38-
39-
# no need anymore get-pip.py
40-
rm get-pip.py
41-
```
42-
4331
### Configure Python Environment
4432

4533
1. Uncomment 'import site' in the `._pth` file:
@@ -55,12 +43,20 @@ Here's a step-by-step guide:
5543

5644
### Install Required Packages
5745

58-
Install all required packages from `requirements.txt`:
46+
Install all required packages from `requirements.txt` using the **system Python** with `--target` to install into the embeddable Python's site-packages:
5947

6048
```bash
61-
./python-3.11.9/python -m pip install -r requirements.txt --no-warn-script-location
49+
# Use system Python (which has development headers) to compile packages,
50+
# installing into the embeddable Python's site-packages directory.
51+
# The embeddable Python lacks Python.h headers needed for native extensions.
52+
python -m pip install -r requirements.txt --target python-3.11.9/Lib/site-packages --upgrade --no-warn-script-location
6253
```
6354

55+
> **Important**: Do NOT use `./python-3.11.9/python -m pip install ...` directly. The embeddable Python lacks the development headers required to compile native extensions (e.g., `Python.h`), which will cause builds to fail with errors like:
56+
> ```
57+
> fatal error C1083: Cannot open include file: 'Python.h': No such file or directory
58+
> ```
59+
6460
### Test and create `run_app.bat` file
6561
6662
1. Test by running app

0 commit comments

Comments
 (0)