How to package/ distribute python applications - python

I've spent countless hours trying to understand this and unfortunately I haven't gotten to an answer yet. Or at least I don't think I have.
First up I should say that I am a Java Developer. I've only recently started working with Python and the build-process is a bit...odd for me.
In my mind I write an application, I compile it to run and I package it into a .jar for other people to use. Either as a library or for end-users to execute and have fun with it. (ignoring stuff like maven or gradle...)
I wrote a little CLT in python that consists of ~6 files and I wanted to distribute it. From what I could find I was supposed to write a setup.py and I found some guides on how to do that but ... to be honest I'm not even sure what that did. I could get my source code bundled into a tar.gz with some other meta data or it would create some weird files that I don't know what to do with.
Then I found PyInstaller and it worked great to package everything into a binary. However I've run into some problems trying to create a Debian package and it has made me re-assess and question the fact that there doesn't seem to be something in Python (without having to use an external tool) that lets me package/ bundle/ whatever my application into a single file to be run.
And that's something I can't get my head around. I mean...before there were tools like PyInstaller and P2Exe and what not, how did people distribute their applications? Am I expected to write a C application, somehow include the python code in there and compile that? Sorry if this seems like a stupid question but I'm really asking. I've googled around so much and spent so much time on it and haven't found a satisfactory answer so I hope someone here can help me with this! Thanks.

If you package your Python code for pip, you can include some executable scripts that start your program. I don't know how the situation was 5 years ago when this question got asked, but nowadays pip is pretty much integrated with Python, to the point that there's a standard library module to bootstrap pip in case it's missing:
https://docs.python.org/3/library/ensurepip.html
The situation is different if you want to package an application for some other package manager, like Anaconda or the package managers of various Linux distributions, or as a Windows installer. Obviously, you'll have to create a separate package for each package manager or installation technique you want to support.

Related

What is the best way to share a python application with someone who isn't a dev [duplicate]

This question already has answers here:
How can I run a Python project on another computer without installing anything on it?
(6 answers)
Closed 9 months ago.
Problem:
I need to send someone (who has little to no computer knowledge) a python program, but I don't even know if he has python installed let alone all the dependencies.
Question:
Assuming he doesn't have python, how should I go about sending the application so that It is as straightforward as possible for him?
What I've tried:
Initially, I thought of using venv and sending him the whole thing, but there has got to be a better solution, as my code only uses two of the built-in libraries.
In my research, I came across Docker, but I think he would need to have that installed. I also saw pipenv but it wouldn't work without python on his PC.
You have multiple ways to do so:
Create an executable file, which is quite a long process but can be useful is you project contains a lot of files and multiple dependencies
Send them the python code (I recommend it if your project fits in one .py file). The person will need to install python and possibly a few pip libraires, but it's not extremely complicated in my opinion, with clear instructions.
Finally, you can go on repl.it and create a repl, which is simply a way to execute code on your browser. I think this is the best option for both large and small projects, except if it contains a lot of odd dependencies, and I'm not sure if repl.it supports graphical interfaces either. Anyway, you should take a look, it might be perfectly fit your needs

how to control if libraries are installed on the system in python

is there a way to create a function that check on the system if the python modules that are needed by the main program are installed and eventually install them automatically?
I searched a lot for something like this but i didn't found anything useful.
Thanks and sorry for the bad english.
The tool you are likely looking for is pip, it's not run as a part of a script but rather is used to install a script.
https://pip.pypa.io/en/stable/reference/
In addition if you wish to develop a script to install your script you can find documentation
http://marthall.github.io/blog/how-to-package-a-python-app/

Difference between installing and importing modules

New to Python, so excuse my lack of specific technical jargon. Pretty simple question really, but I can't seem to grasp or understand the concept.
It seems that a lot of modules require using pip or easy_install and running setup.py to "install" into your python installation or your virtualenv. What is the difference between installing a module and simply taking it and importing the into another script? It seems that you access the modules the same way.
Thanks!
It's like the difference between:
Uploading a photo to the internet
Linking the photo URL inside an HTML page
Installing puts the code somewhere python expects those kinds of things to be, and the import statement says "go look there for something named X now, and make the data available to me for use".
For a single module, it usually doesn't make any difference. For complicated webs of modules, though, an installation program may do many things that wouldn't be immediately obvious. For example, it may also copy data files into locations the new modules can find them, put executables (binary libraries, or DLLs on Windws, for example) where the new modules can find them, do different things depending on which version of Python you have, and so on.
If deploying a web of modules were always easy, nobody would have written setup programs to begin with ;-)

How to package a python program for distribution on a network

I'm not sure if I'm even asking this question correctly. I just built my first real program and I want to make it available to people in my office. I'm not sure if I will have access to the shared server, but I was hoping I could simply package the program (I hope I'm using this term correctly) and upload it to a website for my coworkers to download.
I know how to zip a file, but something tells me it's a little more complicated than that :) In fact, some of the people in my office who need the program installed do not have python on their computers already, and I would rather avoid asking everyone to install python before downloading my .py files from my hosting server.
So, is there an easy way to package my program, along with python and the other dependencies, for simple distribution from a website? I tried searching for the answer but I can't find exactly what I'm looking for. Oh, and since this is the first time I have done this- are there any precautions I need to take when sharing these files so that everything runs smoothly?
PyInstaller or py2exe can package your Python program.
Both are actively maintained. PyInstaller is actively maintained. py2exe has not been updated for at least a year. I've used each with success.
Also there is cx_Freeze which I have not used.
Take a look at http://www.py2exe.org/

Deploying python executables- PyInstaller, cx-freeze, etc

I'm looking to find a way to bundle a python app into stand-alone executables so my windows and mac using friends can use it without installing ugly dependencies. Looking online I've found a few utilities to help do this, including py2exe for windows and py2app for mac, as well as PyInstaller, cx-freeze, and bbfreeze. What have ya'll used and what would you recommend?
I've been building a python app with PyQt and PyQwt for the past few weeks and have had the same problem. I found py2app completely impossible to use, I kept running into so many problems constantly that I gave up. A few days later found PyInstaller which is fantastic. It understands both PyQt and PyQwt out of the box - and does a very nice job in wrapping everything into an app bundle. Haven't tried building a Windows executable with it yet though.
I found a good article at arstechnica on how to use py2exe and py2app although it's a bit old (you can probably skip the python 2.5 stuff) http://arstechnica.com/open-source/guides/2009/03/how-to-deploying-pyqt-applications-on-windows-and-mac-os-x.ars/
I would highly recommend using PyInstaller. There are a couple of tricks though you need to do for OS X since the support is currently only preminary http://diotavelli.net/PyQtWiki/PyInstallerOnMacOSX
I use py2exe to create a windows executable. The documentation is somewhat messy, but once I pulled together a working setup.py to use as a template I haven't had any problems altering it to generate any given exe. I've usually been able to find helpful information though Google searches, like when I needed to bundle in pngs for the UI to use.

Categories

Resources