为什么在尝试使用cx\U freeze冻结python应用程序时添加其他库时出错?

2024-06-28 11:36:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用cx\U Freeze为我的应用程序创建一个可执行文件,但在尝试添加其他库时,我不断遇到相同的错误。以下代码在不尝试“导入numpy”的情况下工作正常。我最初认为错误是因为有多个文件,并且错误地编写了安装文件的脚本,所以我将代码拆分

todotex.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')]
)

错误:

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>

Tags: runanswerinpyliblocallineusers
1条回答
网友
1楼 · 发布于 2024-06-28 11:36:04

我建议您使用pyinstaller

  • 首先,安装它:pip install pyinstaller
  • 然后在终端类型pyintaller onefile -w ToDotExe.py

标志 onefile将为您提供一个exe文件,而不是一堆其他文件。而-w这将阻止python在执行期间启动控制台/终端

注意:仅当您的控制台不需要保持打开状态时才使用-w。(例如,如果您从用户处获取输入,请不要使用它)

您可以观看this video了解更多信息。
您也可以参考official documentation

相关问题 更多 >