Differences between revisions 1 and 17 (spanning 16 versions)
Revision 1 as of 2003-11-01 01:14:49
Size: 366
Editor: dclient80-218-75-60
Comment:
Revision 17 as of 2008-07-08 11:27:43
Size: 1520
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Question: Is it possible to specify options like "--includes" programaticaly in the setup()
function?
= Avoid sys.argv to pass options to py2exe =
Many users write a special setup script for py2exe that simply can be run to build the exe, without the need to specify command line options by hand, each time. That means that "includes", "excludes" options etc have to be passed in some way to py2exe.
Line 4: Line 4:
Answer:
Yes, distutils has such a functionality, although it seems to be undocumented. You can create a dictionary like this:
The {{{setup()}}} function accepts a {{{options}}} keyword argument, containing a dictionary with the options. This is superior to appending options to {{{sys.argv}}} as the transformation of the data to a string and back is avoided as well as mutiple {{{setup()}}} calls per file are possible.
Line 7: Line 6:
opts = {"py2exe": {"includes": "mod1, mod2"}} Note that the long name of options has to be used and '-' in the command line options become '_' in the dictionary (e.g. "dist-dir" becomes "dist_dir").
Line 9: Line 8:
and pass it to the setup script: {{{
opts = {
    "py2exe": {
        "includes": "mod1, mod2",
        "dist_dir": "bin",
    }
}
}}}
Line 11: Line 17:
setup(...
      options = opts,
     )
And pass it to the setup script:
Line 15: Line 19:
{{{
setup(
    options = opts,
    ...
)
}}}

= Avoid using setup parameters that are py2exe-specific =

Instead of passing options like {{{console}}}, {{{windows}}} to {{{setup()}}}, you can subclass Distribution and initialize them as follows:

{{{
from distutils.core import Distribution
class MyDistribution(Distribution):
    def __init__(self, attrs):
        self.com_server = []
        self.services = []
        self.windows = []
        self.console = ['myapp']
        self.zipfile = 'mylibrary.zip'
        Distribution.__init__(self, attrs)

setup(distclass=MyDistribution)
}}}

Avoid sys.argv to pass options to py2exe

Many users write a special setup script for py2exe that simply can be run to build the exe, without the need to specify command line options by hand, each time. That means that "includes", "excludes" options etc have to be passed in some way to py2exe.

The setup() function accepts a options keyword argument, containing a dictionary with the options. This is superior to appending options to sys.argv as the transformation of the data to a string and back is avoided as well as mutiple setup() calls per file are possible.

Note that the long name of options has to be used and '-' in the command line options become '_' in the dictionary (e.g. "dist-dir" becomes "dist_dir").

opts = {
    "py2exe": {
        "includes": "mod1, mod2",
        "dist_dir": "bin",
    }
}

And pass it to the setup script:

setup(
    options = opts,
    ...
)

Avoid using setup parameters that are py2exe-specific

Instead of passing options like console, windows to setup(), you can subclass Distribution and initialize them as follows:

from distutils.core import Distribution
class MyDistribution(Distribution):
    def __init__(self, attrs):
        self.com_server = []
        self.services = []
        self.windows = []
        self.console = ['myapp']
        self.zipfile = 'mylibrary.zip'
        Distribution.__init__(self, attrs)

setup(distclass=MyDistribution)

PassingOptionsToPy2Exe (last edited 2008-07-08 11:27:43 by localhost)