【问题标题】:py2exe fails with "No module named 'clr'" when trying to build exe from script using pythonnet尝试使用 pythonnet 从脚本构建 exe 时,py2exe 失败并显示“没有名为 'clr' 的模块”
【发布时间】:2015-05-12 10:31:35
【问题描述】:

我创建了一个使用 pythonnet 的 python 脚本。该脚本位于名为main.py 的文件中。当我从命令行运行脚本时(只需在 Windows 命令提示符下键入 main.py),导入的 pythonnet 模块 clr 工作正常。但是当我尝试构建一个 exe 时,我收到一条错误消息:No module named clr。

为了找出造成这种情况的原因,我已经验证了使用 py2exe 构建可执行文件(在我的例子中是一个简单的 Tkinter 应用程序)是可行的。我只安装了 Python 3.4 并验证了 where python 指向 C:\Python34\python.exe。

错误发生在可执行文件构建时,似乎是通过在我的setup.py 中的{"includes":["sip","clr"]}} 部分中包含clr 来触发py2exe。完整的追溯如下:

Traceback (most recent call last):
  File "setup.py", line 32, in <module>
    windows = [{'script': "main.py"}],
  File "C:\Python34\lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "C:\Python34\lib\distutils\dist.py", line 917, in run_commands
    self.run_command(cmd)
  File "C:\Python34\lib\distutils\dist.py", line 936, in run_command
    cmd_obj.run()
  File "C:\Python34\lib\site-packages\py2exe\distutils_buildexe.py", line 188, i
n run
    self._run()
  File "C:\Python34\lib\site-packages\py2exe\distutils_buildexe.py", line 267, i
n _run
    builder.analyze()
  File "C:\Python34\lib\site-packages\py2exe\runtime.py", line 164, in analyze
    mf.import_hook(modname)
  File "C:\Python34\lib\site-packages\py2exe\mf3.py", line 120, in import_hook
    module = self._gcd_import(name)
  File "C:\Python34\lib\site-packages\py2exe\mf3.py", line 273, in _gcd_import
    raise ImportError('No module named {!r}'.format(name), name=name)
ImportError: No module named 'clr'

我也阅读/尝试过这些:

https://docs.python.org/2/distutils/setupscript.html
https://pythonhosted.org/setuptools/setuptools.html
http://sourceforge.net/p/py2exe/mailman/message/6937658

引导我将clr.pyd 和Python.Runtime.dll 移动到各个位置,包括main.py、C:\Python34\Lib\site-packages(它们原来的位置)和C:\Python34\Lib\site-packages\py2exe 的位置

这些都没有奏效,我不知道下一步该尝试什么。我可以看到由于某种原因py2exe 找不到clr.pyd 或Python.Runtime.dll 或两者,但不明白为什么。有人有什么想法吗?


代码详情

我的main.py 脚本如下所示:

import clr
clr.AddReference("name.xxxx")
from name.xxxx import aaa
from clr import System

# All my functioning code, that I've verified works when run from the command line

这是我的setup.py 文件包含的内容(我留下了一些注释,以便您可以看到我尝试过的内容):

from distutils.core import setup
import py2exe, sys, os

mydata_files = []
for files in os.listdir('C:\\d\\Project\\TOOLS\\data_acquisition\\trunk\\DLL'):
    f1 = 'C:\\d\\Project\\TOOLS\\data_acquisition\\trunk\\DLL\\' + files
    if os.path.isfile(f1): # skip directories
        f2 = '.', [f1]
        mydata_files.append(f2)

setup(
  data_files=mydata_files,

  # options = {"py2exe" : {"includes" : "module1,module2,module3"}}
  options = {"py2exe": {"includes":["sip", "clr"]}},
  # options = {'py2exe': {'bundle_files': 1 , 'compressed': True,"includes":["sip"]}},
  #python setup.py py2exe
  #CLR.dll and PythonRuntime.dll 
  # options = {'py2exe': {'bundle_files': 1, "skip_archive":1 ,"includes":["sip"]}},
  windows = [{'script': "main.py"}],
  # data_files=mydata_files,
  # zipfile = None
)

如果我将options = {"py2exe": {"includes":["sip", "clr"]}}, 行更改为options = {"py2exe": {"includes":["sip"]}},,则.exe 会构建,但显然无法正常运行。

【问题讨论】:

  • 在上面的代码中,for 循环后的缩进是错误的。我假设这是一个错字,但如果你已经尝试了所有的建议,可能你没有得到你认为你在mydata_files中所拥有的东西.
  • 不是mydata_files,py2exe找不到模块clr,witch被导入到我项目的一个文件中。有什么想法吗?
  • 只有我之前在聊天中提供的那些你现在说你阅读并尝试过的那些。我的诚实意见是 Python 不知道 Python.Runtime.dll 在哪里,并且报告说无法导入 clr。我从未使用过 Python.net,仅在您直接寻求帮助时才对此做出回应。无论如何,您都应该更正 for 循环 - 因为在您的问题中有错误代码是不好的,并且可能会分散任何人的注意力。我只是想知道潜在的错误是否是 mydata_files 中的路径错误,但如果您确信这不是错误,那很好 - 我不是这里的专家。
  • 您是否确实尝试过将 Python.Runtime.dll 像链接建议的那样放在项目的主目录中(因此保证可以找到)?另外-如果您粘贴完整的回溯会有所帮助-然后人们可以帮助您进行调试-而不仅仅是结束消息“没有名为 clr 的模块”
  • 其实——再想一想——它可能找不到clr.pyd。一些谷歌搜索出现了这个线程 - tech-artists.org/forum/… 。对于不同的工具,但请注意响应 " 启动并运行它的快速肮脏方法是将位置附加到 userSetup.py 或脚本中的 sys.path 中。出于某种原因,它不会如果该位置位于 PYTHONPATH 上,则喜欢它。” 希望这里的内容可以帮助您找到问题。让我们知道这些 cmets 的运行情况

标签: python .net python-3.4 py2exe python.net


【解决方案1】:

###安装说明

作为参考,我使用pip install py2exe 执行了py2exe 的标准安装。这会将py2exe 放入Lib\site-packages 以供您安装python。接下来,我通过从 Christoph Gohlke 的unofficial Windows binaries page 下载.whl 来安装pythonnet,然后使用pip install path\to\pythonnet-2.0.0&lt;version_numbers&gt;.whl。这会将clr.pyd 和Python.Runtime.dll 放入Lib\site-packages 以供您安装python。 This question and answers有更多信息。

###问题

这是py2exe 的一个相当奇怪的行为,很难调试。我认为这纯粹是该工具中的一个错误。另外,错误信息也无济于事。

问题在于模块clr 被工具通过其hooks.py 文件明确排除。目前尚不清楚为什么。你可以看到line that does this exclusion here。

###解决方案

解决方法是从 py2exe 安装中的 windows_excludes 变量 hooks.py 文件中删除单词 clr。假设一切都在其标准位置 - 这意味着删除位于 C:\Python34\Lib\site-packages\py2exe 的文件 hooks.py 中的第 23 行。您还需要确保Python.Runtime.dll 以某种方式与您的.exe 打包在一起——我通过将其添加到数据文件中进行了测试。这是我测试和工作的示例 - 我使用了一个非常简单的main.py 来说明导入并确保自己程序确实在工作。我让你的 setup.py 尽可能接近你的版本,注释掉不适合我系统的行

要实际创建.exe - 使用以下命令(如果 python 是 Python 3 安装的别名,则可能不需要 python.exe 的路径)

C:\python34\python.exe setup.py py2exe

###main.py

import clr
# I import clr, but don't use it as this is not my
# expertise.  The fact it imports without error means
# I'm pretty sure it will work

with open('out.txt','a') as f:
    for i in range(30):
        f.write(str(i))

###setup.py

from distutils.core import setup
import py2exe, sys, os

mydata_files = []

# I had to comment these out as they did not apply to my test environment
# for files in os.listdir('C:\\d\\Project\\TOOLS\\data_acquisition\\trunk\\DLL'):
# f1 = 'C:\\d\\Project\\TOOLS\\data_acquisition\\trunk\\DLL' + files
# if os.path.isfile(f1): # skip directories
    # f2 = 'dll', [f1]
    # mydata_files.append(f2)
    
# It's essential that the Python.Runtime.dll is packaged with main.exe
# This is how I've done it
mydata_files.append(('.',['C:\\Python34\\Lib\\site-packages\\Python.Runtime.dll']))

setup(
 data_files=mydata_files,

# I've left all your commented lines in - they weren't necessary for my test
# options = {"py2exe" : {"includes" : "module1,module2,module3"}}

# I haven't included sip as I don't have it installed, but I think it will work
options = {"py2exe": {"includes":["clr"]}},
# options = {'py2exe': {'bundle_files': 1 , 'compressed': True,"includes":["sip"]}},
#python setup.py py2exe
#CLR.dll and PythonRuntime.dll 
# options = {'py2exe': {'bundle_files': 1, "skip_archive":1 ,"includes":["sip"]}},
windows = [{'script': "main.py"}],
# data_files=mydata_files,
# zipfile = None
)

编辑: 对于任何感兴趣的人 - 我在 a chat conversation 中更详细地描述了我是如何隔离和发现这个错误的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 2016-05-24
    • 1970-01-01
    相关资源
    最近更新 更多