title: Loading environment modules on a cluster from my python script
body: <p>I need to submit a python job to a server. While it is running I need to load and unload modules while it runs since it calls several programs, each with different dependencies that conflict i.e. gcc versus intel.</p>
  
  <p>This question has been asked before but the answers have not worked for me in this situation</p>
  
  <p><a href="https://stackoverflow.com/questions/5427040/loading-environment-modules-within-a-python-script">Loading environment modules within a python script</a></p>
  
  <p><a href="https://stackoverflow.com/questions/24044373/loading-environment-modules-within-a-python-script">loading-environment-modules-within-a-python-script</a></p>
  
  <p>I have tried using the following</p>
  
  <pre><code>import subprocess as sub
  cmd = 'module load intel/2016.4'
  p = sub.Popen(cmd, shell=True, stderr = sub.STDOUT, stdout = sub.PIPE).communicate()[0] 
  print(p.decode()) # this simply outputs to screen
  </code></pre>
  
  <p>And, the output says that modules have been switched. </p>
  
  <pre><code>Lmod is automatically replacing "gcc/5.4.0" with "intel/2016.4".
  
  Due to MODULEPATH changes, the following have been reloaded:
    1) openmpi/2.1.1
  </code></pre>
  
  <p>However, when I do 'module list' from the terminal, the modules have not been switched. <code>gcc/5.4.0</code> is still loaded. Also the program requiring <code>intel/2016.4</code> fails to run. For instance later I want to be able to use a version of gromacs that requires <code>intel/2016.4</code> and it fails.</p>
  
  <p>I am a little confused since I thought I was able to use bash commands via  Popen and 'module load' is a bash command. I don't want to have to write a bash script to do this, there is alot of other things in my script done much more conveniently with python than bash.</p>