How do I call a Python 3.5 script from a Python 2.7 script?

8632
10
02-14-2017 03:27 PM
AndyWright
Occasional Contributor

I've got a situation where I have a data preparation script that I've been using for years that was written in Python 2.7.  We now want to add some functionality to that script that is only available in version 3.5 that comes along for the ride with ArcGIS Pro 1.4.1.  So I am doing something like this inside the 2.7 script:

 p = subprocess.Popen("D:/Program Files/ArcGIS/Pro/bin/Python/envs/arcgispro-py3/python.exe ../ArcGISPro/GenerateVectorTpks.py -rt all -vt electric -oc " + operCo, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)

The 3.5 script runs great on its own when it knows it's using the 3.5 interpreter, but it seems like some wires are getting crossed trying to run stuff across both versions.  I am getting this error:

from variables import File "../Common\\variables.py", line 21, in <module>

   import arcpy  File "d:\\program files (x86)\\arcgis\\desktop10.5\\ArcPy\\arcpy\\__init__.py", line 22, in <module>

   from arcpy.geoprocessing import gp  

   File "d:\\program files (x86)\\arcgis\\desktop10.5\\ArcPy\\arcpy\\geoprocessing\\__init__.py", line 14, in <module>

   from _base import *\ImportError: No module named _base

Can someone point me in the right direction on this?  Thanks a ton ...

0 Kudos
10 Replies
AndyWright
Occasional Contributor

So I've found a workaround to this that I'm not 100% happy with, but it works.  I'm calling out to a batch file that sets up the ArcGIS Pro Python environment prior to calling the ArcGIS Pro based Python script.  Here's what the batch file looks like.  This will do, but there must be a way to do this directly in a 2.7 Python script.  This also must be a pretty common scenario right now considering the Pro functionality is not entirely up to snuff with what's available in Desktop.  If anyone can post a code snippet of how this works I'd really appreciate it.  Thanks ...

SETLOCAL

 

SET PYTHON_EXE_PATH="D:\Program Files\ArcGIS\Pro\bin"

SET CONDA_DEFAULT_ENV=arcgispro-py3

SET CONDA_ENV_PATH=D:\Program Files\ArcGIS\Pro\bin\Python\envs\%CONDA_DEFAULT_ENV%

SET PATH=%ARCHOME%\bin\Python\envs\%CONDA_DEFAULT_ENV%\Library\bin;%PYTHON_EXE_PATH%;%PYTHON_EXE_PATH%\Python;%PATH%

SET PYTHONHOME=%CONDA_ENV_PATH%

SET PYTHONPATH=%~dp0;%PYTHON_EXE_PATH%;%PYTHON_EXE_PATH%\..\Resources\ArcToolbox\Scripts

 

python ../ArcGISPro/GenerateVectorTpks.py -rt %1 -vt %2 -oc %3

 

ENDLOCAL

0 Kudos