Executable Python program with all dependencies for Linux - python

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).

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.

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.

Related

How can I run python code with the packages I use without an IDE?

I used PyCharm to write a program that mutes me on discord when I say “mute myself”.
It works fine when I run it with PyCharm, but when I try to run it with idle or cmd, it won't find the modules I use.
It sounds like you may want to look into something like Poetry to manage the Virtualenv for your program. Poetry will help you bundle together the modules your program needs in a structured way.
After you've packaged your program using poetry, check out the "run" command.
If you don't like Poetry, you can find other alternatives over on the PyPA documentation.
PyCharm, by default, creates a virtual environment where it installs the modules (the venv folder).
I am assuming you are on windows?
To use that environment's packages you can activate it in your command line
venv\Scripts\activate.bat
and run your script like
python main.py
Another alternative would be to package your project into a exe using a tool like pyinstaller
# while in your virtual environment
pip install pyinstaller
pyinstaller --onefile main.py

.py to .exe file conversion issue

I'm trying to convert a simple python code (any sample code) into an executable file but on opening the application all I can see is a blank black screen.
I am using:
Python 3.7
PyInstaller 3.6
One file output
I am able to convert the file to .exe using auto-py-to-exe but I don't see any output when I try to run the application.
Pyinstaller is much easier than other things, you need to install with pip:
pip install pyinstaller
And then go to the path of your Python file and then:
pyinstaller -w -F my_file.py
In general there are some different ways so you can try the below and see if the issue is solved.
auto-py-to-exe is "A .py to .exe converter using a simple graphical interface built using Eel and PyInstaller in Python.". You can find a quick guide here
PyInstaller "freezes (packages) Python applications into stand-alone executables, under Windows, GNU/Linux, Mac OS X, FreeBSD, Solaris and AIX." You can find more usage info here (also pointed out by #nakE)
py2exe "is a Python Distutils extension which converts Python scripts into executable Windows programs, able to run without requiring a Python installation.".You can find more usage info here (also as pointed out by #Kompocik)
If it is not solved then I am pretty confident that there is an issue with the executed code per se and not the conversion.
Can you please share your code so we can have a look?
Try doing this in order:
py -3.7 -m pip uninstall pyinstaller
py -3.7 -m pip install pyinstaller
pyinstaller --onefile test.py

Run python script from another computer without installing packages/setting up environment?

I have a Jupyter notebook script that will be used to teach others how to use python.
Instead of asking each participant to install the required packages, I would like to provide a folder with the environment ready from the start.
How can I do this?
What is the easiest way to teach python without running into technical problems with packages/environments etc.?
If you just need to install Python dependencies, you can use #Aero Blue solution. However, the users would need probably to make a virtual environment, so they don't mess with other environments and versions, etc.
However, if they should need some Linux packages, this would not be enough. Therefore, I would suggest using Docker. You would need to provide them with a Dockerfile, that you should set to install any dependencies (whether is for Python or Linux), and they would just need to use docker build and docker run commands.
The easiest way I have found to package python files is to use pyinstaller which packages your python file into an executable file.
If it's a single file I usually run pyinstaller main.py --onefile
Another option is to have a requirements file
This reduces installing all packages to one command pip install -r requirements.txt
You would need to use a program such as py2exe, pyinstaller, or cx_freeze to package each the file, the modules, and a lightweight interpreter. The result will be an executable which does not require the user to have any modules or even python installed to access it; however, because of the built-in interpreter, it can get quite large (which is why Python is not commonly used to make executables).
Have you considered using Azure notebooks or another Jupyter hosting service ? Most of these have a special syntax you can use to perform pip installs. For Azure it is !pip install
https://notebooks.azure.com

How to add uninstall script to debian package

I have created a simple debian package for my python program using this post.
I am also using a postinst script to setup and populate mysql tables. The package gets installed with following command.
sudo apt install mypackage.deb
I now want to add an uninstall script so that if the package is removed, uninstall script gets called to cleanup the environment.
How can I incorporate uninstall script with the debian package?
You probably need to write a postrm script too the same way as you wrote the postinst script. See maintainer scrips flowcharts to understand how these scripts work.
A quote from the same article:
"It is possible to supply scripts as part of a package which the package management system will run for you when your package is installed, upgraded or removed.
These scripts are the control information files preinst, postinst, prerm and postrm. They must be proper executable files; if they are scripts (which is recommended), they must start with the usual #! convention. They should be readable and executable by anyone, and must not be world-writable."

Why does easy_install work with some Windows binaries?

Background
Windows does not include a compiler by default, and installing a compiler (and perhaps configuring Python to use it) is a complicated enough task that many developers avoid doing so. To this end, many packages that have binary dependencies are available as a precompiled Windows executable that contains binary files. As an example, there is psycopg.
The executable file is an installer. When executed, it provides a graphical interface that locates an installed version of Python via the registry, and it installs the Python library and the included binary dependencies in the global Python installation.
However, this is not always desirable. Particularly when using virtualenv, developers don't want to install the library globally. They want the library installed in the virtual environment. Since this environment is not represented in the registry, the graphical installer cannot locate it. Luckily, a command similar to the following can be used to install the library to the virtual environment:
C:\> C:\virtualenv\Scripts\activate.bat
(virtualenv) C:\> easy_install psycopg2-2.5.win32-py2.7-pg9.2.4-release.exe
Note that this works regardless of whether easy_install comes from setuptools or distribute.
The actual question
Why does this command work? What is it about the exe that allows easy_install to process it?
I have noticed that the exe seems to be some kind of zip file. 7-Zip is able to open it for browsing, and these exe files that easy_install can process seem to have a common file structure. They have a top directory named PLATLIB which contains an egg-info file or folder and another (possibly more than 1?) folder. Are these exes just Python eggs with some kind of executable wrapped around them? How might I produce one myself? (Or to word it differently, is there some standard way of producing exes like this?)
Edit
Bonus question: Why doesn't pip work with these files?

Categories

Resources