使用cx_freeze时导入PIL时出错

时间:2015-03-07 00:53:46

标签: python python-3.x python-imaging-library cx-freeze

我现在整整一天都在这里,而且我是把电脑扔到窗外。我使用tkinter,matplotlib和PIL创建了一个UI。如果我运行代码这完全有效,但是当使用cx_freeze冻结时,我在执行python setup.py build时会得到以下信息:

c:\Python33>python setup.py build
running build
running build_exe
Traceback (most recent call last):
  File "setup.py", line 18, in <module>
    executables = [Executable("interface.py", base = base, icon="link.ico")]
  File "c:\Python33\lib\site-packages\cx_Freeze\dist.py", line 365, in setup
    distutils.core.setup(**attrs)
  File "c:\Python33\lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "c:\Python33\lib\distutils\dist.py", line 929, in run_commands
    self.run_command(cmd)
  File "c:\Python33\lib\distutils\dist.py", line 948, in run_command
    cmd_obj.run()
  File "c:\Python33\lib\distutils\command\build.py", line 126, in run
    self.run_command(cmd_name)
  File "c:\Python33\lib\distutils\cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "c:\Python33\lib\distutils\dist.py", line 948, in run_command
    cmd_obj.run()
  File "c:\Python33\lib\site-packages\cx_Freeze\dist.py", line 235, in run
    freezer.Freeze()
  File "c:\Python33\lib\site-packages\cx_Freeze\freezer.py", line 575, in Freeze

    self.finder = self._GetModuleFinder()
  File "c:\Python33\lib\site-packages\cx_Freeze\freezer.py", line 330, in _GetMo
duleFinder
    finder.IncludePackage(name)
  File "c:\Python33\lib\site-packages\cx_Freeze\finder.py", line 579, in Include
Package
    module = self._ImportModule(name, deferredImports)
  File "c:\Python33\lib\site-packages\cx_Freeze\finder.py", line 288, in _Import
Module
    raise ImportError("No module named %r" % name)
ImportError: No module named 'PIL'

我的源代码在导入中有以下内容:

import pymysql
import csv
from itertools import groupby
from collections import defaultdict
from collections import OrderedDict
import math
import os
import glob
from tkinter import *
from tkinter import ttk
import tkinter.filedialog
import tkinter.messagebox
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from tkinter import PhotoImage
import PIL
import win32com.client

正如我上面所说,它运作正常。有没有办法手动进行导入或类似的东西?我在许多网站上都看到cx_freeze有时会导致某些模块的某些依赖项出现问题。

最初,我只是编译并得到了该错误,并在点击结果导致的可执行文件时得到了上述错误。我试图直接在setup.py文件中添加PIL模块并在cmd上得到相同的错误。

这是setup.py,以防万一:

import sys

from cx_Freeze import setup, Executable



base = None
if sys.platform == "win32":
    base = "Win32GUI"

build_exe_options = {"packages": ["PIL"]}

setup(
        name = "Link",
        version = "1.0",
        description = "SLA screen program for CRM.",
        options = {"build_exe": build_exe_options}, 
        executables = [Executable("interface.py", base = base, icon="link.ico")]


)

有关如何使其发挥作用的任何想法?

谢谢!

2 个答案:

答案 0 :(得分:3)

编辑:我很高兴这个解决方案适合您!我会在评论中将其移到此处,以防它可以帮助其他任何人。


<强>解决方案: 我在StackOverflow上发现了类似的错误,OP似乎找到了解决他自己问题的方法。他说他在他正在安装的软件包版本上错误地使用了pip install,并且它将软件包下载为.egg文件,这似乎会导致cx_freeze出现问题。可能不是解决方案,但在故障排除方面值得检查。

cx_freeze how to include 3rd party modules, ImportError: No module named progressbar

答案 1 :(得分:0)

我有相同的错误消息并通过执行这些步骤解决了这个问题(我使用的是Windows):

  1. 从cmd提示符(pip uninstall Pillow
  2. 卸载Pillow
  3. 下载.exe file version of new Pillow package

    在我的情况下,我使用的是Python 3.4,因此我选择了适合我的Python版本的.exe文件。

  4. 安装exe文件
  5. 再次从cmd提示符(C:/python34/python/python setup.py build
  6. 运行setup.py

    不再有错误。