Command line application package for distribution?

I am currently writing a command-line application in Python that should be accessible to end users in such a way that it is very easy to download and run. For those on Windows that can have Python (2.7) installed, I intend to use PyInstaller to create a standalone Windows executable. Then users can simply download "myapp.exe" and run myapp.exe [ARGUMENTS] .

I would also like to provide a (lesser) download for users (on different platforms) who already have Python installed. One option is to put all my code in a single .py file "myapp.py" (starting with #! /usr/bin/env python ) and make it accessible. This can be downloaded and then run using myapp.py [ARGUMENTS] or python myapp.py [ARGUMENTS] . However, limiting my application to one .py file has several drawbacks, including limiting my ability to organize code and the difficulty of using third-party dependencies.

Instead, I would like to distribute the contents of several files of my own code, as well as some (pure Python) dependencies. Are there any tools that can pack it all into a single file that can be easily downloaded and run using an existing Python installation?

Edit: Please note: I need these applications to be easily accessible to end users. They probably did not have pip installed or anything else that is outside of the Python core. Using PyInstaller, I can generate a file that these users can download from the Internet and run with a single command (or, if there is no argument, just double-click). Is there a way to achieve this ease of use without using PyInstaller (i.e., without over-combining Python runtime)?

+7
python software-distribution packaging
source share
2 answers

I do not like the idea of ​​a single file, because it becomes a load on the service. I would consider an approach similar to the one below.

I became a big fan of Python virtual environments because it allows you to synchronize your application dependencies from OS installation. Imagine a scenario in which the application you are about to distribute now uses the Python requests v1.0 package. After a while, you will create another application that you want to distribute using requests v2.3 . You may encounter version conflicts on a system where you want to install both applications side by side. Virtual environments solve this problem, as each application will have its own package location.

Creating a virtual environment is very simple. Once you have installed virtualenv , it is just a matter of startup, for example virtualenv /opt/application/env . You now have a python sandbox for your application. In addition, virtual environments are very easy to clean up, just delete the env directory and you're done.

To install the application in the environment you need the setup.py . Say your application uses requests v2.3.0 , your own code is in a package named acme , and your script is called phone_home . The structure of your catalog is as follows:

 acme/ __init__.py models.py actions.py scripts/ phone_home setup.py 

setup.py will look something like this:

 from distutils.core import setup install_requires = [ 'requests==2.3.0', ] setup(name='phone_home', version='0.0.1', description='Sample application to phone home', author='John Doe', author_email=' john@doe.com ', packages=['acme'], scripts=['scripts/phone_home'], url='http://acme.com/phone_home', install_requires=install_requires, ) 

Now you can make a tarball from your project and place it however you want (your own web server, S3, etc.):

 tar cvzf phone_home-0.0.1.tar.gz . 

Finally, you can use pip to install your package into the created virtual environment:

 /opt/application/env/bin/pip install http://acme.com/phone_home-0.0.1.tar.gz 

Then you can run phone_home with:

 /opt/application/env/bin/phone_home 

Or create a symlink in / usr / local / bin to just call the script using phone_home :

 ln -s /opt/application/env/bin/phone_home /usr/local/bin/phone_home 

All the steps described above can be placed in a shell script that will make the process install one command.

And with minor changes, this approach works very well for development environments; for example, using pip to install / link to your development directory: pip install -e . where . refers to the current directory, and you should be in the project directory next to setup.py .

Hope this helps!

+7
source share

You can use pip as suggested in the comments. You need to create MANIFEST.in and setup.py in your project to make it accessible. You can also add modules as prerequisites. In this question you can find additional information (not related to Django):

How do I package a python application to make it protocol accessible?

This will make your module available in Python. Then you can run the file that runs your module using python path/run.py , ./path/run.py (with permission +x ) or python -c "some code here" (for example, for an alias).

You can even install users from the public git repository, for example,

 pip install git+https://bitbucket.org/yourname/projectname.git 

... in which case they also need git.

+5
source share

All Articles