Skip to main content

Download tools from w64devkit with Python.

Project description

py_win_x86_64_gcc

Use a bundled Windows x86_64 GCC toolchain from Python.

py_win_x86_64_gcc packages the w64devkit toolchain and provides small helper functions that return executable paths such as gcc.exe, g++.exe, make.exe, and 7zr.exe. It is useful for Python scripts that need a predictable C/C++ compiler on 64-bit Windows without asking users to install MinGW or modify PATH manually.

[!IMPORTANT] This project simply packages 7zr.exe and w64devkit.exe without any modifications.

This project is only compatible with 64-bit Windows systems based on the x86_64 architecture.

What is included

  • 7zr.exe, the standalone 7-Zip extractor.
  • w64devkit-x64-2.7.0, including GCC, G++, GNU Make, GDB, and related tools.
  • Python helpers for locating tools inside the extracted w64devkit/bin directory.

Installation

pip install py_win_x86_64_gcc

Quick start

[!NOTE] The first import or tool lookup may extract w64devkit from the bundled archive. This usually takes about ten seconds.

from py_win_x86_64_gcc import get_all_tools, get_tool

# List executable files available from w64devkit/bin.
print(get_all_tools())

# Get absolute paths for specific tools.
print(get_tool("7zr"))
print(get_tool("g++"))
print(get_tool("gcc"))

Compile a C++ file

import subprocess

from py_win_x86_64_gcc import get_tool

gxx = get_tool("g++")
if gxx is None:
    raise RuntimeError("g++ was not found")

ret1 = subprocess.run(
    [gxx, "test_gcc.cpp", "-o", "test_gcc.exe"],
    check=True,
)
ret2 = subprocess.run(["test_gcc.exe"])

Tool names may be passed with or without a suffix:

assert get_tool("gcc") == get_tool("gcc.exe")
assert get_tool("g++") == get_tool("g++.exe")

API

get_tool(tool_name: str, force_unzip: bool = False) -> str | None

Returns the absolute path to a bundled tool.

  • tool_name may be gcc, gcc.exe, g++, make, gdb, 7zr, and so on.
  • 7zr is returned from the package data directory.
  • Other tools are returned from w64devkit/bin.
  • None is returned when the requested tool is not available.
  • Set force_unzip=True to remove and re-extract the bundled w64devkit directory before looking up the tool.

get_all_tools(force_unzip: bool = False) -> list[str]

Returns sorted executable filenames available in w64devkit/bin, plus 7zr.exe.

unzip(force: bool = False, quiet: bool = False) -> bool

Extracts the bundled w64devkit archive when needed and returns True when the toolchain directory is ready.

  • force=True removes the existing extracted directory and extracts again.
  • quiet=True suppresses the py_win_x86_64_gcc: unzip ... message.

First-use extraction and multiprocessing

The extracted w64devkit directory is intentionally excluded from the Python package and is created on first use. The package uses a lock file during extraction, so multiple Python processes can import the package or call get_tool() at the same time. Only one process performs the extraction; the others wait and then reuse the completed directory.

If extraction is interrupted and leaves an incomplete directory behind, the next call will remove the incomplete directory and extract it again.

Subprocess usage tips

Prefer passing the tool path as the first item in a subprocess argument list:

import subprocess

from py_win_x86_64_gcc import get_tool

subprocess.run(
    [get_tool("gcc"), "--version"],
    check=True,
)

You do not need to add w64devkit/bin to PATH for this style of invocation.

Some build systems expect compiler tools to be discoverable from PATH. In that case, prepend the parent directory of gcc.exe for the subprocess only:

import os
import subprocess

from py_win_x86_64_gcc import get_tool

gcc = get_tool("gcc")
if gcc is None:
    raise RuntimeError("gcc was not found")

env = os.environ.copy()
env["PATH"] = os.path.dirname(gcc) + os.pathsep + env["PATH"]

subprocess.run(["make"], check=True, env=env)

Troubleshooting

  • FileNotFoundError for 7zr.exe or w64devkit-x64-2.7.0.7z.exe usually means the package data files were not installed correctly.
  • get_tool(...) returning None means that executable is not included in w64devkit/bin or the name is misspelled.
  • This package is Windows-only and targets x86_64 systems. It is not intended for Linux, macOS, Windows ARM64, or 32-bit Windows.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_win_x86_64_gcc-0.1.2.tar.gz (58.7 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_win_x86_64_gcc-0.1.2-py3-none-any.whl (58.7 MB view details)

Uploaded Python 3

File details

Details for the file py_win_x86_64_gcc-0.1.2.tar.gz.

File metadata

  • Download URL: py_win_x86_64_gcc-0.1.2.tar.gz
  • Upload date:
  • Size: 58.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.11.15 Windows/10

File hashes

Hashes for py_win_x86_64_gcc-0.1.2.tar.gz
Algorithm Hash digest
SHA256 92b3f488bcec8994f8b08ad985018d8852378660208bc7c43a968175dafb0e3a
MD5 740e49df1d634415b6cd7688cc923005
BLAKE2b-256 2c47835ac58fc8922ee7a02da4a736cf90fcecdc309f2d4301e522bc6abe9c12

See more details on using hashes here.

File details

Details for the file py_win_x86_64_gcc-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: py_win_x86_64_gcc-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 58.7 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.11.15 Windows/10

File hashes

Hashes for py_win_x86_64_gcc-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 25573c28e48dff095b10d88997f865b32d3dff0987bd130117f7c25ba65f62e0
MD5 483fe04e5900914b81b085231c1d2494
BLAKE2b-256 823424563056cf02e64fdddfd912c0314eb783a6b134954ae85dbdb7d0d99343

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page