Re: [Tutor] release a script with non-standard dependencies

2012-12-19 Thread Andre' Walker-Loud
Hi Rail,

 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.
 
 Any help is appreciated.

I would like to know the answer to your question as well.  
Hopefully, the people you work with are comfortable installing something like 
docopt - but I know that is often not the case.

If you are unsuccessful, and consider going back to OptParse, I just wanted to 
point you to using Argparse instead.  It is a bit better, and more importantly, 
is not deprecated (Optparse is no longer updated).
However, it shares the same issues the creator of docopt railed against in his 
presentation that Modulok sent a link to earlier.


Regards,

Andre
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] release a script with non-standard dependencies

2012-12-19 Thread Steven D'Aprano

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@ando python]$ ls test
ham.py  __init__.py  __main__.py  spam.py
[steve@ando python]$ python test
Success!


Now build a zip file, change the extension, and run it:

[steve@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@ando python]$ mv myscript.zip myscript.py
[steve@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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] release a script with non-standard dependencies

2012-12-19 Thread Oscar Benjamin
On 19 December 2012 23:45, Steven D'Aprano st...@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

Re: [Tutor] release a script with non-standard dependencies

2012-12-19 Thread eryksun
On Wed, Dec 19, 2012 at 6:45 PM, Steven D'Aprano st...@pearwood.info wrote:

 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).

This works in Windows. If it's a GUI app use the extension .pyw to run
without a console.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor