【问题标题】:Python3 : module 'tabula' has no attribute 'read_pdf'Python3:模块'tabula'没有属性'read_pdf'
【发布时间】:2020-02-24 13:36:22
【问题描述】:

.py 程序可以运行,但完全相同的代码在作为 API 公开时不起作用。

代码使用 Tabula 读取 pdf 并提供表格内容作为输出。

我试过了:

import tabula
df = tabula.read_pdf("my_pdf")
print(df)

from tabula import wrapper
df = wrapper.read_pdf("my_pdf")
print(df)

我已经在运行 Ubuntu 的 AWS EC2 上安装了 tabula-py(不是 tabula)。

除了 read_pdf,我实际上还想转换为 CSV 并给出输出。但这也行不通。我得到相同的无属性错误,即module 'tabula' has no attribute 'convert_into

.py 文件和 API 文件(.py 也一样)在同一个目录下,由同一个用户访问。

任何帮助将不胜感激。

编辑:我尝试从 API 运行相同的 python 文件作为 OS 命令 (os.system("python3 /home/ubuntu/flaskapp/tabler.py"))。但它也没有奏效。

【问题讨论】:

  • 你的pip freeze 显示了什么?
  • 详情在这里 - pastebin.com/yGBgr5jM。注意,同一个 API 文件有更多的功能要公开。因此,您会发现比 tabula 更多的 pip 组件。
  • 我尝试从 API 运行相同的 python 文件作为 OS 命令 (os.system("python3 /home/ubuntu/flaskapp/tabler.py"))。但效果并不好。
  • 您有没有将您的脚本之一命名为tabula.py,是否有机会? import 可能会优先选择已安装的模块。或执行import tabula; print(dir(tabula)) 以查看它正在定义的确切名称。
  • 没有。我的文件都没有被命名为“tabula”。

标签: python tabula tabula-py


【解决方案1】:

确保您安装了 tabula-py 而不仅仅是 tabula 使用

!pip install tabula-py

并导入它使用

from tabula.io import read_pdf

【讨论】:

  • 这应该被标记为正确答案
【解决方案2】:

其实有an entry in the FAQ专门针对这个问题:

如果你已经安装了tabula,将会与命名空间冲突。你应该在删除tabula之后安装tabula-py

尽管使用tabula.io 中的read_csv() 有效,正如其他答案所建议的那样,在删除tabula 并重新安装tabula-py(使用pip install --force-reinstall tabula-py)后,我也能够使用tabula.read_csv()

【讨论】:

    【解决方案3】:

    tabula 包有些问题。我看了看里面没有__init__.py。你可以这样做:

    from tabula.io import read_pdf
    

    它对我有用。

    【讨论】:

      【解决方案4】:

      如果你在安装 tabula-py 之前不小心安装了 tabula,它们会在命名空间中发生冲突(即使在卸载 tabula 之后)。

      卸载 tabula-py 并重新安装。这对我有用。

      【讨论】:

        【解决方案5】:

        from tabula import read_pdf 对我不起作用。我已将 tabula.read_pdf() 替换为 tabula.io.read_pdf() 以使其正常工作。

        【讨论】:

          【解决方案6】:

          它是这样工作的:

          import tabula # just this here!
          
          #declare the path of your file
          file_path = "/path/to/pdf_file/data.pdf"
          
          #Convert your file
          df = tabula.io.read_pdf(file_path)
          

          泰国就是一切!

          【讨论】:

            【解决方案7】:

            试试

            from tabula import read_pdf

            我遇到了同样的问题,这解决了它。

            【讨论】:

              猜你喜欢
              • 2023-02-16
              • 2020-02-11
              • 2021-12-10
              • 2021-06-20
              • 2018-11-26
              • 1970-01-01
              • 1970-01-01
              • 2022-01-03
              • 2018-06-05
              相关资源
              最近更新 更多