python 在 py2exe 构建中包含 PYD/DLL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/220777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:40:07  来源:igfitidea点击:

Including PYDs/DLLs in py2exe builds

pythondllinstallationpy2exepyd

提问by TheObserver

One of the modules for my app uses functions from a .pyd file. There's an option to exclude dlls (exclude_dlls) but is there one for including them? The build process doesn't seem to be copying the .pyd in my module despite copying the rest of the files (.py). I also need to include a .dll. How do I get py2exe to include both .pyd and .dll files?

我的应用程序的模块之一使用 .pyd 文件中的函数。有一个选项可以排除 dll (exclude_dlls),但是有没有包含它们的选项?尽管复制了其余文件 (.py),但构建过程似乎并未复制我模块中的 .pyd。我还需要包含一个 .dll。如何让 py2exe 包含 .pyd 和 .dll 文件?

回答by Tony Meyer

.pyd's and .DLL's are different here, in that a .pyd ought to be automatically found by modulefinder and so included (as long as you have the appropriate "import" statement) without needing to do anything. If one is missed, you do the same thing as if a .py file was missed (they're both just modules): use the "include" option for the py2exe options.

.pyd 和 .DLL 在这里是不同的,因为 .pyd 应该由 modulefinder 自动找到并包含在内(只要您有适当的“导入”语句),而无需执行任何操作。如果遗漏了一个,您会做与遗漏 .py 文件相同的事情(它们都只是模块):对 py2exe 选项使用“include”选项。

Modulefinder will not necessarily find dependencies on .DLLs (py2exe can detect some), so you may need to explicitly include these, with the 'data_files' option.

Modulefinder 不一定会找到 .DLL 的依赖项(py2exe 可以检测到一些),因此您可能需要使用 'data_files' 选项明确包含这些。

For example, where you had two .DLL's ('foo.dll' and 'bar.dll') to include, and three .pyd's ('module1.pyd', 'module2.pyd', and 'module3.pyd') to include:

例如,您有两个 .DLL(“foo.dll”和“bar.dll”)要包含,三个 .pyd(“module1.pyd”、“module2.pyd”和“module3.pyd”)要包含包括:

setup(name='App',
      # other options,
      data_files=[('.', 'foo.dll'), ('.', 'bar.dll')],
      options = {"py2exe" : {"includes" : "module1,module2,module3"}}
     )

回答by John Millikin

If they're not being automatically detected, try manually copying them into py2exe's temporary build directory. They will be included in the final executable.

如果没有自动检测到它们,请尝试手动将它们复制到 py2exe 的临时构建目录中。它们将包含在最终的可执行文件中。

回答by Claudiu

You can modify the setup script to copy the files explicitly:

您可以修改安装脚本以显式复制文件:

script = "PyInvaders.py"        #name of starting .PY
project_name = os.path.splitext(os.path.split(script)[1])[0]
setup(name=project_name, scripts=[script]) #this installs the program

#also need to hand copy the extra files here
def installfile(name):
    dst = os.path.join('dist', project_name)
    print 'copying', name, '->', dst
    if os.path.isdir(name):
    dst = os.path.join(dst, name)
    if os.path.isdir(dst):
        shutil.rmtree(dst)
    shutil.copytree(name, dst)
    elif os.path.isfile(name):
    shutil.copy(name, dst)
    else:
    print 'Warning, %s not found' % name

pygamedir = os.path.split(pygame.base.__file__)[0]
installfile(os.path.join(pygamedir, pygame.font.get_default_font()))
installfile(os.path.join(pygamedir, 'pygame_icon.bmp'))
for data in extra_data:
    installfile(data)

etc... modify to suit your needs, of course.

等等...当然可以根据您的需要进行修改。

回答by John Fouhy

Maybe you could use the data_files option to setup():

也许您可以将 data_files 选项用于 setup():

import glob
setup(name='MyApp',
      # other options,
      data_files=[('.', glob.glob('*.dll')),
                  ('.', glob.glob('*.pyd'))],
     )

data_files should be a list of tuples, where each tuple contains:

data_files 应该是元组列表,其中每个元组包含:

  1. The target directory.
  2. A list of files to copy.
  1. 目标目录。
  2. 要复制的文件列表。

This won't put the files into library.zip, which shouldn't be a problem for dlls, but I don't know about pyd files.

这不会将文件放入 library.zip,这对于 dll 应该不是问题,但我不知道 pyd 文件。