Python cx_freeze 资源问题
Python cx_freeze issue with resources
我正在使用 cx_freeze
模块来创建安装程序和可执行文件。这似乎工作正常,但在 运行 可执行文件上我得到以下错误和回溯。
D:\>"app.exe"
Traceback (most recent call last):
File "C:\Program Files\Python 3.3\lib\site-packages\pkg_resources\__init__.py", line 421, in get_provider
module = sys.modules[moduleOrReq]
KeyError: 'resources.images'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files\Python 3.3\lib\site-packages\cx_Freeze\initscripts\Console3.py", line 27, in <module>
exec(code, m.__dict__)
File "main.py", line 8, in <module>
File "C:\Program Files\Python 3.3\lib\site-packages\pkg_resources\__init__.py", line 1148, in resource_filename
return get_provider(package_or_requirement).get_resource_filename(
File "C:\Program Files\Python 3.3\lib\site-packages\pkg_resources\__init__.py", line 423, in get_provider
__import__(moduleOrReq)
File "C:\Python-bit.3\lib\importlib\_bootstrap.py", line 1567, in _find_and_load
File "C:\Python-bit.3\lib\importlib\_bootstrap.py", line 1514, in _find_and_load_unlocked
File "C:\Python-bit.3\lib\importlib\_bootstrap.py", line 313, in _call_with_frames_removed
File "C:\Python-bit.3\lib\importlib\_bootstrap.py", line 1567, in _find_and_load
File "C:\Python-bit.3\lib\importlib\_bootstrap.py", line 1531, in _find_and_load_unlocked
ImportError: No module named 'resources'
这很奇怪,因为当 运行 脚本通过 Python 时我没有得到这样的错误。 (具体来说,PyCharm 上的 Shift-F10。)
这是我在回溯中写的唯一脚本:
main.py
from tkinter import Tk
from client import Client
from pkg_resources import resource_filename
root = Tk()
root.wm_title("MyApp")
root.wm_iconbitmap(root, resource_filename('resources.images', 'eternal_logo.ico'))
root.wm_iconbitmap()
client = Client(root)
root.mainloop()
我的 setup.py 看起来像:
setup.py
from setuptools import find_packages
from pkg_resources import resource_filename
from cx_Freeze import setup, Executable
setup(
name='MyApp',
version='0.3-alpha',
packages=find_packages(),
package_data={'': ['*.png', '*.gif', '*.ico']},
executables=[
Executable(
"main.py",
icon=resource_filename('resources.images', 'eternal_logo.ico'),
targetName="app.exe"
)
]
)
最后我的文件结构(缩写)如下所示:
MyApp/
bin/
build/
client/
__init__.py
..
dist/
docs/
preferences/
__init__.py
..
resources/
__init__.py
images/
__init__.py
main.py
setup.py
__init__.py
这是不久前的事了,但由于我又遇到了类似的问题,所以我又回到了这里。
我相信解决此问题的方法是将以下 build_exe_options
添加到 setup.py
:
build_exe_options = {
"include_files": [
('resources', 'resources'),
('config.ini', 'config.ini')
]
}
我正在使用 cx_freeze
模块来创建安装程序和可执行文件。这似乎工作正常,但在 运行 可执行文件上我得到以下错误和回溯。
D:\>"app.exe"
Traceback (most recent call last):
File "C:\Program Files\Python 3.3\lib\site-packages\pkg_resources\__init__.py", line 421, in get_provider
module = sys.modules[moduleOrReq]
KeyError: 'resources.images'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files\Python 3.3\lib\site-packages\cx_Freeze\initscripts\Console3.py", line 27, in <module>
exec(code, m.__dict__)
File "main.py", line 8, in <module>
File "C:\Program Files\Python 3.3\lib\site-packages\pkg_resources\__init__.py", line 1148, in resource_filename
return get_provider(package_or_requirement).get_resource_filename(
File "C:\Program Files\Python 3.3\lib\site-packages\pkg_resources\__init__.py", line 423, in get_provider
__import__(moduleOrReq)
File "C:\Python-bit.3\lib\importlib\_bootstrap.py", line 1567, in _find_and_load
File "C:\Python-bit.3\lib\importlib\_bootstrap.py", line 1514, in _find_and_load_unlocked
File "C:\Python-bit.3\lib\importlib\_bootstrap.py", line 313, in _call_with_frames_removed
File "C:\Python-bit.3\lib\importlib\_bootstrap.py", line 1567, in _find_and_load
File "C:\Python-bit.3\lib\importlib\_bootstrap.py", line 1531, in _find_and_load_unlocked
ImportError: No module named 'resources'
这很奇怪,因为当 运行 脚本通过 Python 时我没有得到这样的错误。 (具体来说,PyCharm 上的 Shift-F10。)
这是我在回溯中写的唯一脚本:
main.py
from tkinter import Tk
from client import Client
from pkg_resources import resource_filename
root = Tk()
root.wm_title("MyApp")
root.wm_iconbitmap(root, resource_filename('resources.images', 'eternal_logo.ico'))
root.wm_iconbitmap()
client = Client(root)
root.mainloop()
我的 setup.py 看起来像:
setup.py
from setuptools import find_packages
from pkg_resources import resource_filename
from cx_Freeze import setup, Executable
setup(
name='MyApp',
version='0.3-alpha',
packages=find_packages(),
package_data={'': ['*.png', '*.gif', '*.ico']},
executables=[
Executable(
"main.py",
icon=resource_filename('resources.images', 'eternal_logo.ico'),
targetName="app.exe"
)
]
)
最后我的文件结构(缩写)如下所示:
MyApp/
bin/
build/
client/
__init__.py
..
dist/
docs/
preferences/
__init__.py
..
resources/
__init__.py
images/
__init__.py
main.py
setup.py
__init__.py
这是不久前的事了,但由于我又遇到了类似的问题,所以我又回到了这里。
我相信解决此问题的方法是将以下 build_exe_options
添加到 setup.py
:
build_exe_options = {
"include_files": [
('resources', 'resources'),
('config.ini', 'config.ini')
]
}