exe转换python 2.7脚本

时间:2017-07-24 10:50:07

标签: python python-2.7 tkinter py2exe

我在python 2.7中编写了一个python脚本,我在使用py2exe包对该文件进行exe转换后遇到了问题。我有exe文件但是当我运行时,我得到了一个错误,我在下面提到过。请注意,我已保存名为test.py的脚本文件。请告诉我@Bryan Oakley的问题

from Tkinter import *
import os
from os.path import expanduser
import time
import datetime
from docx import Document
from docx.shared import Inches

document = Document()
docx_prefix = "Doc_File_"

home_directory = expanduser("~")
folder_directory = home_directory + "\myapp"
doc_file_directory = folder_directory + "/"

if not os.path.exists(folder_directory):
    os.makedirs(folder_directory)

def create_file():

        now = datetime.datetime.now()
        current_time = now.strftime("%Y-%m-%d %H%M%S")
        file_name =  docx_prefix + current_time

        document = Document()

        document.add_paragraph("word document")
        document.save(doc_file_directory + file_name + '.docx')

main_root = Tk()
main_root.title("Toolbar")
main_root.geometry('105x26+1+1')

toolbar = Frame(main_root, bg="gray")
toolbar.pack(side="top")

create_file_button = Button(toolbar, text="Create File", command = 
create_file, bg="gray", width=10)
create_file_button.pack(side="left")

main_root.mainloop()

使用py2exe进行exe转换的设置文件:

from distutils.core import setup
import py2exe
setup(
    console=['test.py'],
    options = {'py2exe': {'packages' : ['docx']}}
)

我在exe转换后遇到以下错误:

C:\Python27\Lib\site-packages\py2exe\samples\scripting\dist>test.exe
Traceback (most recent call last):
File "test.py", line 6, in <module>
File "docx\__init__.pyc", line 3, in <module>
File "docx\api.pyc", line 14, in <module>
File "docx\package.pyc", line 11, in <module>
File "docx\opc\package.pyc", line 12, in <module>
File "docx\opc\part.pyc", line 12, in <module>
File "docx\opc\oxml.pyc", line 12, in <module>
File "lxml\etree.pyc", line 12, in <module>
File "lxml\etree.pyc", line 10, in __load
File "src/lxml/lxml.etree.pyx", line 92, in init lxml.etree 
(src\lxml\lxml.etree.c:225327)
ImportError: cannot import name _elementpath

1 个答案:

答案 0 :(得分:1)

模块lxml不是纯Python。它依赖于用C编写的DLL。您需要在etree.pyd包中添加objectify.pydpy2exe。他们在site-packages\lxml