cx-Freeze无法构建包含urllib和bs4的Python程序

0 投票
1 回答
1326 浏览
提问于 2025-04-18 14:23

我写了一个用Python编写的脚本,使用了urllib和bs4库,现在我想用cx-freeze把它变成一个可执行的文件。但是我遇到了问题,出现了以下错误:

Missing modules:
? Carbon imported from plistlib
? Carbon.File imported from plistlib
? Carbon.Files imported from plistlib
? MacOS imported from platform
? OpenSSL.SSL imported from requests.packages.urllib3.contrib.pyopenssl
? _dummy_threading imported from dummy_threading
? _emx_link imported from os
? _scproxy imported from urllib
? bs4.builder imported from bs4
? builtins imported from requests.packages.urllib3.packages.six
? cchardet imported from bs4.dammit
? ce imported from os
? chardet imported from bs4.dammit
? fcntl imported from subprocess
? gestalt imported from platform
? http imported from requests.compat
? http.client imported from requests.packages.urllib3.connectionpool
? http.cookies imported from requests.compat
? iconv_codec imported from bs4.dammit
? java.lang imported from platform
? ndg.httpsclient.ssl_peer_verification imported from requests.packages.urllib3.
contrib.pyopenssl
? ndg.httpsclient.subj_alt_name imported from requests.packages.urllib3.contrib.
pyopenssl
? netbios imported from uuid
? org.python.core imported from copy, pickle
? os.path imported from os, requests.certs, shlex
? os2 imported from os
? os2emxpath imported from os
? posix imported from os
? pwd imported from getpass, posixpath
? pyasn1.codec.der imported from requests.packages.urllib3.contrib.pyopenssl
? queue imported from requests.packages.urllib3.connectionpool
? riscos imported from os
? riscosenviron imported from os
? riscospath imported from os
? rourl2path imported from urllib
? simplejson imported from requests.compat
? termios imported from getpass
? urllib.parse imported from requests.compat, requests.packages.urllib3.request
? urllib.request imported from requests.compat
? vms_lib imported from platform
? win32api imported from platform
? win32con imported from platform
? win32pipe imported from platform
? win32wnet imported from uuid
This is not necessarily a problem - the modules may not be needed on this platfo
rm.

在我的程序中,我导入了bs4、requests、re和time这些库。我该怎么做呢?

1 个回答

1

如果你的构建失败是因为没有自动包含导入的库,你可以把这些库添加到你的setup.py文件的包部分。可以参考这篇文章

setup.py

import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["bs4, urllib, requests"], "excludes": [""]}

# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(  name = "my-app",
        version = "0.9.0",
        description = "Copyright 2013",
        options = {"build_exe": build_exe_options},
        executables = [Executable("my_module.py", base=base)])

你可能只需要指定bs4这个库,大多数模块应该会被自动找到并包含进去。BS4有已知的问题吗?所以一开始就试着把它包含进去吧。

这里是cx_freeze的文档。

撰写回答