Forums

"unable to load configuration" error when using subprocess.Popen()

I am struggling with deployment because of problems with Python subprocesses I experience on PA but not my local machine.

I'm using flask. To isolate the issue I created a route in app/routes.py:

def test_subprocess_function():
    try:
        test_command = "print(2 + 3)"



        import subprocess

        process = subprocess.Popen(
            [sys.executable, '-c', test_command],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True
        )
        stdout, stderr = process.communicate()

        # Check for errors in stderr
        if process.returncode != 0:
            return f"Subprocess error: {stderr.strip()}"

        return f"Subprocess output: {stdout.strip()}"

    except Exception as e:
        return f"Error: {str(e)}"

The result of navigating to test-subprocess is this error: unable to load configuration from print(2 + 3)

What does this error message mean? From what I understand, I should be able to use subprocesses on PA.

sys.executable is the uwsgi executable when you're running in a web app, it's not Python. Run Python directly without using sys.executable when running from a web app.

Thanks a lot for responding. How would I run Python directly without using sys.executable when running from a web app?

You can provide a full path to the Python executable, if you want to run it like this instead executing it directly.