0

On my Windows 7 laptop I have both Python 2 and Python 3 installed, and I've been switching back and forth between them like py -2 myscript.py etc.

I explicitly installed a package called pdfminer by using pip2 and I've verified that it's on my disk at C:\Python27\Lib\site-packages\pdfminer

However, when I try to run a script I get an error:

> py -2 pdfminer.py
Traceback (most recent call last):
  File "pdfminer.py", line 4, in <module>
    from pdfminer.pdfparser import PDFParser
  File "C:\Users\me\Documents\myprog\pdfminer.py", line 4, in <module>
    from pdfminer.pdfparser import PDFParser
ImportError: No module named pdfparser

The import statement is exactly as given in the PDFMiner documentation, and I even added the Python 2 scripts directory explicitly to the PATH just in case that would help, but it didn't (I actually think that's redundant because from printing out sys.path it seems it was already there):

import sys
sys.path.append("C:\Python27\Lib\site-packages\\")

from pdfminer.pdfparser import PDFParser

Within site-packages\pdfminer there is a file pdfparser.py and within it class PDFParser(PSStackParser). The case doesn't match the import statement, but actually making the import statement lowercase to match the file also doesn't help.

I don't really see anything wrong here. Is there anything else I could try?

Stephen
  • 8,508
  • 12
  • 56
  • 96

2 Answers2

2

You've called your own script pdfminer.py which is now shadowing the module you're trying to import. Rename your script.

You can see this in the traceback:

File "C:\Users\me\Documents\myprog\pdfminer.py", line 4, in <module>

That isn't the path that you installed the package on, but we can see that there is a file called pdfminer.

roganjosh
  • 12,594
  • 4
  • 29
  • 46
  • 2
    Oh man, that was it. I also had a directory named `pdfminer`, not sure which of the two was the cause but once I fixed both it started working. THANK YOU!!!!! – Stephen May 09 '18 at 19:58
0

Have you tried specifying which Python installation to use at the start of your programme, providing a link to the installation.

e.g.

#!/usr/bin/python
Watty62
  • 602
  • 1
  • 5
  • 21
  • 1
    I am running Python with `py -2 myscript.py`. It is getting the right version of Python, I verified this by printing `sys.version` – Stephen May 09 '18 at 19:52