virtualenv & subprocess: How to get the correct path to a script

810 Views Asked by At

When someone installs my Python package, they can use the command mycmd (it's a console script added to the python bin/ directory). In turn, mycmd launches several other Python console scripts using subprocess:

subprocess.Process('celery arg1 arg2') subprocess.Process('huey arg1 arg2') ...

(celery and huey commands are installed through my package's pip dependencies.)

This generally works fine, except in the situation where someone invokes mycmd directly without activating its virtualenv. For example, I am trying to use mycmd inside the process control system "circusd". See here where the circus.ini file invokes venv/bin/chaussette directly, without actually activating the venv. If I do this, I get the message celery: No such file or directory, I presume because the virtualenv is not activated and therefore those commands are not found on the path.

How can I ensure that when someone runs mycmd, the correct celery gets run, even if the virtualenv was not activated? (And should also work if the person is not using virtualenv at all, and cross-platform, etc.)

By the way, I am not using subprocess.Process directly, but rather using Honcho, which provides a layer around it.

1

There are 1 best solutions below

0
On

I solved this simply by adding my virtualenv bin path to the PATH used by circus.