Search code examples
pythonlinuxsubprocesssingularity-container

Is the singularity environment preserved for Python's subprocess?


I am working on a Python program and I wish to use Python's subprocess's run/Popen functions. I am working on a singularity environment and I found the following post which asks somewhat of a similar question but was left unanswered. I am running the Python from a singularity environment, is it preserved for the subprocesses?


Solution

  • Yes, when you run Python's subprocess functions from within a Singularity container, the subprocesses inherit the same container environment by default.

    Here's an example to demonstrate this:

    import subprocess
    import os
    
    # Print the current Python interpreter path (should be inside Singularity)
    print("Parent process Python: ", sys.executable)
    
    # Print some environment variable to confirm we're in Singularity
    print("SINGULARITY_CONTAINER:", os.environ.get("SINGULARITY_CONTAINER", "Not in Singularity"))
    
    # Run a subprocess and check its environment
    result = subprocess.run(["which", "python"], capture_output=True, text=True)
    print("Subprocess found Python at:", result.stdout.strip())
    
    # Run another subprocess to confirm we're still in the same environment
    env_result = subprocess.run(["printenv", "SINGULARITY_CONTAINER"], 
                               capture_output=True, text=True)
    print("Subprocess Singularity env:", env_result.stdout.strip())