[Tutor] release a script with non-standard dependencies
Steven D'Aprano
steve at pearwood.info
Thu Dec 20 00:45:53 CET 2012
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.
You cannot just copy and paste code from one module to another, unless the
licence permits it, and you obey whatever obligations are laid on you by doing
so. If you don't, then you are in breach of copyright and, worse, potentially
acting in an unethical manner. Just because a programmer gives you access to
source code doesn't mean you can appropriate it as your own without respecting
his licence conditions.
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.
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.)
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.
For example, I create a Python package, consisting of a directory, two special
files, and whatever modules are needed:
test/
+- __init__.py
+- __main__.py
+- spam.py
+- ham.py
The file "__init__.py" is special, it is needed by Python to treat this as a
package. It can be empty.
The file "__main__.py" is special. It is used as the script when you give the
command "python test". Whatever code is inside the __main__.py file will run.
The two modules "spam.py" and "ham.py" are just examples, you can use any
modules that you like or need.
Inside __main__.py, type this:
import ham, spam # or whatever modules you need
print "Success!" # or do something useful
Now test that this works correctly, as a Python package. From the command
line:
[steve at ando python]$ ls test
ham.py __init__.py __main__.py spam.py
[steve at ando python]$ python test
Success!
Now build a zip file, change the extension, and run it:
[steve at ando python]$ zip -j myscript test/*
adding: ham.py (stored 0%)
adding: ham.pyc (deflated 33%)
adding: __init__.py (stored 0%)
adding: __main__.py (stored 0%)
adding: spam.py (stored 0%)
adding: spam.pyc (deflated 32%)
[steve at ando python]$ mv myscript.zip myscript.py
[steve at ando python]$ python myscript.py
Success!
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.
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.
--
Steven
More information about the Tutor
mailing list