Search code examples
pythonpython-2.7python-3.xsubprocessarcpy

Call a Python 2.7 module in Python 3.2


I have a GUI program that I have developed in Python 3.2 to extract various geospatial data products. I need to call a module I have developed in Python 2.7.

I am looking for a way to be able to call the Python 2.7 code using a Python 2.7 interpreter inside of Python 3.2 program. I cannot port the 2.7 to Python 3.2 as it uses a Python version installed with ESRI ArcMap and relies on the arcpy module which is not available for Python 3. My only idea right now would be to use subprocess to call the module as a batch process however this is a bit messy and I would prefer the two programs had some relationship.

Thank you in advance.


Solution

  • Try using subprocess.check_output(['C:\\python27\\python.exe', 'yourModule.py'])

    If you want to call a specific function within the 27 file, you can use more system arguments. The call would look like:

    subprocess.check_output(['C:\\python27\\python.exe', 'yourModule.py', 'funcName'])
    

    And in the 27 file you can add:

    import sys
    if __name__=='__main__':
        if 'funcName' in sys.argv:
            funcName()
         else:
            #... execute normally