Table of Contents
Question
Problems with virtual environment
Problems with Python installation
Pyinstaller's problem
Knowing Python Bundle!
Create Bundle
Home Backend Development Python Tutorial Portable Python Bundles on Windows

Portable Python Bundles on Windows

Jan 26, 2025 pm 08:17 PM

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

Now our Bundle looks like this:

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

Let us also add a Scripts directory:

<code>mkdir python3\Scripts</code>
Copy after login

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

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

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>
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

Our folder structure should now be shown as follows:

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

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 of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
Python vs. C  : Applications and Use Cases Compared Python vs. C : Applications and Use Cases Compared Apr 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 Approach The 2-Hour Python Plan: A Realistic Approach Apr 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: Games, GUIs, and More Python: Games, GUIs, and More Apr 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.

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.

Python vs. C  : Learning Curves and Ease of Use Python vs. C : Learning Curves and Ease of Use Apr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and Time: Making the Most of Your Study Time Python and Time: Making the Most of Your Study Time Apr 14, 2025 am 12:02 AM

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python: Exploring Its Primary Applications Python: Exploring Its Primary Applications Apr 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.

Python: Automation, Scripting, and Task Management Python: Automation, Scripting, and Task Management Apr 16, 2025 am 12:14 AM

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

See all articles