How to distribute python application with all dependencies - python

I want to distribute a python application with all its dependencies. Target machine doesn't have an outside connection so I can't pip install anything
and all packages must be included.
I'm using python 2.7 for my application and the target machine has a different python version. I would like to pack python 2.7 as part of my distribution.
Any ideas?

If you want to distribute on Windows you can use py2exe: http://www.py2exe.org/
or cross-platform http://www.pyinstaller.org/
That way you convert your whole application to a single executable file, which includes everything you need to run it.

Related

Is there a way to pack a python project with all dependencies but not as an executable, but with access to the code?

since I think the original post wasn't clear enough -
EDIT:
Is there a tool or an option to pack my python project and all of it's dependencies and sub dependencies - same as pyinstaller does, but pyinstaller generates an executable binary, and I'd like to have the ability to make changes in the code after it's distributed (e.g on a client's environment)
Original Post
I have this python project, with a lot of dependencies and sub-dependencies, that's currently distributed by building it using pipenv to create a virtual environment and getting the 3rd party libraries, and pyinstaller to generate an executable which is being used on a virtual machine with another OS (I'm packing an executable to the target's machine OS by building it on a docker with the same OS).
the thing is - I'm using some data and scripts from this virtual machine in my python project, so i can't run it locally, and whenever there's a bug, or an error, I have to make changes locally and rebuild (which takes some time) and only then move the executable to the vm in order to run it.
my goal is to have all of the code packed with the dependencies, but with the structure of my project, and not as executable, so I can make quick changes on the VM itself.
The VM may not have an external connection, so I can't just install the dependencies on the machine.
is there a tool that can help me do such a thing?
Note
Currently the python version on the VM is different than the python version the project uses. it's possible to install another version if necessary.

Portable Python/IPython

I am currently starting a business where I will be providing support to clients directly on their business offices. I need to be able to go to different computers and be able to run custom python scripts, my question is if there's a way to make my python environment portable?
Assuming that your users are running Windows, I see two options here.
If you have already defined which scripts you will be running, compile them into exe files using py2exe, that way you can just plug a USB and run them as needed. (the caveat is that some antivirus will automatically block the unsigned executables)
The other option is to use WinPython, that is a full python environment with a lot of packages already preinstalled that ives in it's own directory. In case you need to install a new package, just use the Powershell or CMD that comes with it and use the preinstalled "pip".
I found something interesting here Portable Python. I use that method to create portable Python 3.9 and 3.10 and everything works so have a look.

Create python script that don't depend on modules

I've made a python script that depends of numpy, cv2 and some other modules to run and need to run it on a Linux server where I'm not allowed to install anything.
Is there a way to join all that stuff into a single executable that runs without installing anything?
It sounds like you're looking for PyInstaller, which bundles all the modules your script depends on into a single program. It even includes Python itself. There are a few alternatives, some of which are listed on this page.
You could create a standalone executable using Nuitka. Assuming you have all required packages on your development machine you can run
nuitka --python-version=2.7 --standalone foobar
Just make sure you run it with the standalone flag and correct python version.

Public python program as one file with all modules included

I am new in Python, and I wonder if I can release my program in some kind of compiled build project with all modules and librarys included, so I can run it on diffrent systems? I don't want to install opencv on every pc.
You can specify a requirements.txt file which lists the dependencies used by your program. Python pip can read this file to bundle and package your application. See the docs here: https://pip.pypa.io/en/stable/user_guide/#requirements-files.
Also, I believe that OpenCV requires some native extensions installed which are not packaged with Python. Unfortunately, you'll need to install native extensions on each new machine you use.

Canonical way to install modules and app onto offline system

We have an app (a bunch of Twisted classes actually) which runs on a specific Python version and depends on quite a bit of modules. This app needs to be deployed onto a Windows Server machine which has no access to Internet.
Currently we are choosing between:
having to install Python prior to everything else, and a Python script which unpacks all modules and runs setup.py,
making an NSIS installer which installs Python, then all modules with .exe installers, then unpacks smaller modules into some other dir, then adds the dir to %PYTHONPATH%.
What is the good accepted way of dealing with such situation? Obviously we cannot use pip, easy_install.exe and other blessed tools, and our approaches are silly and inelegant.
As a third option you can consider deploing the application as an executable using PyInstaller (http://www.pyinstaller.org). You dont need to install anything on the client machine (not even python)
PyInstaller is a program that converts (packages) Python programs into stand-alone executables, under Windows, Linux, Mac OS X, Solaris and AIX. Its main advantages over similar tools are that PyInstaller works with any version of Python since 2.4, it builds smaller executables thanks to transparent compression, it is fully multi-platform, and use the OS support to load the dynamic libraries, thus ensuring full compatibility.
I have used it in a project to deploy standalone application in both Linux and Windows. Worked like a charm. My project also used Twisted.
Between your current two choices the setup.py approach is more pythonic. But beware that if any of your modules has some c implementation for faster performance that need to be compiled, you can't do that on you client's machine.
Try to use wheels. It designed to cover your case, i.e. you build wheel once, downloading all required packages. Then you just copy wheel archive to the target machine and install your application without downloading anything.

Categories

Resources