我在 Windows 系统上使用 Python 2.6 和 cx_Freeze 4.1.2。我已经创建了 setup.py 来构建我的可执行文件,一切正常。

当 cx_Freeze 运行时,它将所有内容移动到 build 目录。我还有一些其他文件希望包含在我的文件中 build 目录。我怎样才能做到这一点?这是我的结构:

src\
    setup.py
    janitor.py
    README.txt
    CHNAGELOG.txt
    helpers\
        uncompress\
            unRAR.exe
            unzip.exe

这是我的片段:

设置

( name='Janitor',
  version='1.0',
  description='Janitor',
  author='John Doe',
  author_email='john.doe@gmail.com',
  url='http://www.this-page-intentionally-left-blank.org/',
  data_files = 
      [ ('helpers\uncompress', ['helpers\uncompress\unzip.exe']),
        ('helpers\uncompress', ['helpers\uncompress\unRAR.exe']),
        ('', ['README.txt'])
      ],
  executables =
      [
      Executable\
          (
          'janitor.py', #initScript
          )
      ]
)

我似乎无法让它发挥作用。我需要一个 MANIFEST.in 文件?

有帮助吗?

解决方案

弄清楚了。

from cx_Freeze import setup,Executable

includefiles = ['README.txt', 'CHANGELOG.txt', 'helpers\uncompress\unRAR.exe', , 'helpers\uncompress\unzip.exe']
includes = []
excludes = ['Tkinter']
packages = ['do','khh']

setup(
    name = 'myapp',
    version = '0.1',
    description = 'A general enhancement utility',
    author = 'lenin',
    author_email = 'le...@null.com',
    options = {'build_exe': {'includes':includes,'excludes':excludes,'packages':packages,'include_files':includefiles}}, 
    executables = [Executable('janitor.py')]
)

笔记:

  • include_files 必须包含“仅”相对路径 setup.py 脚本,否则构建将失败。
  • include_files 可以是字符串列表,即一堆文件及其相对路径
    或者
  • include_files 可以是元组列表,其中元组的前半部分是带有绝对路径的文件名,后半部分是带有绝对路径的目标文件名。

(当缺少文档时,请咨询 Kermit the Frog)

其他提示

有一个更复杂的例子: cx_freeze - wxPyWiki

所有选项的缺少文档位于: cx_Freeze(互联网档案馆)

cx_Freeze, ,不过,我仍然在单个文件夹中获得包含 11 个文件的构建输出,这与 Py2Exe.

备择方案: 包装|鼠标vs。Python

为了找到自己的附加文件(include_files = [-> your attached files <-]),你应该在你的setup.py代码插入以下功能:

def find_data_file(filename):
    if getattr(sys, 'frozen', False):
        # The application is frozen
        datadir = os.path.dirname(sys.executable)
    else:
        # The application is not frozen
        # Change this bit to match where you store your data files:
        datadir = os.path.dirname(__file__)

    return os.path.join(datadir, filename)

见CX-冻结:使用数据文件

您也可以创建单独的脚本,将构建后复制文件。这就是我用它来重建在Windows应用程序(你应该安装使“CP”作品“的GNU工具为Win32”)。

的build.bat:

cd .
del build\*.* /Q
python setup.py build
cp -r icons build/exe.win32-2.7/
cp -r interfaces build/exe.win32-2.7/
cp -r licenses build/exe.win32-2.7/
cp -r locale build/exe.win32-2.7/
pause
scroll top