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?

BruceWayne
  • 22,923
  • 15
  • 65
  • 110
  • ensure that you have your python program in the same directory in from which the CMD works . – MD5 Jul 11 '18 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. – JMAA Jul 11 '18 at 17:07
  • According to [the docs](https://docs.python.org/3/library/subprocess.html) "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." – Iguananaut Jul 11 '18 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 – BruceWayne Jul 11 '18 at 17:16
  • (@JMAA - If you want to turn that in to an answer, go for it and I can the question as Answered) – BruceWayne Jul 11 '18 at 21:09
  • @Iguananaut - Ah, thanks for clarifying that! – BruceWayne Jul 11 '18 at 21:10
  • Duplicate of [What is the reason for '...' is not recognized as an internal or external command, operable program or batch file?](https://stackoverflow.com/questions/41454769/what-is-the-reason-for-is-not-recognized-as-an-internal-or-external-comman) – Mofi Jul 12 '18 at 05:44

1 Answers1

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.

JMAA
  • 1,730
  • 13
  • 24