简体   繁体   中英

Why do I get an error when adding additional libraries when trying to freeze my python application using cx_freeze?

I'm trying to create an executable for my application using cx_Freeze and I keep getting the same error when I try and add additional libraries. The following code works fine without trying to 'import numpy'. I originally thought the error was from having multiple files and incorrectly scripting my setup file so that is why I have the code split up.

ToDotExe.py

from mathLibrary import *
from tkinter import *
from tkinter import simpledialog

window=Tk()

btn = Button(window, text = "This prompts a dialog box for 5x",command = lambda: timesByFive(window))
btn.grid()
btn2 = Button(window, text = "This prompts a dialog box for cosx",command = lambda: operatebycos(window))
btn2.grid()

window.title("Basic Calculator")
window.geometry("300x200+10+20")
window.mainloop()

MathLibrary.py

from tkinter import simpledialog
from numpy import cos


def timesByFive(master):
    answer = 1
    while not answer is None:
        answer = simpledialog.askinteger("Input", "What would you like to times by 5?",
                                parent=master)
        print(answer)
        try:
            print (answer*5)
        except TypeError:
            if answer is None:
                break
            pass

def operatebycos(master):
    answer = 1
    while not answer is None:
        answer = simpledialog.askinteger("Input", "What would you like to operate on by cos?",
                                parent=master)
        print(answer)
        try:
            print (cos(answer))
        except TypeError:
            if answer is None:
                break
            pass

Setup.py

from cx_Freeze import setup,Executable

includes = []
excludes = []
packages = ["tkinter","numpy"]
includefiles = []

setup(
    name = 'toDotExe',
    version = '0.1',
    description = 'A general enhancement utility',
    author = 'YosTruly',
    author_email = 'le...@null.com',
    options = {'build_exe': {'includes':includes,'excludes':excludes,'packages':packages,'include_files':includefiles}}, 
    executables = [Executable('toDotExe.py')]
)

Error:

Traceback (most recent call last):
  File "setup.py", line 15, in <module>
    executables = [Executable('toDotExe.py')]
  File "C:\Users\Jerwin\AppData\Local\Programs\Python\Python37\lib\site-packages\cx_Freeze\dist.py", line 342, in setup
    distutils.core.setup(**attrs)
  File "C:\Users\Jerwin\AppData\Local\Programs\Python\Python37\lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "C:\Users\Jerwin\AppData\Local\Programs\Python\Python37\lib\distutils\dist.py", line 966, in run_commands
    self.run_command(cmd)
  File "C:\Users\Jerwin\AppData\Local\Programs\Python\Python37\lib\distutils\dist.py", line 985, in run_command
    cmd_obj.run()
  File "C:\Users\Jerwin\AppData\Local\Programs\Python\Python37\lib\distutils\command\build.py", line 135, in run
    self.run_command(cmd_name)
  File "C:\Users\Jerwin\AppData\Local\Programs\Python\Python37\lib\distutils\cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "C:\Users\Jerwin\AppData\Local\Programs\Python\Python37\lib\distutils\dist.py", line 985, in run_command
    cmd_obj.run()
  File "C:\Users\Jerwin\AppData\Local\Programs\Python\Python37\lib\site-packages\cx_Freeze\dist.py", line 217, in run
    freezer.Freeze()
  File "C:\Users\Jerwin\AppData\Local\Programs\Python\Python37\lib\site-packages\cx_Freeze\freezer.py", line 645, in Freeze
    self._WriteModules(fileName, self.finder)
  File "C:\Users\Jerwin\AppData\Local\Programs\Python\Python37\lib\site-packages\cx_Freeze\freezer.py", line 536, in _WriteModules
    sourcePackageDir = os.path.dirname(module.file)
  File "C:\Users\Jerwin\AppData\Local\Programs\Python\Python37\lib\ntpath.py", line 221, in dirname
    return split(p)[0]
  File "C:\Users\Jerwin\AppData\Local\Programs\Python\Python37\lib\ntpath.py", line 183, in split
    p = os.fspath(p)
TypeError: expected str, bytes or os.PathLike object, not NoneType

C:\Users\Jerwin\Desktop\Trying to create .Exe>

I would suggest you use pyinstaller .

  • First, install it: pip install pyinstaller
  • Then in the terminal type pyintaller --onefile -w ToDotExe.py

The flag --onefile will give you a single exe file instead of a bunch of other files. Whereas -w this will stop python from bringing up the console/terminal during execution.

Note : Only use -w only when your console doesn't require the console to remain open. (Don't use it if you are taking input from the user, for example).

You can watch this video for more information.
You can also refer to the official documentation

I had a similar issue that cxfreeze somehow got wrong set of libraries. I had to remove those erratic libs after building, then copy & paste those from site-packages folder of my anaconda environment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM