首页 > 解决方案 > 使用 py2exe 编译 python 会产生错误:AttributeError: 'GenericRdata' object has no attribute 'target'

问题描述

我有一组 Python 3 源文件(其中一个名为eventcythonized),我正在尝试main.exe 使用以下设置脚本将其转换为setup.py

setup(
    name="event",
    cmdclass={"build_ext": build_ext},
    ext_modules=ext_modules,
    windows=[{'script': 'main.py'}],
)

我在尝试打开时遇到的错误main.exe是:

Traceback (most recent call last):
  File "main.py", line 6, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 627, in _load_backward_compatible
  File "<frozen zipimport>", line 259, in load_module
  File "mongo.pyc", line 9, in <module>
  File "pymongo\mongo_client.pyc", line 639, in __init__
  File "pymongo\uri_parser.pyc", line 500, in parse_uri
  File "pymongo\srv_resolver.pyc", line 102, in get_hosts
  File "pymongo\srv_resolver.pyc", line 86, in _get_srv_response_and_hosts
  File "pymongo\srv_resolver.pyc", line 87, in <listcomp>
AttributeError: 'GenericRdata' object has no attribute 'target'

我考虑过将所有 .py 文件转换为 .c,然后使用 MSVC 将其编译为 exe,但不确定这是正确的解决方法。

有没有人遇到过这个?

非常感谢您提前回复!

标签: pythonpython-3.xpymongocythonpy2exe

解决方案


在OP的问题之后有点晚了,但以防万一这仍然需要,并在未来为其他人省去麻烦:

我无法从 OP 的帖子中确切确认您的打包过程是什么,但我在使用which containsAttributeError: 'GenericRdata' object has no attribute 'target'打包项目时遇到了同样的问题。经过大量挖掘,似乎问题在于它没有获取' 的依赖项,我通过在构建选项中指定来修复它:cx_freezepymongopymongodnspythondns

build_exe_options = {
    "packages": ["dns"],
    "excludes": ["tkinter"],
}

base = "Console"

setup(
    name="demo",
    version="0.0.0",
    description="demo exe",
    options={"build_exe": build_exe_options},
    executables=[
        Executable(
            "demo.py",
            base=base,
        )
    ]
)

我没有用其他打包程序(py2exe、PyInstaller 等)对此进行测试,但如果他们有同样的问题,这可能是相同的根本原因。


推荐阅读