I am building a little Python 3.6.5 tool that takes in an XML document and outputs all its XPaths. It works well, however I need to distribute it in EXE form. I am running on Win10 and only need to provide to Win10 users.
我正在构建一个Python 3.6.5工具,它接收一个XML文档并输出所有的XPath。它工作得很好,但我需要以EXE形式分发它。我在Win10上运行,只需要提供给Win10用户。
My main code is;
我的主要代码是;
from tkinter import filedialog
from tkinter import *
from pathlib import Path
import winsound
from lxml import etree
import csv
root = Tk()
root.geometry("250x200")
root.resizable(0, 0)
class gui:
def __init__(self, master):
self.master = master
master.title("XGen")
self.welcome = Label(master, text="Welcome to")
self.welcome.config(font=("Arial", 10))
self.welcome.pack()
self.header = Label(master, text="The XPath Generator")
self.header.config(font=("Arial", 16))
self.header.pack()
self.description = Label(master, text="This Tool Takes in an XML Document\nand Provides all its XPaths")
self.description.config(font=("Arial", 10))
self.description.pack()
self.greet_button = Button(master, text="Select Input XML", command=self.greet)
self.greet_button.pack()
self.reportFilename = Label(master, text="")
self.reportFilename.pack()
self.reportProgress = Label(master, text="")
self.reportProgress.pack()
# self.close_button = Button(master, text="Close", command=master.quit)
# self.close_button.pack()
def greet(self):
print("File Selection Started")
from_file_path = filedialog.askopenfilename(initialdir="/",
title="Select file",
filetypes=(("XML Files", "*.xml"), ("all files", "*.*")))
from_file_path_split = Path(from_file_path).parts
to_file_path = ''
if from_file_path is '':
self.reportFilename.config(text="You Did not Select a File")
print("No File Selected. File Selection Ended")
else:
self.reportFilename.config(text="You Selected " + from_file_path_split[-1])
print("From File Path = " + from_file_path)
print("File Name = " + from_file_path_split[-1])
to_file_path = filedialog.asksaveasfilename(initialdir=from_file_path,
title="Save As",
filetypes=(("CSV Files", "*.csv"), ("all files", "*.*")))
if to_file_path is '' or to_file_path == 'Null':
self.reportProgress.config(text="Please select a Save Location")
# elif to_file_split[-1][:4] == "xsd":
else:
to_file_split = Path(to_file_path).parts
to_file_name = to_file_split[-1]
print("Filename = " + to_file_name)
to_file_extension = to_file_split[-1][-4:]
print("Last 4 chars = " + to_file_extension)
if to_file_extension == ".csv":
pass
else:
to_file_path = to_file_path + ".csv"
# to_file_name = to_file_path
print("To File Path = " + to_file_path)
if from_file_path == '' or to_file_path == '' or to_file_path == 'Null':
self.reportProgress.config(text="Please Select a Valid XML File")
winsound.PlaySound("SystemExclamation", winsound.SND_ALIAS)
print("Bad File, Try Again")
else:
out(from_file_path, to_file_path)
self.reportProgress.config(text="Generated " + Path(to_file_path).parts[-1])
print("XGen Complete")
def out(in_path, out_path):
with open(in_path, 'r') as source_file:
xml = source_file.read()
root = etree.fromstring(xml)
tree = etree.ElementTree(root)
line = ['XPath', 'Attribute Name', 'Current Data']
if out_path is None:
return 1
else:
pass
with open(out_path, 'w', newline='') as csv_file:
csv_writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
csv_writer.writerow(line)
for e in root.iter():
path = tree.getpath(e)
list_of_attributes = []
nodes = root.xpath(path)
for node in nodes:
attrs = []
for att in node.attrib:
attrs.append(("@" + att, node.attrib[att]))
list_of_attributes.append(attrs)
for attributes in list_of_attributes:
line = [path, None]
csv_writer.writerow(line)
if len(attributes) == 0:
pass
else:
for x in range(len(attributes)):
xpath = path + attributes[x][0]
current_data = (attributes[x][1])
attribute_name = attributes[x][0]
line = [xpath, attribute_name, current_data]
csv_writer.writerow(line)
my_gui = gui(root)
root.mainloop()
(Please forgive the messy code! I do intend to clean it up for the next version!)
(请原谅凌乱的代码!我打算为下一个版本清理它!)
setup.py
contains the following;
setup.py包含以下内容;
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
executables = [
Executable('xin.py', base=base)
]
setup(name='XGen',
version='0.1',
description='XGen',
executables=executables
)
This does allow the program to build an EXE, however when that EXE runs, it returns this error.
这确实允许程序构建一个EXE,但是当EXE运行时,它会返回此错误。
I have been looking everywhere for solutions.
我一直在寻找解决方案。
- I found a suggestion that uninstalling all versions of Python then installing just one might work, but no joy.
- I have previously tried Py2exe but that does not work either.
- I found another suggestion that the PATH could be incorrect, so checked and everything seems correct there too.
我发现了一个建议,即卸载所有版本的Python然后安装一个版本可能会有效,但没有任何乐趣。
我之前尝试过Py2exe,但这也不起作用。
我发现了另一个建议,即PATH可能不正确,所以检查了一切似乎也是正确的。
As suggested by the error text, I also tried checking that tkinter is properly installed. Not only can this program run through Python, but also IDLE lets me import tkinter
.
正如错误文本所示,我也尝试检查tkinter是否已正确安装。这个程序不仅可以通过Python运行,而且IDLE还可以导入tkinter。
What could be causing cx_Freeze to throw this error?
什么可能导致cx_Freeze抛出此错误?
Thanks!
1 个解决方案
#1
0
The location of the TK DLLs needs to be added to the setup.py
. On Windows 10, this might be in the root of C: or in C:\Users\[username]\AppData\Local\Programs\Python\Python36-32/DLLs
. Add a variable to store that path, then make it a raw string with a leading r
.
需要将TK DLL的位置添加到setup.py中。在Windows 10上,这可能位于C:或C:\ Users \ [用户名] \ AppData \ Local \ Programs \ Python \ Python36-32 / DLLs的根目录中。添加一个变量来存储该路径,然后使其成为带有前导r的原始字符串。
import os
import sys
from cx_Freeze import setup, Executable
PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
syspath = r"C:\Users\[username]\AppData\Local\Programs\Python\Python36-32/DLLs"
buildOptions = dict(
packages=[],
excludes=[],
include_files=[syspath + '/tcl86t.dll', syspath + '/tk86t.dll']
)
executables = [
Executable('xin.py', base=base)
]
setup(name='XGen',
version='0.1',
options=dict(build_exe=buildOptions),
description='XGen',
executables=executables
)
Many thanks to MrLeeh and DeePak M. Birajdar!
非常感谢MrLeeh和DeePak M. Birajdar!
#1
0
The location of the TK DLLs needs to be added to the setup.py
. On Windows 10, this might be in the root of C: or in C:\Users\[username]\AppData\Local\Programs\Python\Python36-32/DLLs
. Add a variable to store that path, then make it a raw string with a leading r
.
需要将TK DLL的位置添加到setup.py中。在Windows 10上,这可能位于C:或C:\ Users \ [用户名] \ AppData \ Local \ Programs \ Python \ Python36-32 / DLLs的根目录中。添加一个变量来存储该路径,然后使其成为带有前导r的原始字符串。
import os
import sys
from cx_Freeze import setup, Executable
PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
syspath = r"C:\Users\[username]\AppData\Local\Programs\Python\Python36-32/DLLs"
buildOptions = dict(
packages=[],
excludes=[],
include_files=[syspath + '/tcl86t.dll', syspath + '/tk86t.dll']
)
executables = [
Executable('xin.py', base=base)
]
setup(name='XGen',
version='0.1',
options=dict(build_exe=buildOptions),
description='XGen',
executables=executables
)
Many thanks to MrLeeh and DeePak M. Birajdar!
非常感谢MrLeeh和DeePak M. Birajdar!