hi there serdar yagalop here for infoworld in this episode of smartpython we're going to look at the
zip app standard for allowing python programs to be redistributed as a standalone package
one of python's biggest shortcomings is that it tends to be hard to take a python program
you've written bundle it up and give it to someone else to run in almost all cases the other person
must have a copy of the python interpreter to do this typically the other person takes an
archive of your project for instance by cloning a git repository sets up a virtual environment
installs the requirements needed to run it in the virtual environment and then goes from there
now that's not the hardest thing in the world for somebody with a decent amount of python experience
but it can be tedious what many people don't know is there's a standard within python for bundling a
python application directory into an archive that can be executed with one click it's called zip app
and it can simplify application distribution for a few key scenarios
the zip app standard is pretty simple it's just a zipped archive of a directory of python files
packaged up in such a way that python knows what module and function to use
as its entry point here is an exceedingly simple example i have here a module named
myapp a directory with a main.pi file an init.pi file and an auxiliary file from which we import
a function to run now i can run this module from the command line by just typing pi my app
now if i want to convert this module into a zip app all i have to do is type pi dash m zip app
my app i don't have to install anything because zip app is part of the standard library
it will then create a dot pi z file this is a zipped archive of the directory
and it's registered with the python runtime so to run it
all i have to do is either double click on it or type pi myapp.piz and it will run
it seems simple enough right well there are several key limitations to the way zip apps work
for one by default there is no convenient way to include third party modules with a zip app
that's a big drawback because so much of the functionality of any python program is in third
party modules here's an example of this problem in action i have here a slightly modified version
of the same program its chief difference is that it imports and uses numpy which is a large and
complex library now i can make a zip app out of this program by typing pi dash m zip app myapp
as before but i can't run it if i try to run it i get an error that tells me numpy hasn't been found
so if i give this to someone else they're most likely just going to get the same error
now one workaround for this is to use pip to install numpy into the same folder that we have
our app in and this isn't actually that hard because pip has an option that lets us control
the target folder we install things into we can do it for instance by way of typing this command
and once it finishes installing we can try to run the zip app as before and now as you can see it
does work but now we have a new problem we have the same old problem all over again in a new skin
how do we bundle all this to give it to somebody else well in theory we could just
zip up this whole directory minus our original program folder since we don't need it anymore
and just tell whoever's at the other end that they can unpack all of this into a folder and run the
pi z file with their copy of python well that's not necessarily the greatest solution now there's
a number of folks who have provided libraries of their own making that extend on zip apps behavior
to allow things like this to be done one such library is shiv created by the folks at linkedin
shiv has the same basic approach as zip app it's built on much of the same code but it has a
couple of key differences first of all it lets you bundle in third-party modules which are extracted
automatically the first time you run the program so you don't actually have to install them
now because shiv is that much more powerful it's also that much more complicated
it takes all the things you want to put into your bundle and it uses pip
and python's setup tools mechanisms to pack them together
so in order to make this work with my application i have to turn it into a pip installable package
so in this example here which is just basically a copy of the of the same thing i was working on
before i have here a setup.pi file which allows me to take the application that i was building
and turned it into a pip installable package so once i have that set up i then run shiv
now i have to pass a fair number of options to chev to get the results i need
i have to pass it the interpreter to use the way to invoke my application the file that i want to
write out as a zip app some additional details about how to unpack and pack that file and a
list of packages to install into the bundle one of them as you can see is numpy and the last one here
this trailing dot that means that the setup.pi file in our current folder is going to be used
so rather than my fingers we're trying to type all of this out over and over again i'm just going to
make a batch file to handle it as you can see so when we run shif it processes each package
it has to add to the bundle and at the end it generates a high z file just as zip app does
and when we run this zip app for the first time it unpacks all the needed dependencies into a
directory that it can access now normally this directory is in the user profile on the target
system but for the sake of demonstration i have forced shift unpack the dependencies into the same
directory where i have the zip app you can do this and because this way you can clean them up more
easily when you're finished with them instead of having them littering in your user profile
all we would need to do to distribute this project to somebody is send them the pi z file
everything would unpack itself but we still have several limitations to this approach
we still need to have the other person running a copy of the python runtime
and it has to be the same version of python he used to package everything up with
also i can only package this program for people running the same edition of python
on the same operating system in this case 64-bit windows if i wanted to package this for linux
i'd have to spin up a linux machine and do it there on a mac i would have to do the same thing
another thing that i haven't touched on again because of the complexity that it creates
is what i would need to do if i had to pack up data files along with my application instead of
just third-party libraries i could in theory do this by placing them into a sub-folder
and access them in my program by way of searching from them for the current working directory
and then pack them up in a big zip file along with my zip app so they can be extracted side
by side with it but you can see that that's a fair amount of work so to sum up distributing
a python program is one of the weaknesses in the python ecosystem the zip app standard in python
provides a way to distribute some small programs that don't have dependencies and the shiv library
expands on that to pack up dependencies and requirements along with the app's code
so if your needs aren't that demanding one of these solutions may help you get closer
to what you want that's it for this video if you liked it please leave a comment below
don't forget to subscribe to the tech talk channel on youtube and for more smart python
and more dev with serdar be sure to follow me on facebook youtube and infoworld.com
you