Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Execute custom command using subprocess that is in PATH variable

I want to execute another program that I compiled earlier from python using the subprocess.call(command) function.

However, python states that it cannot find the command. I suspect that subprocess is not able to find my custom command since it does not know the PATH variable of my Ubuntu system. Is it possible to somehow execute the following code, where command is part of my PATH?

import subprocess
subprocess.run("command -args")

Running this code leads to the error command not found.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can either provide the explicit path to your command:

subprocess.run('/full/path/to/command.sh')

or else modify your PATH variable in your Python code:

import os
os.environ['PATH'] += ':'+'/full/path/to/'
subprocess.run('command.sh')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading