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.
Altering the Folder Structure
In order to package our application well, we need to make some additions to our folder structure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/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 |
Alter the folder structure to create necessary files:
1 2 3 4 5 6 7 |
touch ~/MyApplication/setup.py touch ~/MyApplication/README.py touch ~/MyApplication/MANIFEST.py touch ~/MyApplication/CHANGES.py mv ~/MyApplication/run.py ~/MyApplication/bin/run |
Create the setup.py
1 2 3 |
nano ~/MyApplication/setup.py |
Place the below self explanatory contents:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
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", ], ) |
Save and exit using CTRL+X and confirm with with Y.
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
.
1 2 3 |
nano ~/MyApplication/MANIFEST.in |
Place the below self explanatory contents:
1 2 3 4 |
recursive-include app/templates * recursive-include app/static * |
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:
1 2 3 4 |
cd ~/MyApplication python setup.py sdist |
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:
1 2 3 4 |
# root@hostname:~/MyApplication# ls dist # MyApplication-0.1.0.tar.gz |
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.