5

Is there a way to deploy a Python program that includes all dependencies on a Linux system?

I have used py2exe to "compile" a python script with all modules to a standalone .exe, but that's obviously only working on Windows. Is there an easy way to e.g. develop a flask server with Python and have all its scripts and modules bundled together so that it can be executed on Linux without having to install the dependencies with pip? (assuming python3 is installed on the Linux platform, but no specific Python modules).

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
kevinunger
  • 334
  • 5
  • 13

2 Answers2

6

Use PyInstaller in Linux based systems PyInstaller is a program used to convert Python scripts into standalone deployable applications.

Install PyInstaller from PyPI:

pip install pyinstaller

Go to your program’s directory and run:

pyinstaller yourprogram.py

This will generate the bundle in a subdirectory called dist

You can use -onefile argument in order to generate the bundle with only a single executable file.

Saikat
  • 14,222
  • 20
  • 104
  • 125
Melan Rashitha
  • 504
  • 2
  • 13
  • 2
    When on windows, pyinstaller is creating a .exe in the /dist directory. How can I tell it to target linux? – kevinunger Jan 30 '20 at 08:35
  • 1
    use --distpath flag to define your executable output locations – Melan Rashitha Jan 30 '20 at 08:41
  • 1
    pyinstaller --distpath=./dist/ --workpath=./build/win64 src/app.spec – Melan Rashitha Jan 30 '20 at 08:45
  • 2
    I tested it with a simple helloWorld.py. When try to run the executable on linux, I get this error: ´ File "site-packages/PyInstaller/loader/rthooks/pyi_rth__tkinter.py", line 30, in FileNotFoundError: Tcl data directory "/var/folders/3q/wfnfkkm976z8q9gjz_npccbh0000gp/T/_MEIafCy5q/tcl" not found. [20445] Failed to execute script pyi_rth__tkinter´ – kevinunger Jan 30 '20 at 11:09
  • Got it to run. I used a linux vm and could run it on linux and macOS. The problem is that my target is on arm architecture. So I probably need to compile this on this architecture. – kevinunger Jan 30 '20 at 15:33
  • @kevinunger Did you ever got it to compile on arm? – Twistx77 Jun 01 '23 at 07:14
0

You can install the dependencies in the same directory as the program as mentioned here and then package it any way you want. This way the program can always access the dependencies even if they are not installed in the system the program is being executed in.

BBloggsbott
  • 195
  • 2
  • 12