I am trying to include a source distribution in a program I am writing.

However, I want it to include the main .py file AND any other modules/packages it used.

Here is my setup.py script:

from distutils.core import setup

setup_options = {
'name': 'somename', 'version': '1.11',
'author': 'developer', 'author_email': 'email', 'py_modules': ['mymodule'], 
}

setup(**setup_options)

However, using command line to run python setup.py bdist only creates a folder with mymodule.py.

Also, another script I have (to create a standalone .exe) does not include the data files:

import sys
from cx_Freeze import setup, Executable

base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(name = "somename",
    version = "1.11",
    description = "some description",
    executables = [Executable("mymodule.py", base=base)],
    data_files = ['helpData.pkl', 'General Public License - 3.0.pdf'])

(I am executing it using python setup.py bdist --format=msi).

How can I include all of the modules for my first setup script and include the data files for my second script? Thanks!

有帮助吗?

解决方案

You've got two separate questions here.


How can I include all of the modules for my first setup script

distutils can't do that, and isn't intended to. That's why things like cx_Freeze, zc.buildout, etc. exist in the first place.

If you know the exact set of modules that you need, you can just specify them explicitly.

If you want to have Python gather them up for you, you'll need to do some extra work. The simplest solution is to use the dependency-finder code out of cx_Freeze or py2exe or whatever you prefer. (Some of them have a "semi-standalone" flag that skips over stdlib modules; others will require you to filter it yourself.)


… and include the data files for my second script?

You're confusing yourself by trying to merge the freeze step and the installer-building step into one.

First, you need to pass the data files to the freeze step, to get them copied to appropriate relative paths alongside the .exe, as covered in cx_freeze FAQ.

Then you can give the bdist-msi step the files to package up.

To do that, you need to give the right paths in either a package_data or a data_files. A list of plain filenames in data_files will probably do the wrong thing, as well as giving you a warning. You're not supposed to do that, and, rather than try to figure out why it's breaking in a different way than you hoped for it to break, it's better to do the right thing. See the distutils docs for the relevant sections.


Meanwhile, whenever distutils isn't doing what you expect, and you don't know why, you should run it with the debug flag. Even if it doesn't tell you the answer, it will at least give you more information that you can post to places like SO, so it can tell someone else the answer (and they can tell you). Just set DISTUTILS_DEBUG to the string "True" in your environment, then run your setup again.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top