如何使用py2app修复“找不到模块”?

2024-04-25 19:17:24 发布

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

我正在尝试将我的python代码转换成一个适用于mac的独立应用程序。我能够成功地运行python setup.py py2app,但得到警告:

Modules not found (conditional imports): * StringIO (docx.compat) * _frozen_importlib_external (importlib._bootstrap) * java (platform) * java.lang (platform)

当我尝试使用它时,我的应用程序崩溃了。你知道吗

下面是我的python代码:

# importing tkinter and tkinter.ttk 
# and all their functions and classes 
from tkinter import * 
from tkinter.ttk import *

# importing askopenfile function 
# from class filedialog 
from tkinter.filedialog import askopenfile 

csv_file_name = ""

root = Tk()
root.withdraw() # Hides the tkinter window
filename = askopenfile(parent=root,mode='r',filetypes=[('CSV file','*.csv')],title='Choose a CSV file')
if filename != None:
    # print(filename.name) 
    csv_file_name = filename.name

if csv_file_name != "":
    import csv
    data = []
    with open(csv_file_name, newline='') as csvfile:
        reader = csv.reader(csvfile, delimiter=',', quotechar='|')
        for row in reader:
            data.append(row)
    # print(data)

    from docx import *

    document = Document()

    table = document.add_table(rows=0, cols=3)
    table.style = 'Table Grid' 
    for person in data:
        time = person[0]
        actor = person[1] + " " + person[2]
        role = person[3]
        agent = person[4]
        row_cells = table.add_row().cells
        row_cells[0].text = time + "\n" + actor + "\n" + role + "\n" + agent + "\n" + "# ________"

    document.add_page_break()

    desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')

    document.save(desktop + '/demo.docx')

我已经试了好几个小时来解决这个问题,但一直没有成功。我只是不明白如何修复丢失的模块问题。你知道吗

编辑:

这是我的设置.py文件:

"""
This is a setup.py script generated by py2applet

Usage:
    python setup.py py2app
"""

from setuptools import setup

APP = ['csv2word.py']
DATA_FILES = []
OPTIONS = {'argv_emulation': True}

setup(
    app=APP,
    data_files=DATA_FILES,
    options={'py2app': OPTIONS},
    setup_requires=['py2app'],
)

Tags: csvnamefrompyimportdatatkintersetup