Python pdfminer - 导入错误:没有名为 pdfminer.pdfdocument 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35904738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:07:47  来源:igfitidea点击:

pdfminer - ImportError: No module named pdfminer.pdfdocument

pythonpdfminer

提问by KLL

I am trying to install pdfMiner to work with CollectiveAccess. My host (pair.com) has given me the following information to help in this quest:

我正在尝试安装 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.

编译时,可能需要指示
安装使用上面的帐户空间,而不是尝试安装
到操作系统目录中。通常,
在安装命令的末尾使用“-- home=/usr/home/username/pdfminer”应该允许这样做。

I followed this instruction when trying to install. The result was:

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

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

I don't see anything wrong with that (I'm very new to python), but when I try to run the sample command $ pdf2txt.py samples/simple1.pdfI get this error:

我没有发现任何问题(我对 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

I'm running python 2.7.3. I can't install from root (shared hosting). The most recent version of pdfminer, which is 2014/03/28. I've seen some posts on similar issues ("no module named. . . " but nothing exactly the same. The proposed solutions either don't help (such as installing with sudo - not an option; specifying the path for python (which doesn't seem to be the issue), etc.).

我正在运行 python 2.7.3。我无法从 root 安装(共享主机)。pdfminer 的最新版本,即 2014/03/28。我看过一些关于类似问题的帖子(“没有命名的模块......”,但没有完全相同。建议的解决方案要么没有帮助(例如使用 sudo 安装 - 不是一个选项;指定 python 的路径(似乎不是问题)等)。

Or is this a question for my host? (i.e., something amiss or different about their setup)

或者这是我的主人的问题?(即,他们的设置有问题或不同)

采纳答案by aneroid

Since the package pdfmineris installed to a non-standard/non-default location, Python won't be be able to find it. In order to use it, you will need to add it to your 'pythonpath'. Three ways:

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

  1. At run time, put this in your script pdf2txt.py:

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

    or

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

    Note:The install path specified with --homeis used as the Lib for all packages which you might want to install, not just this one. You should delete that folder and re-install with -- home=/usr/home/username/myPyLibs(or any generic name) so that when you install other packages with that install path, you would only need the one path to add to your local Lib to be able to import them:

    import sys
    sys.path.insert(1, "/usr/home/username/myPyLibs")
    
  2. Add it to PYTHONPATH before executing your script:

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

    And then put that in your ~/.bashrcfile (/usr/home/username/.bashrc) or .profileas applicable. This may not work for programs which are not executed from the console.

  3. Create a VirtualEnv and install the packagesyou need to that.

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

    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 并安装所需的软件包

回答by Farzin Faridfar

I had an error like this:

我有这样的错误:

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

My problem was that I had named my script pdfminer.pywhich for the reasons that I don't know, Python took it for the original pdfminerpackage files and tried to compiled it.

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

I renamedmy script to something else, deleted all the *.pycfile and __pycache__directory and my problem was solved.

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

回答by Cory Brickner

I have a virtual environment and I had to activate it before I did a pip3 install to have the venv see it.

我有一个虚拟环境,我必须在进行 pip3 安装之前激活它才能让 venv 看到它。

source ~/venv/bin/activate