How do you distribute python apps


How do you distribute python apps


Altering the Folder Structure

In order to package our application well, we need to make some additions to our folder structure.
<code>/MyApplication
    |-- run.py
    |__ /app
         |-- __init__.py
         |-- /module_one
             |-- __init__.py
             |-- controllers.py
             |-- models.py                
         |__ /templates
             |-- module_one
                 |-- hello.html
         |__ /static
         |__ ..
         |__ .
    |-- setup.py    # Distribution setup file
    |-- README.txt  # Read-me file
    |-- MANIFEST.in # Distribution manifest file
    |-- CHANGES.txt # Changes log
</code>
Alter the folder structure to create necessary files:
<code>touch ~/MyApplication/setup.py
touch ~/MyApplication/README.py
touch ~/MyApplication/MANIFEST.py
touch ~/MyApplication/CHANGES.py
mv    ~/MyApplication/run.py ~/MyApplication/bin/run
</code>

Create the setup.py

<code>nano ~/MyApplication/setup.py
</code>
Place the below self explanatory contents:
<code>from distutils.core import setup

setup(
    # Application name:
    name="MyApplication",

    # Version number (initial):
    version="0.1.0",

    # Application author details:
    author="name surname",
    author_email="name@addr.ess",

    # Packages
    packages=["app"],

    # Include additional files into the package
    include_package_data=True,

    # Details
    url="http://pypi.python.org/pypi/MyApplication_v010/",

    #
    # license="LICENSE.txt",
    description="Useful towel-related stuff.",

    # long_description=open("README.txt").read(),

    # Dependent packages (distributions)
    install_requires=[
        "flask",
    ],
)
</code>
Save and exit using CTRL+X and confirm with with Y.Having created an exemplary application structure of a web site that uses flask, we can continue with taking the first step into preparing the distribution.

Create the MANIFEST.in

If you need to ship extra directories (e.g. static or templates), you need to explicitly state them in the manifest to be packaged. We will do this inside the MANIFEST.in.
<code>nano ~/MyApplication/MANIFEST.in
</code>
Place the below self explanatory contents:
<code>recursive-include app/templates *
recursive-include app/static *
</code>
Save and exit using CTRL+X and confirm with with Y.
And that’s it! Your Python distribution package is ready to be installed and shipped.

Additional Files

Please remember that in order to have a complete distribution, your file/directory must contain (and linked):
  • README.txt
  • MANIFEST.in
  • LICENSE.txt

Working With the Distribution Ready Application

As we have finalized creation of our application followed by making necessary amendments to the file structure to prepare it for a flawless distribution build, we can begin with going through the packaging operations.

How to Create The Distribution File

In order to generate a distribution file copy, run the following:
<code>cd     ~/MyApplication
python setup.py sdist
</code>
This command will go through your setup, print out the operations being performed and generate a tar archive inside the newly created dist directory, similar to:
<code># root@hostname:~/MyApplication# ls dist
# MyApplication-0.1.0.tar.gz
</code>
Note: Since we did not populate all the sub-folders (i.e. static) and worked with additional files (e.g. README.txt), you might see some warnings during the creation process.

Take your time to comment on this article.

Comments