Search code examples
pythoncpython

Is there a way to determine if my code is run in an embedded Python?


So we're developing a C++ Python application that is both used in native Python and also in a embedded Python version that is wrapped in R using reticulate. Now we want to determine whether the currently running Python is in embedded mode or not.

Does anyone know what I could do here? I have not found any documentation about different env setups or internal hints that I could use to find this information within Python. We would have to determine on the Python side and not the C++ side...

I appreciate any hint I could get!

Edit about environments: The main problem is that we use an embedded Python version with our R application and a native Python version with our main application. So our clients can access the same code snippet from basically any Python version that is included in the PY_LIMITED_API. So if I recall it correctly thats any version 3.2+


Solution

  • I haven't answered my question until now.

    My workaround was a simple

    try:
        import pip
    except ImportError:
        PythonIsRunEmbedded = True
    

    Since pip is not included with the embedded version of Python