Skip to main content

Lightweight Microsoft Visual C++ build tools for Python

Project description

MSVCLib

PyPI version Python Support License

English | 中文


中文

背景

在安装某些 Python 第三方包时,如果仓库中只有源码包且包含 C 语言代码,可能会遇到以下错误:

error: microsoft visual c++ 14.0 or greater is required. get it with "microsoft c++ build tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/

按照提示安装完整的 Visual Studio 会占用至少 6GB 的磁盘空间,这对于仅需要编译工具的用户来说过于臃肿。

解决方案

MSVCLib 基于 PortableBuildTools 项目,提供了一个轻量级的解决方案。它将必要的 Microsoft Visual C++ 构建工具打包成 Python wheel,可通过 pip 轻松安装。

v0.2.1 更新:修复了 uv tool install 安装方式的路径查找问题,现在完全支持 uv 工具链安装。

安装

使用 pip 安装

pip install msvclib

推荐:全局安装或使用 uv

uv tool install msvclib

使用方法

当遇到 Microsoft Visual C++ 14.0 相关错误时,只需运行初始化命令:

msvcinit

初始化完成后,即可正常编译需要 C++ 构建工具的 Python 包。

优势

  • 轻量级:相比完整的 Visual Studio 安装,占用空间极小
  • 便携性:无需管理员权限,可在任何环境中使用
  • 简单易用:一条命令即可解决编译问题
  • 多种安装方式:支持 pip、uv tool 等多种安装方式

从源代码构建

如果你想从源代码构建 MSVCLib,请按照以下步骤:

前置要求

  • Python 3.6+
  • uv 包管理器
  • 7-Zip 压缩工具

构建步骤

  1. 下载 MSVC 构建工具

    使用 PortableBuildTools 下载必要的 MSVC 文件到本地:

    git clone https://github.com/Data-Oriented-House/PortableBuildTools.git
    cd PortableBuildTools
    # 按照 PortableBuildTools 的说明下载 MSVC 工具
    
  2. 复制文件到 msvclib

    将下载的 MSVC 文件复制到 msvclib 目录中:

    # 将 PortableBuildTools 下载的文件复制到 msvclib/ 目录
    cp -r path/to/downloaded/msvc/files/* msvclib/
    
  3. 构建 wheel 包

    使用 uv 构建 wheel 包:

    uv build --wheel
    
  4. 重新压缩优化包大小

    使用 7-Zip 的 Bzip2 算法重新压缩 wheel 包以减小体积:

    # 解压原始 wheel 包
    7z x dist/msvclib-*.whl -o temp_wheel/
    
    # 使用 Bzip2 算法极限压缩重新打包
    cd temp_wheel/
    7z a -tbzip2 -mx9 ../dist/msvclib-optimized.whl *
    

    注意:使用 Bzip2 压缩可以显著减小包大小,且与 pip install 原生兼容。


English

Background

When installing certain Python third-party packages that contain only source code with C language components, you might encounter this error:

error: microsoft visual c++ 14.0 or greater is required. get it with "microsoft c++ build tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/

Installing the full Visual Studio as suggested would consume at least 6GB of disk space, which is excessive for users who only need the build tools.

Solution

MSVCLib is based on the PortableBuildTools project and provides a lightweight solution. It packages the necessary Microsoft Visual C++ build tools into a Python wheel that can be easily installed via pip.

v0.2.1 Update: Fixed path finding issues with uv tool install installation method, now fully supports uv toolchain installation.

Installation

Install with pip

pip install msvclib

Recommended: Global installation or using uv

uv tool install msvclib

Usage

When encountering Microsoft Visual C++ 14.0 related errors, simply run the initialization command:

msvcinit

After initialization, you can compile Python packages that require C++ build tools without issues.

Advantages

  • Lightweight: Minimal disk space usage compared to full Visual Studio installation
  • Portable: No administrator privileges required, works in any environment
  • Easy to use: One command solves compilation issues
  • Multiple installation methods: Supports pip, uv tool, and other installation methods

Building from Source

If you want to build MSVCLib from source, follow these steps:

Prerequisites

  • Python 3.6+
  • uv package manager
  • 7-Zip compression tool

Build Steps

  1. Download MSVC Build Tools

    Use PortableBuildTools to download necessary MSVC files locally:

    git clone https://github.com/Data-Oriented-House/PortableBuildTools.git
    cd PortableBuildTools
    # Follow PortableBuildTools instructions to download MSVC tools
    
  2. Copy Files to msvclib

    Copy the downloaded MSVC files to the msvclib directory:

    # Copy PortableBuildTools downloaded files to msvclib/ directory
    cp -r path/to/downloaded/msvc/files/* msvclib/
    
  3. Build Wheel Package

    Use uv to build the wheel package:

    uv build --wheel
    
  4. Recompress to Optimize Package Size

    Use 7-Zip's Bzip2 algorithm to recompress the wheel package for smaller size:

    # Extract original wheel package
    7z x dist/msvclib-*.whl -o temp_wheel/
    
    # Repackage with Bzip2 algorithm for maximum compression
    cd temp_wheel/
    7z a -tbzip2 -mx9 ../dist/msvclib-optimized.whl *
    

    Note: Using Bzip2 compression can significantly reduce package size while maintaining native pip install compatibility.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

msvclib-0.2.2-py3-none-any.whl (341.2 MB view details)

Uploaded Python 3

File details

Details for the file msvclib-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: msvclib-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 341.2 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for msvclib-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6f31dcfe1a9fece1d86fe92372eb3b56e78611568780dc3096eeb9cb21f0022f
MD5 0746452ab144ad5b187455527230f2c6
BLAKE2b-256 dac83554bde23f09a6d00ecafa22b7811d30ccc65740b0e1698b19dd1af2c4b7

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