I first looked this up because I wanted to give someone a GUI tool that used PyTorch. They should be able to run it without installing Python, configuring an environment, or downloading dependencies. On May 23, 2025 I read Kivy’s packaging instructions, followed by a question whose title was almost exactly mine: “Create a single executable from a Python project”.1 Three days later my question had become more specific: what files must the program collect if it also needs PyTorch?
If the recipient already has Python, can I send just one file?
When bundling a Node.js backend with ncc, you can ship just the build artifact and run it using Node on the server. Python’s standard library has zipapp for a similar kind of delivery. Put the entry point __main__.py and the labels.py it imports inside tool/. The following commands package and run it. The recipient needs only the second command and a compatible Python.2
python3 -m zipapp tool -o tool.pyz
python3 tool.pyz
Open tool.pyz and you will see that both modules retain their filenames. Python imports directly from this ZIP archive and executes the entry point. zipapp collects files from a directory, so pure-Python third-party dependencies must be placed there first. To have the build tool handle that too, shiv invokes pip to collect dependencies, then extracts them into a local cache on first launch. This matters especially for native extensions, since Python cannot load .so or .pyd files directly from a ZIP.23
If the requirement specifically calls for one .py script, Stickytape can combine an entry point and its imported pure-Python modules into a single script. It finds modules by analyzing import, so dynamic imports need to be added explicitly and data files such as templates and images must be delivered separately. For my GUI tool, all of these methods shared a prerequisite: a Python installation capable of running it on the recipient’s machine.4
What runs the app on a machine without Python?
On the build machine, PyInstaller analyzes imports from the entry script, collects the Python interpreter, required modules, and recognizable dynamic libraries, and supplies a bootloader. When the user opens the executable, the bootloader prepares the runtime environment and starts the bundled interpreter. This depends on Python and third-party packages in the build environment, so first get the original program running with a known set of dependencies. Windows, macOS, Linux, and different CPU architectures each need matching artifacts.5
| Build mode | What to send | What happens at startup |
|---|---|---|
--onedir | The entire output directory, which can be zipped for transfer | The bootloader uses the support files directly in the directory. Missing files are easier to inspect. |
--onefile | One executable | The bootloader extracts required support files into a temporary directory, then runs the app. It cleans up after normal exit. Startup costs extra time when there are many dependencies. |
The official documentation advises, “Before you attempt to bundle to one file, make sure your app works correctly when bundled to one folder.”5 Directory mode has a practical advantage: if a resource is missing, you can inspect the output folder directly without guessing where temporary files went.
A minimal example with a resource file
Suppose the program needs to read a field-name configuration. Create a directory with two files. Name the first fields.json:
{"label": "资产编号"}
Name the second lookup.py. Locate the resource relative to the script so users can start it from a different working directory and still get the same configuration.6
import json
from pathlib import Path
fields_file = Path(__file__).with_name("fields.json")
fields = json.loads(fields_file.read_text(encoding="utf-8"))
print(fields["label"])
In a build environment with PyInstaller, run the source first, then generate a directory build and a single-file build. The different names preserve both outputs. The commands use PyInstaller 6 argument syntax.7
python -m PyInstaller --version
python lookup.py
python -m PyInstaller --onedir --name lookup-dir --add-data "fields.json:." lookup.py
python -m PyInstaller --onefile --name lookup-one --add-data "fields.json:." lookup.py
If the build environment lacks the tool, run python -m pip install pyinstaller there. The left side of --add-data is the source file at build time. The . on the right places it beside the entry point in the package. On Linux, run ./dist/lookup-dir/lookup-dir and ./dist/lookup-one in turn. The Windows entries have .exe extensions. Both should print “资产编号” (asset number). Finally, copy just the single-file build into an empty directory, launch it from another working directory, and try it on a matching machine without the project environment.
The JSON here is a read-only default shipped with the release. If users must edit configuration or write to a SQLite database, choose a stable, writable location separately. A single-file app cleans up the temporary directory after normal exit and starts from the bundled initial content again on the next launch.56
With PyTorch, how can I tell which kind of file is missing?
Opening the program and completing the model’s first inference are separate tests. A Python package can select submodules by string name, or load a native dynamic library or open model weights only on first inference. Directory mode is especially convenient when diagnosing errors.87
| Symptom | Inspect first | Possible remedy |
|---|---|---|
Source works, but a bundled feature raises ModuleNotFoundError | Does the feature dynamically import a plugin or backend? | Confirm the module, then add --hidden-import or declare it in the .spec file’s hiddenimports. |
| Missing model, vocabulary, or template | The path actually opened and the artifact directory | Collect small read-only resources using datas. Large, updatable weights can be shipped alongside the directory and loaded from an agreed location. |
Python module is present, but loading a DLL or .so fails | The shared library, its dependencies, target system, and architecture | Run a minimal model in the target environment, identify the missing library, then check PyInstaller’s collection hooks or the .spec file’s binaries. |
I read Ali Asaria’s account of distributing Transformer Lab. Users had to be able to install plugins, which brought new Python dependencies of their own. The team ultimately gave the application a dedicated Conda environment. This helped me express delivery requirements more precisely: one-click launch and the ability for plugins to install libraries can be addressed together by an installer. A single file is just one packaging format.9
What did I actually add in a later single-file configuration?
When packaging DesktopFriend for macOS in 2026 I encountered a similar resource collection problem. The build configuration for its Python backend, desktop-friend-core, included SQLite migration SQL in datas, preserving the directory hierarchy the program expected. It also listed Uvicorn logging, event loop, and protocol modules under hiddenimports. Finally, EXE(...) took the scripts, binary dependencies, and data files and produced one executable backend. Adding table creation SQL and adding a dynamically imported module require changes to different lists. Copying Python source alone misses the SQL.10
References
Footnotes
-
ShadowFlame. (2012, August 21). Create a single executable from a Python project [Question, original title]. Stack Overflow. Original question ↩
-
Python Software Foundation. (n.d.). zipapp — Manage executable Python zip archives (Python 3.12). Python documentation. Creating Standalone Applications with zipapp and Caveats ↩ ↩2
-
Shiv contributors. (n.d.). Shiv. Shiv documentation. Building and Bootstrapping ↩
-
Williamson, M. (n.d.). Stickytape: Convert Python packages into a single script. GitHub. Usage and limitations ↩
-
PyInstaller contributors. (n.d.). What PyInstaller does and how it does it (6.12.0). PyInstaller documentation. Bundling to One File, final paragraph and How the One-File Program Works ↩ ↩2 ↩3
-
PyInstaller contributors. (n.d.). Run-time information (6.12.0). PyInstaller documentation. Using file ↩ ↩2
-
PyInstaller contributors. (n.d.). Using spec files (6.12.0). PyInstaller documentation. Adding Data Files and Adding Binary Files ↩ ↩2
-
PyInstaller contributors. (n.d.). When things go wrong (6.12.0). PyInstaller documentation. Listing Hidden Imports ↩
-
Asaria, A. (2024, August 23). Packaging Python and PyTorch for a machine learning application. LinkedIn. Where We Finally Landed ↩
-
DesktopFriend contributors. (2026, July 15). Build (macOS): add signed release pipeline [Build configuration, commit 19bfa4b]. GitHub. desktop-friend-core.spec ↩