kramer65
kramer65

Reputation: 53873

Why can I import a Python module from command line, but not from script?

I installed the Python pdfminer module, which comes with pdf2txt.py. I can use pdf2txt.py as follows from the command line:

pdf2txt.py -o converted.html some-pdf.pdf

The pdf2txt.py file is located at:

  $ which pdf2txt.py
/usr/local/bin/pdf2txt.py

I can also import it from the interactive python command line, but to my surprise, I cannot import it in a program which I'm writing:

enter image description here

Does anybody know why this is, and more importantly, how I can solve it? All tips are welcome!

Upvotes: 3

Views: 4190

Answers (2)

pippo
pippo

Reputation: 295

The reason is exactly because you start the interpreter from with the same directory as the script. Your script's name happens to be the same with the library name (pdf2txt). In python's search path for libraries, it will first search its current working directory (same directory where the script lives) and imported your pdf2txt.py, not the library version of pdf2txt.py.

Try rename your script to anything other than pdf2txt.py and it should work.

Upvotes: 3

ErlVolton
ErlVolton

Reputation: 6784

Most likely because you started the interpreter from within the same directory as pdf2txt.py, which put it in your module search path. Is your python code file located in a different directory?

Upvotes: 1

Related Questions