Search code examples
pythonmultiprocessingpython-3.5freezecx-freeze

cx_freeze keeps looking in python/libs, not in compiled libs


I cx_freezed a Python program (moved from cx_freeze 4.3.4 to 5.0), and I have a problem that apparently cx_freeze keeps looking for the libs it needs in Python's (Anaconda's) directory, instead of looking for it in the directory of the executable it produced.

The current error I'm getting is the following, as I'm using multiprocessing:

Traceback (most recent call last):
  File "D:\Anaconda3\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 12, in <module>
    __import__(name + "__init__")
  File "D:\Anaconda3\lib\site-packages\cx_Freeze\initscripts\Console.py", line 21, in <module>
    scriptModule = __import__(moduleName)
  File "Main.py", line 4, in <module>
  File "D:\Anaconda3\lib\multiprocessing\__init__.py", line 16, in <module>
    from . import context
  File "D:\Anaconda3\lib\multiprocessing\context.py", line 5, in <module>
    from . import process
ImportError: cannot import name 'process'

The directory you see up there is my Python installation!!!! Why is it looking there? Now in the frozen program, if I go to: myFrozenProgram/multiprocessing/, I find a file called Process.pyc!

My setup script is the following. It's a little more complicated than usual because I add custom dlls from numpy because of a problem that happened in the past. Also the TCL part I added due to an error with tcl:

import sys
import os
import json
import glob
from cx_Freeze import setup, Executable
from GUI.Meta import *
import os

PythonPath = os.path.split(sys.executable)[0] #get python path

os.environ['TCL_LIBRARY'] = os.path.join(PythonPath,"tcl","tcl8.6")
os.environ['TK_LIBRARY']  = os.path.join(PythonPath,"tcl","tk8.6")

mkl_files_json_file = glob.glob(os.path.join(PythonPath, "conda-meta","mkl-[!service]*.json"))[0] #json files that has mkl files list (exclude the "service" file)
with open(mkl_files_json_file) as file:
    mkl_files_json_data = json.load(file)

numpy_mkl_dlls = mkl_files_json_data["files"] #get the list of files from the json data file

np_dlls_fullpath = list(map(lambda currPath: os.path.join(PythonPath,currPath),numpy_mkl_dlls)) #get the full path of these files



includefiles = [("GUI/icon.png","GUI/icon.png") + np_dlls_fullpath
target = Executable("Main.py",
                    #base = "Win32GUI",
                    icon = "GUI/icon.ico",
                    targetName="MyProg.exe")

setup(
    name = "My program",
    version = SOFTWARE_VERSION,
    description = "Program",
    options = {'build_exe': {'include_files':includefiles, 'includes': ["sip","re","atexit","PyQt5.QtCore","PyQt5.QtGUI","PyQt5.QtWidgets","multiprocessing"]}},
    executables = [target])

Solution

  • It turns out that renaming Process.pyc to process.pyc solves the problem. Also another file with the same issue is from the QtGui library. The file QtGUI.pyd must be renamed to QtGui.pyd, and then everything works fine.