我是Python新手......,好吧,实际上是一般的编程新手,所以请耐心等待。在Ubuntu 20.04 (是的,我也是Linux新手)上,使用Python 3.8.2。
我试图运行一个使用PyPDF2的脚本。我用Python 3.8.2安装它就可以了。
sudo apt-get install python3-pypdf2
而且我可以从命令行导入它,没有任何错误。
import PyPDF2
但当我尝试从Pycharm导入时,它却产生了一个ModuleNotFoundError错误。
Traceback (most recent call last):
File "/home/surista/.config/JetBrains/PyCharm2020.1/scratches/scratch_2.py", line 1, in <module>
from PyPDF2 import PdfFileReader
ModuleNotFoundError: No module named 'PyPDF2'
这是我使用的脚本.
从PyPDF2导入PdfFileReader。
def get_info(path):
with open(path, 'rb') as f:
pdf = PDFFileReader(f)
info = pdf.getDocumentInfo()
number_of_pages = pdf.getNumPages()
print(info)
author = info.author
creator = info.creator
producer = info.producer
subject = info.subject
title = info.title
if __name__ == '__main__':
path = '/home/surista/Documents/pdfs/test_eng-1.pdf'
get_info(path)
也许这里遗漏了一些明显的东西,但任何帮助都将是感激的。
首先,你应该通过pip安装python包。运行 pip install PyPDF2
,这可能已经解决了问题。
另外检查一下你的项目在pycharm中选择了哪个解释器。如果Pycharm没有使用你的系统python,它就不会看到从普通shell安装的包。
你会发现它在 Settings -> Project: your_project -> Project Interpreter
.