1

I have the DVCS Mercurial on my PATH in a Windows system respective in a GNU/Linux system. If I open a Bash shell in GNU/Linux I can execute hg (the Mercurial command), the same goes if I open a Command Prompt in Windows.

I would like to execute hg and check exit status and capture the output. I know about the subprocess module and check_output. But I can´t get it to use my PATH. I would prefer not to hard code the path as I want the script to work as long as you have Mercurial on your path.

An example would be greatly appreciated.

Happy holidays!

1 Answer 1

3

If you already have hg in your path you shouldn't need anything special in the call to subprocess since you should get the same environment.

Anyway, if you want to explicitly add a PATH environment variable to your subprocess, you can do this:

env = os.environ.copy()
env['PATH'] = <whatever you need here>
subprocess.Popen(<hg command>, env=env)

This code copies the environment you're getting from your shell and overwrites the PATH variable. You might want to append to PATH instead if you're going to use the same env dictionary to run other commands.

There's no need to copy the whole environment, but this will prevent other problems you might have from happening because other environment variables are missing.

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.