How to make python portable?
Asked Answered
S

3

10

I want to make a portable app that would have some code and python executable that would run on any Windows even if python is not installed.

I would like it to be python 3.6 and so it has only pip and setup tools installed.

EDIT: concerning duplicate

not quite. I don't want to compile the code. I wanted to give them .py files but realize that Windows won't have python installed on default. I want something that can be carry on a flash drive but will run my code from source not binary.

Sowder answered 11/6, 2017 at 19:57 Comment(2)
Possible duplicate of How to make a Python script standalone executable to run without ANY dependency?Milone
Python eggs might be what you are looking for https://mcmap.net/q/63547/-what-is-a-python-eggOsteophyte
C
5

Please correct me, if I understood it wrong. I think there are at least two ways to do it. suppose you have one portable_run.py script you want to run everywhere on a flashdisk.

  1. Make a exe file with pyinstaller for example. you can get a exe file like portable_run.exe. On target windows system what you need to do is to run the exe direcltly protable_run.exe

  2. Use a portable python distribution like winpython or python-xy. you just need to copy this portable distribution on the flash disk together with your portable_run.py. To run it on target system flashdisk/path-of-winpython/python portable_run.py

Hopefully it could give you some idea.

Corroboration answered 11/6, 2017 at 20:41 Comment(5)
unfortunately no, as i said i don't want to compile my code and all those portable pythons does not have pip and have lots of other stuff i don't need. I was thinking of create my own app like this but i dont know where to start.Sowder
@Kazz, did you find the solution?Kareenkarel
Unfortunately no, and even more issue came up with compiling. Turn out that even compiling it to C won't work since on fresh Win7 Installation (fully updated) there is no C runtime env by default. I personly gave up on developing python apps on windows, you might wanna look into using docker on windows 10.Sowder
3 and a half years later and there is still no good option out there.Kopp
Well you can use embeddable Python (for example for Python3.8 go to python.org/ftp/python/3.8.0 and search for embed). Unzipped it to a folder. cd to the folder. Install pip (curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && python get-pip.py). Install the packages you need (python -m pip install x y z). You have a portable python. I managed to package a gradio app (one of those on huggingface.co/spaces, but my friends want to run the app locally and they dont know too much about python) this way and send the whole thing to my friends.Autosuggestion
F
1

I also encountered the same problem and managed to create a portable python with Python's official Windows embeddable package.

I wrote the steps into a ps1 script so I can easily repeat the process without going through the pain.

The steps:

  1. Download the portablepy.ps1 from the repo : https://github.com/Dreamsavior/portable-python-maker
  2. Create a blank folder, put the portablepy.ps1 to that folder.
  3. Execute the portablepy.ps1

The script will create a portable python 3.9.10 with pip in the current folder by default.

To install custom version of Python run the script with -source and -destination parameter

.\portablepy.ps1 -source "https://www.python.org/ftp/python/3.9.10/python-3.9.10-embed-amd64.zip" -destination "C:\SomeDir\PortablePython\"

Where the -source is the url of the Python's Windows embeddable package from this page: https://www.python.org/downloads/windows/

And -destination is the path of your folder (ended with backslash).

Frederick answered 17/2, 2022 at 14:12 Comment(1)
You still need Universal C Runtime present in the OS right? That's what blocked me 5y ago. What Windows versions did you test this on?Sowder
T
0

Embeddable python may be portable solution in the future, but currently it doesn't support easy install of pip, tkinter, and possibly other dependencies.

For the standard (non-embeddable) environments, sometimes just combining them with binaries and replacing absolute paths with relatives is enough.
But this will create very large portable archive >100MB, while online install initial distribution overhead <1MB.

Both options can be done via powershell without packing into custom binaries, here is example which couples original pyton binaries and environment (with tkinter and pandas dependency) into portable folder.

Topographer answered 5/3, 2023 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.