[Tutor] release a script with non-standard dependencies

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Dec 20 01:59:19 CET 2012


On 19 December 2012 23:45, Steven D'Aprano <steve at pearwood.info> wrote:
> On 20/12/12 04:37, rail shafigulin wrote:
>
>> I'm attempting to write a script with command-line arguments. Based on
>> some
>> reading I have done online and some advice from this mailing list I'm
>> going
>> to use docopt and schema modules. The problem I'm facing is that I'd like
>> to be able to give this script to anyone who needs it by just using one
>> file, i.e. the script itself. Does anybody know if there is a way to
>> achieve this without pasting code from docopt and schema into my script
>> file.

I generally agree with what Steven has said but would like to add some
more information on some of the points.

> Also, copying and pasting code is one of the worst habits you can get into.
> Don't do it.
>
> The solution to this depends on how much effort you want to put into this.
>
> 1) The most basic option: dependencies are not your problem. It is up to the
> recipient of your software to install the prerequisite modules, not you.

For a project consisting of a single file that is not going to be very
widely used, this is a common method. Note that, if you are generally
using and installing Python software packages it is a good idea to
have pip (or something similar) installed. If you do have pip
installed the two packages you referred to can be installed by typing

$ pip install docopt schema

or if you want to install them for one user only you can modify the above to

$ pip install --user docopt schema

which will install the packages somewhere hidden in your user
directory (Python knows where to find them).

> 2) Give them a download link where they can get the dependencies.
>
>
> 3) Actually install those dependencies for them: when your program runs,
> it tries to import the modules it needs, and if it cannot find them, it
> runs an installer to download the modules from the Internet and install
> them.
> (This is a lot of work and probably not a good idea.)

Please don't do this unless as part of an installer script that

1) is clearly an installer script
2) explicitly explains what it wants to do or is doing to the user's system
3) gives the option to cancel with no changes to the user's system

Also generally don't do this. This kind of thing is typically done on
systems that don't have software repositories (like Windows). Python
programs never fall into this category since PyPI is usable on all
platforms.

> 4) Bundle the modules together in one file. If you zip up the modules in a
> zip file as if it were a package, *without* the package directory, then
> change
> the file extension to .py, you can run it as if it were a single Python
> script
> (at least under Linux, I haven't tried Windows or Mac).
>
> But if you do this, you have to obey the licence conditions (if any) on the
> modules you use.

Both of these projects seem to indicate that this usage pattern is
expected. You'll need to check the license properly and perhaps
contact the authors to be clear about what conditions to follow when
doing that, though.
"you can just drop schema.py file into your project—it is self-contained."
"you can just drop docopt.py file into your project--it is self-contained."
>From here:
https://github.com/halst/schema
https://github.com/docopt/docopt

> 5) On Windows, you can bundle your script and dependencies using py2exe.
> I know nothing about it, but the search engine of your choice will have
> lots of information:
>
> https://duckduckgo.com/?q=py2exe
>
> I believe there is also a py2app for Mac users.
>
> Again, obey the licence conditions.

Things may have improved since the last time I attempted this, but I
remember this being relatively painful and generating massive .exe
files. Also pyinstaller worked better for me then py2exe but that was
a while ago. I would say that this is overkill for a project
consisting of three .py files. This option is really for the extreme,
but not uncommon, case where your (Windows) user cannot be expected to
install any dependencies, not even Python itself (which would probably
be installed already on a non-Windows OS).

> 6) There are a few more options here:
>
> http://www.effbot.org/zone/python-compile.htm
>
>
> 7) Use a full Python packaging and distribution utility, like Easy Install
> or Pip:
>
> http://packages.python.org/distribute/easy_install.html
>
> http://pypi.python.org/pypi/pip
>
>
> There are wars being fought over which distribution utilities; I don't have
> an opinion on which is best.

I wouldn't say that there are wars (maybe I haven't looked in the
right places), although I would personally recommend pip for
installing such simple packages as these. There are a number of
distribution utilities and they have some differences but they all
have one thing in common which is that they mainly just download and
install software that has been uploaded to PyPI. If your package is on
PyPI then anyone can install it with their own chosen utility:
http://pypi.python.org/pypi

If your package is on PyPI then you can include a file that describes
its dependencies and those will be automatically installed when
someone does

 $ pip install railsscript

I would rather install your script that way. This is the most common
way that python projects are released. If I heard of a Python project
and wanted to find it, PyPI would be the first place to look.
Similarly if I was trying to comprehensively discover a Python project
fulfilling a particular use I would try some search terms in PyPI.


Oscar


More information about the Tutor mailing list