Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python can't recognize command, but CMD can

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?

like image 344
BruceWayne Avatar asked Mar 30 '26 01:03

BruceWayne


1 Answers

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.

like image 58
JMAA Avatar answered Apr 02 '26 03:04

JMAA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!