简体   繁体   English

从script.py运行Python可执行文件时出错

[英]Error with running Python executable from script.py

I created an executable .exe from a .py file using the latest version of cx_freeze for Python 2.7 with the following setup.py file: 我使用最新版本的Python 2.7 cx_freeze和以下setup.py文件从.py文件创建了一个可执行文件.exe

import sys
from cx_Freeze import setup, Executable

setup(
    name = "Any",
    version = "1.0",
    description = "Any",
    executables = [Executable("D:\\script.pyw", base = "Win32GUI")])

I ran it from command line: 我从命令行运行它:

python setup.py build

Then I got the executable file in the build directory as expected. 然后,我按预期将可执行文件放在生成目录中。 It ran perfectly. 运行得很好。

Now, If I move the executable file into a different directory on the same computer. 现在,如果我将可执行文件移动到同一台计算机上的其他目录中。 I get the following error dialog: 我收到以下错误对话框:

在此处输入图片说明

My guess is that I need to embed something into the executable file to make it work not only on my computer but on any other computer where Python is not installed by changing something in the setup.py file. 我的猜测是,我需要在可执行文件中嵌入一些内容,以使其不仅可以在我的计算机上运行,​​而且还可以通过更改setup.py文件中的某些内容在未安装Python的任何其他计算机上运行。 What could be it missing? 可能会丢失什么?

I am building compiled executable as follows. 我正在构建编译的可执行文件,如下所示。

Using following versions: 使用以下版本:

OS: Windows-7-6.1.7601-SP1
Python: 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]
cx_Freeze: 4.2.3

I have this code hello_tkinter.py : 我有这段代码hello_tkinter.py

from Tkinter import *
import ttk

class Main(Tk):
    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)

        self.frame = ttk.Frame(self)
        self.frame.pack(expand=True, fill=BOTH, padx=5, pady=5)

        self.button = ttk.Button(self.frame, text="Test")
        self.button.pack(expand=True, fill=BOTH)

root = Main()
root.mainloop()

I call this script installed with cx_freeze : 我称此脚本与cx_freeze安装:

c:\Python27\Scripts\cxfreeze.bat hello_tkinter.py --target-dir=Bin/tkinter --base-name=Win32GUI --target-name=hello_tkinter.exe

And I get the directory containing: 我得到的目录包含:

tcl\
tk\
_ctypes.pyd
_tkinter.pyd
bz2.pyd
hello_tkinter.exe
MSVCR90.dll
python27.dll
tcl85.dll
tk85.dll
unicodedata.pyd

It works fine and even if I move the directory. 即使我移动目录,它也能正常工作。 You did take the whole directory, not just the exe, did not you? 您确实获得了整个目录,而不仅仅是exe,不是吗? The executable was also tested on Windows XP without Python installed. 该可执行文件还在未安装Python的Windows XP上进行了测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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