【问题标题】:Pyjasper shows error (NameError: Invalid resource directory!)Pyjasper 显示错误(NameError: Invalid resource directory!)
【发布时间】:2019-08-26 09:37:59
【问题描述】:

我正在尝试通过将 python 与 pyreport 库一起使用来生成 .jrxml,但是当我使用使用 Pyinstaller 从 .py 转换的 .exe 时,它​​会显示错误。

Pyinstaller 版本是 3.5,pyJasper 版本是 0.41,pyreportJasper 版本是 1.0.2,Python 版本是 3.7.1。

当我使用 Pycharm 运行 .py 文件时,它能够生成文件(pdf、xlms),并带有警告“正在加载类 `com.mysql.jdbc.Driver'。这已被弃用。新的驱动程序类是 `com. mysql.cj.jdbc.Driver'。驱动是通过SPI自动注册的,一般不需要手动加载驱动类。"。根据此链接:"Loading class com.mysql.jdbc.Driver ... is deprecated" message,我认为这不是原因,但仅供参考。

fileName 示例为“115kV Line No.1_2019-08-25”。

这是代码。

def createFolder(output_file, folderName):
    print("createFolder function started...")
    try:
        os.mkdir(output_file + folderName)
    except OSError:
        print("Creation of the directory %s failed." % output_file)
    else:
        print("Successfully created the directory %s." % output_file)


def jasperReport(input_file, output_file, reportParameters, folderName, fileName, fileFormat):
    print("JasperReport function started...")
    con = {
        'driver': 'mysql',
        'username': 'root',
        'password': 'root',
        'host': 'localhost',
        'database': 'cscs_prj',
        'port': '3306'
    }
    createFolder(output_file, folderName)
    output = output_file + folderName + "/" + fileName
    jasper = pyreportjasper.JasperPy()
    jasper.process(
        input_file,
        output_file=output,
        format_list=fileFormat,
        parameters=reportParameters,
        db_connection=con,
        locale='en_US'  # LOCALE Ex.:(en_US, de_GE)
    )

但是,当我执行 .exe 时,它​​在下面显示错误。

  File "JasperReport.py", line 37, in jasperReport
  File "site-packages\pyreportjasper\jasperpy.py", line 151, in process
  File "site-packages\pyreportjasper\jasperpy.py", line 198, in execute
NameError: Invalid resource directory!
[5288] Failed to execute script Executer

你们知道错误发生的原因吗?

【问题讨论】:

    标签: python python-3.x pyinstaller pyreport


    【解决方案1】:

    我发现问题来自 pyinstaller 不包括 .jar 和 jasperstarter.exe。我通过在 db_connection 中定义带有 jdbc_dir 的 .jar 目录和带有 jasper.path_executable 的 jasperstarter.exe 目录来解决这个问题。

    这是下面的示例。

    def jasperReport(input_file, output_file, reportParameters, folderName, fileName, fileFormat, mySQLConfig, jasperConfig):
        print("JasperReport function started...")
        con = {
            'driver': 'mysql',
            'username': 'root',
            'password': 'root',
            'host': 'localhost',
            'database': 'cscs_prj',
            'jdbc_dir': 'C:/SCPS_PRJ/JasperStarter/jdbc/',
            'port': '3306'
    
        }
        createFolder(output_file, folderName)
        output = output_file + folderName + "/" + fileName
        jasper = pyreportjasper.JasperPy()
        jasper.path_executable = "C:/SCPS_PRJ/JasperStarter/bin/"
        jasper.process(
            input_file,
            output_file=output,
            format_list=fileFormat,
            parameters=reportParameters,
            db_connection=con,
            locale='en_US'  # LOCALE Ex.:(en_US, de_GE)
        )
    

    为了使这个例子能够正常运行,请不要忘记将 .jar 放在“C:/SCPS_PRJ/JasperStarter/jdbc/”文件夹中,并将 jasperstarter.exe 放在“C:/SCPS_PRJ/JasperStarter/bin/”文件夹中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      • 2017-11-03
      • 2023-01-04
      • 2020-11-09
      相关资源
      最近更新 更多