Home >Backend Development >Python Tutorial >Portable 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.
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.
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.
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 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.
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>
**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!