search
HomeBackend DevelopmentPython TutorialPortable Python Bundles on Windows

Packaging a Python application and its environment on MS Windows for use by other users, making it "ready to run" on any machine, is a tricky task. This blog post describes my personal solution: something I call Python for Windows Bundle, which is similar to a virtual environment but is portable between machines.

The Python Bundle sits somewhat at the intersection of the value and trade-offs offered by virtual environments, regular Python installations, and standalone executables created by tools like PyInstaller or Py2exe.

No new tools are required to create such a Bundle. This is just a loose and lightweight convention for folder structure and some wrapper scripts that you can easily create manually. Or automate their creation in scripts or CI jobs.

Portable Python Bundles on Windows

Question

Let us assume that we want to package and deliver a Python application or environment to our users in a self-contained and "ready-to-run" manner.

We may not know which version of Python our users have installed, or it may not be installed at all. We definitely don't want to tamper with a Python installation they may already have, which includes not letting them request the installation of additional Python versions. In other words: our packages should be everything our users need to run our applications or use our Python environment.

Problems with virtual environment

After some brainstorming, we might think of creating a virtual environment (python -m venv venv_dir), installing everything into the virtual environment, and then zipping and distributing the virtual environment folder to our users. However, we realize that the virtual environment folder cannot be easily relocated to a different path than where it was created. Additionally, our virtual environment relies on the base Python installation used to create it (using the exact same Python version in that path). Therefore, we need to tell our users where to place their copies of the virtual environment. And they have to install a specific version of Python in a specific path. This is not what we want.

Problems with Python installation

Instead of a virtual environment we can install all our requirements into a regular Python installation and then zip and distribute its folder (e.g. c:Program FilesPython 3.13.1). This mostly works. The Python installation directory on Windows can usually be relocated to a different path (this is not the case on Unix due to static prefix paths - but that's another topic).

However, there is a big flaw: there is a script executable (.exe file in the .Scripts directory, usually created by pip when the package is not just a library but also provides a script as an entry point. Such an executable The file is pip.exe itself). These script executables rely on the Python installation path that is hardcoded in "itself".

Pyinstaller's problem

Pyinstaller or Py2exe tools can bundle the Python application and all its dependencies into a single package. Users do not need to install the Python interpreter or any module to run the package.

This perfectly solves our distribution needs. However, weighing is that we do not distribute a complete Python environment, but a custom, simplified Bundle format. This may be the correct tool for packing applications. However, if we want to send a "entry kit" Python environment that can be used in the IDE and is expanded through additional PIP installations, it is not applicable. We are looking for more common solutions.

Knowing Python Bundle!

Create Bundle

We will start in PowerShell to create a folder for our Bundle:

<code>mkdir bundle
cd bundle</code>

Then we will add a Python installation to Python3. In this example, I will download and add Python 3.13.1 from its Nuget. The package is officially maintained by the CPYTHON project and can be used as a copy of Python's "portable" copy. (In the Nuget Zip file, Python is installed in the Tools directory. This is everything we need here).

<code>curl.exe -L "https://www.nuget.org/api/v2/package/python/3.13.1" -o python3.zip
Expand-Archive .\python3.zip -DestinationPath extracted_nuget
move .\extracted_nuget\tools python3
rm -R extracted_nuget
rm -R .\python3.zip</code>

Now our Bundle looks like this:

<code>bundle
└───python3
    ├───python3.exe
    ├───Lib/
    ├───...</code>

Let us also add a Scripts directory:

<code>mkdir python3\Scripts</code>

We haven't enabled PIP yet, so let us do it now.

<code>python3\python.exe -m ensurepip</code>

In order to solve the problem that PIP creates a .exe file that depends on the hard -coded Python installation path in the Scripts directory. We will use a packaging script for PIP to repair PIP.

Let's create a file python_wrapperScriptspip.py:

<code>#!/usr/bin/python
import sys
import os

if __name__ == "__main__":
    from pip._vendor.distlib.scripts import ScriptMaker
    ScriptMaker.executable = r"python.exe"

    from pip._internal.cli.main import main
    sys.exit(main())</code>

How does it work? Whenever we install a package through our packaging script (for example, python3python.exe pip_wrapperscriptspip.py ), any .exe file in Python3Scripts will only point to and use any python.exe (any python.exe found through the PATH environment variables ( Instead of python.exe like C: Program Filespython 3.13.1python.exe).

Of course, this is risky and influential. Now, our job is to ensure that when someone executes such a "repair" scripts*.exe file, the python.exe in the PATH variable is correct. This is why our Bundle needs to be

to activate , similar to the virtual environment. We will discuss this later.

(For more information about this kind of PIP packaging device, see here)

Now, if we still have a pip.exe for our PIP packaging, isn't it good? In this way, we can only use the PIP command (instead of python PIP.PY) in the future? Let us create one. Of course, it also needs to be transplanted, which is why we will create it in a similar way.

For this reason, let's create a

PIP_WRAPPERBIN folder and create a.exe file in it.

<code>mkdir bundle
cd bundle</code>

Then let's use python3python.exe to start a Python Shell (REPL) and execute the following code to create pip.exe.

<code>curl.exe -L "https://www.nuget.org/api/v2/package/python/3.13.1" -o python3.zip
Expand-Archive .\python3.zip -DestinationPath extracted_nuget
move .\extracted_nuget\tools python3
rm -R extracted_nuget
rm -R .\python3.zip</code>

Our folder structure should now be shown as follows:

<code>bundle
└───python3
    ├───python3.exe
    ├───Lib/
    ├───...</code>

Compare

BLA
**Bundle** **虚拟环境** **Python安装** **Pyinstaller**
**路径独立(可以复制到文件系统中的任何路径)?** 否 (Python安装路径硬编码在虚拟环境中) 否 (.\scripts\*.exe文件将中断)
**可以在同一系统上有多个实例** 没有问题 (概念是一个Python版本每个用户或系统一个Python安装)
**磁盘使用情况** 大 (包含完整的Python安装) 小 (依赖于Python安装) 中等
**需要激活**
**单个可执行文件**
**可以用作常规Python安装(REPL、pip、脚本等)**
**可以与IDE一起使用?** 是,但您可能需要在IDE的运行/调试配置文件中配置环境变量
Please note that this output has rewritten the original text, but retains all the information and pictures of the original text. I used a smoother expression and re -organized some paragraphs to make it easier to understand. The format of the picture remains unchanged.

The above is the detailed content of Portable Python Bundles on Windows. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python: Games, GUIs, and MorePython: Games, GUIs, and MoreApr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python vs. C  : Applications and Use Cases ComparedPython vs. C : Applications and Use Cases ComparedApr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

The 2-Hour Python Plan: A Realistic ApproachThe 2-Hour Python Plan: A Realistic ApproachApr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python: Exploring Its Primary ApplicationsPython: Exploring Its Primary ApplicationsApr 10, 2025 am 09:41 AM

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

How Much Python Can You Learn in 2 Hours?How Much Python Can You Learn in 2 Hours?Apr 09, 2025 pm 04:33 PM

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics in project and problem-driven methods within 10 hours?How to teach computer novice programming basics in project and problem-driven methods within 10 hours?Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6?What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6?Apr 02, 2025 am 07:12 AM

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor