So I have a Python script:
myscript.py
I am executing it like this:
python D:\myscript.py
However, I must have Python installed and included in the PATH
environment variable for that to work.
Is it somehow possible to "bundle" Python executable with a Python script so other people will be able to run it on their PCs without Python?
It is ok if it will work only in Windows.
EDIT:
After trying the compile.py
I get this error:
Traceback (most recent call last): File "D:\stuff\compile.py", line 4, in <module> import py2exe ImportError: No module named py2exe
py2exe is a Python extension which converts Python scripts (. py) into Microsoft Windows executables (.exe). These executables can run on a system without Python installed. It is the most common tool for doing so.
The only realistic way to run a script on Windows without installing Python, is to use py2exe to package it into an executable. Py2exe in turn examines your script, and embeds the proper modules and a python interpreter to run it.
Yes, you can run a Python script without actually installing Python on Windows with Docker [ https://www.docker.com/ ]. Let's say you have a local script hello.py: print("hello world")
Here is one way to do it (for Windows, using py2exe
).
First, install the py2exe
on your Windows box.
Then create a python script named compile.py
, like this:
import sys from distutils.core import setup import py2exe entry_point = sys.argv[1] sys.argv.pop() sys.argv.append('py2exe') sys.argv.append('-q') opts = { 'py2exe': { 'compressed': 1, 'optimize': 2, 'bundle_files': 1 } } setup(console=[entry_point], options=opts, zipfile=None)
To compile your Python script into a Windows executable, run this script with your program as its argument:
$ python compile.py myscript.py
It will spit out a binary executable (EXE) with a Python interpreter compiled inside. You can then just distribute this executable file.
PyInstaller has worked well for me, generating reasonably small packages due to its use of upx. Its dependency detection was better than py2exe at the time as well. It seems not to have a lot of recent development and probably doesn't work with 3.x, however.
The source in the repository is a better starting point than the 1.4 package.
Also see the wiki page about working with Python 2.6+.
From the features list:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With