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.
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
<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
<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
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
**Bundle** | **虚拟环境** | **Python安装** | **Pyinstaller** | |
**路径独立(可以复制到文件系统中的任何路径)?** | 是 | 否 (Python安装路径硬编码在虚拟环境中) | 否 (.\scripts\*.exe文件将中断) | 是 |
**可以在同一系统上有多个实例** | 是 | 是 | 没有问题 (概念是一个Python版本每个用户或系统一个Python安装) | 是 |
**磁盘使用情况** | 大 (包含完整的Python安装) | 小 (依赖于Python安装) | 大 | 中等 |
**需要激活** | 是 | 是 | 否 | 否 |
**单个可执行文件** | 否 | 否 | 否 | 是 |
**可以用作常规Python安装(REPL、pip、脚本等)** | 是 | 是 | 是 | 否 |
**可以与IDE一起使用?** | 是,但您可能需要在IDE的运行/调试配置文件中配置环境变量 | 是 | 是 | 否 |
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!

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 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.

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 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.

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 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 when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Atom editor mac version download
The most popular open source editor