Search code examples
pythonpython-3.xpython-imaging-librarycx-freeze

Error importing PIL when using cx_freeze


I've been at this the whole day now and I'm about throw my computer out the window. I made a UI using tkinter, matplotlib and PIL. If I run the code this works perfectly, however when freezing using cx_freeze, I get the following when doing 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'

My source code has the following in the imports:

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

And as I said above it works fine. Is there a way ta manually do the import or something similar? I've read on many websites that cx_freeze has issues sometimes importing some dependencies of some modules.

Originally, I was just compiling and got that error and got the error above when clicking on the executable that comes as a result. I tried to add the PIL module directly on the setup.py file and got that same error but on the cmd.

Here is the setup.py just in case:

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")]


)

Any ideas on how to get this to work?

Thanks!!


Solution

  • Edit: I'm glad this solution worked for you! I'll move it here from the comments in case it can help anyone else.


    Solution: I found a similar error on StackOverflow, and the OP seemed to have found a solution to his own problem. He said that he incorrectly used pip install on a version of a package he was installing, and it made the package download as a .egg file, which seems to cause problems with cx_freeze. Might not be the solution, but it's worth checking out in terms of troubleshooting.

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