Skip to content Skip to sidebar Skip to footer

Starting Module Shell Command From Python Subprocess Module

I'm trying to run vnc server, but in order to do it first I need to run 'module load vnc'. If I call which module in loaded bash shell then the command in not found is the PATH but

Solution 1:

Environment Modules usually just modifies a couple environment variables for you. It's usually possible to skip the module load whatever step altogether and just not depend on those modules. I recommend

subprocess.Popen(['/possibly/path/to/vncserver', ':8080', '-localhost'], 
                 env={'WHATEVER': 'you', 'MAY': 'need'})

instead of loading the module at all.

If you do insist on using this basic method, then you want to start bash yourself with Popen(['bash',....

Solution 2:

If you want to do it with shell=False, just split this into two Popen calls.

subprocess.check_call('module load vnc'.split())
subprocess.Popen('vncserver :8080 -localhost'.split())

Solution 3:

You can call module from a Python script. The module command is provided by the environment-modules software, which also provides a python.py initialization script.

Evaluating this script in a Python script enables the module python function. If environment-modules is installed in /usr/share/Modules, you can find this script at /usr/share/Modules/init/python.py.

Following code enables module python function:

import os
exec(open('/usr/share/Modules/init/python.py').read())

Thereafter you can load your module and start your application:

module('load', 'vnc')
subprocess.Popen(['vncserver', ':8080', '-localhost'])

Post a Comment for "Starting Module Shell Command From Python Subprocess Module"