pdfminer - ImportError: No module named pdfminer.pdfdocumen

2024-04-18 08:15:48 发布

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

我正在尝试安装pdfMiner以使用CollectiveAccess。我的主机(pair.com)提供了以下信息来帮助我完成此任务:

When compiling, it will likely be necessary to instruct the
installation to use your account space above, and not try to install
into the operating system directories. Typically, using "--
home=/usr/home/username/pdfminer" at the end of the install command should allow for that.

尝试安装时,我遵循了此说明。 结果是:

running install
running build
running build_py
running build_scripts
running install_lib
running install_scripts
changing mode of /usr/home/username/pdfminer/bin/latin2ascii.py to 755
changing mode of /usr/home/username/pdfminer/bin/pdf2txt.py to 755
changing mode of /usr/home/username/pdfminer/bin/dumppdf.py to 755
running install_egg_info
Removing /usr/home/username/pdfminer/lib/python/pdfminer-20140328.egg-info
Writing /usr/home/username/pdfminer/lib/python/pdfminer-20140328.egg-info

我看不出有什么问题(我对python很陌生),但是当我尝试运行示例命令$ pdf2txt.py samples/simple1.pdf时,会得到以下错误:

Traceback (most recent call last):   File "pdf2txt.py", line 3, in <module>
    from pdfminer.pdfdocument import PDFDocument ImportError: No module named pdfminer.pdfdocument

我在运行Python2.7.3。我无法从根目录(共享主机)安装。pdfminer的最新版本,2014年3月28日。 我看过一些关于类似问题的帖子(“没有命名的模块…”但完全不一样。建议的解决方案要么没有帮助(例如使用sudo安装-不是一个选项;指定python的路径(这似乎不是问题所在)等等)。

还是这是我主人的问题?(也就是说,他们的设置有些不对劲或不一样)


Tags: installofthetopybuildhomebin
2条回答

由于包pdfminer安装在非标准/非默认位置,Python将无法找到它。为了使用它,你需要将它添加到你的“pythonpath”中。三种方式:

  1. 在运行时,将其放入脚本pdf2txt.py

    import sys
    # if there are no conflicting packages in the default Python Libs =>
    sys.path.append("/usr/home/username/pdfminer")
    

    或者

    import sys
    # to always use your package lib before the system's =>
    sys.path.insert(1, "/usr/home/username/pdfminer")
    

    注意:--home指定的安装路径用作可能要安装的所有软件包的库,而不仅仅是这个。您应该删除该文件夹并使用-- home=/usr/home/username/myPyLibs(或任何通用名称)重新安装,以便在使用该安装路径安装其他软件包时,您只需要将一个路径添加到本地库即可导入它们:

    import sys
    sys.path.insert(1, "/usr/home/username/myPyLibs")
    
  2. 在执行脚本之前将其添加到PYTHONPATH:

    export PYTHONPATH="${PYTHONPATH}:/usr/home/username/myPyLibs"
    

    然后将其放入~/.bashrc文件(/usr/home/username/.bashrc)或.profile中(如果适用)。对于不是从控制台执行的程序,这可能不起作用。

  3. 创建一个你需要的VirtualEnv and install the packages

我犯了这样的错误:

No module named 'pdfminer.pdfinterp'; 'pdfminer' is not a package

我的问题是我命名了我的脚本pdfminer.py,由于我不知道的原因,Python将它作为原始的pdfminer包文件并试图编译它。

我把我的脚本重命名为其他东西,删除了所有的*.pyc文件和__pycache__目录,我的问题就解决了。

相关问题 更多 >