3

I have installed qpdf and am trying to call it via Python.

I have added the path to my system Environment Variables and can successfully run the following command via the command prompt:

qpdf --decrypt input.pdf output.pdf

This runs, no issues.

However, when trying to call via Python (code from here), I get

'qpdf' is not recognized as an internal or external command, operable program or batch file.

import subprocess
subprocess.call(["cmd", "/c", "qpdf --decrypt input.pdf output.pdf"], shell=True)
# or 
subprocess.run(["qpdf", "--decrypt", "input.pdf", "output.pdf"], shell=True)
# or
subprocess.run(["qpdf --decrypt input.pdf output.pdf"], shell=True)

Why can I run this via cmd, but not in Python?

7
  • ensure that you have your python program in the same directory in from which the CMD works . Commented Jul 11, 2018 at 17:04
  • 1
    It may be that you're using a shell session from before you added qpdf to PATH. Try running the python from a new session. Commented Jul 11, 2018 at 17:07
  • According to the docs "On Windows with shell=True, the COMSPEC environment variable specifies the default shell. The only time you need to specify shell=True on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy). You do not need shell=True to run a batch file or console-based executable." Commented Jul 11, 2018 at 17:13
  • @JMAA - Aha! I had SublimeText open before, during, and after installation of qpdf. I exited out and re-opened and it's not throwing the error anymore. That was it, a simple fix thankfully :D Commented Jul 11, 2018 at 17:16
  • (@JMAA - If you want to turn that in to an answer, go for it and I can the question as Answered) Commented Jul 11, 2018 at 21:09

1 Answer 1

1

As per the comments:

The problem is that environment variables are only loaded when you launch your executable/shell session/whatever. Here, a directory was added to the PATH environment variable, so sublime text needed to be restarted before it could see the updated PATH. This isn't specific to sublime, the same would be true if you were running directly from a terminal or another IDE.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.