Is there a way to force a stand-alone python script to use 3.4 instead of 2.7?

1626
9
10-27-2017 11:52 AM
KateNewell1
Occasional Contributor

I have a script that needs to access an ArcPro project to update a vector tile package. 

I'm not quite ready to make 3.4 my default python program as we have TONS of scripts still using 2.7.

Is there a way to script/force using 3.4 for a single stand-along python script?  

0 Kudos
9 Replies
DanPatterson_Retired
MVP Esteemed Contributor

There is a python 2 to 3 translator-ish for a one-off, just examine your python 2 code for suggested changes... I bet, it will be mostly print statements

0 Kudos
KateNewell1
Occasional Contributor

Dan, the code is written for python 3 (basically copy/pasted from ArcPro help). It fails when I run it because it doesn't understand the module "mp".  I don't want to change my default program to 3, as all my other scripts will fail and I'm not ready to "modernize" them

Below is the code. I'm hoping there is a simple way to tell this script only to use Python 3.4?

# Name: CreateVectorTilePackage.py
# Description: Find all the maps in the project and
#   create a vector tile package for each map
# import system modules
import os
import arcpy

#set environment settings
arcpy.env.overwriteOutput = True

VTPK_Path = "N://ArcShared//Planning//HousingStoryMap"
# Loop through the project, find all the maps, and
#   create a vector tile package for each map,
#   using the same name as the map
p = arcpy.mp.ArcGISProject(r"N:\ArcShared\Planning\HousingStoryMap\CreateVectorTilePackages.aprx")
for m in p.listMaps():
    print("Packaging " + m.name)
    arcpy.CreateVectorTilePackage_management(m, VTPK_Path + m.name + '.vtpk', "ONLINE", "", "INDEXED", 100000, 500, "N:\\ArcShared\\Planning\\HousingStoryMap\\SFBedroomIndex.shp")

The error message I get:

Traceback (most recent call last):
File "N:\Users\ke\ArcPyMapping\UpdateVectorTilePackages.py", line 15, in <module>
p = arcpy.mp.ArcGISProject(r"N:\ArcShared\Planning\HousingStoryMap\CreateVectorTilePackages.aprx")
AttributeError: 'module' object has no attribute 'mp'

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Ok you have bigger issues since it is looking for a *.aprx which is an ArcGIS Pro project not an ArcMap project, so that won't even load.  You have to run this against pro and you have to run your python within the Anaconda version of python, probably the easiest is through the python window in Pro.  

You won't have to worry about anything else if you run it within pro.

as for 'mp' ... that is formerly 'mapping' in ArcMap, they changed the mapping module

0 Kudos
Luke_Pinner
MVP Regular Contributor

ArcGIS Pro uses the Anaconda Python distribution. You can run your script standalone by activating the arcgispro-py3 environment and calling python or directly by calling propy.bat. It's explained in the help http://pro.arcgis.com/en/pro-app/arcpy/get-started/using-conda-with-arcgis-pro.htm

KateNewell1
Occasional Contributor

Thank you Luke! What I am struggling with (as a novice python users) is the actual code to place into my script to call Anaconda Python....I'm guessing it goes under the import os?

# Name: CreateVectorTilePackage.py
# Description: Find all the maps in the project and
#   create a vector tile package for each map
# import system modules
import os
import arcpy

#is there code that goes here to specifically call Anaconda python so that it doesn't use 2.7?

#set environment settings
arcpy.env.overwriteOutput = True

VTPK_Path = "N://ArcShared//Planning//HousingStoryMap"
# Loop through the project, find all the maps, and
#   create a vector tile package for each map,
#   using the same name as the map
p = arcpy.mp.ArcGISProject(r"N:\ArcShared\Planning\HousingStoryMap\CreateVectorTilePackages.aprx")
for m in p.listMaps():
    print("Packaging " + m.name)
    arcpy.CreateVectorTilePackage_management(m, VTPK_Path + m.name + '.vtpk', "ONLINE", "", "INDEXED", 100000, 500, "N:\\ArcShared\\Planning\\HousingStoryMap\\SFBedroomIndex.shp")
   
0 Kudos
Luke_Pinner
MVP Regular Contributor

Kate, you don't specify which python to use *in* your script. You specify which python to use when you *run* your script.  i.e by activating the arcgispro-py3 environment and calling python path\to\your\script.py or directly by calling propy.bat path\to\your\script.py

RebeccaStrauch__GISP
MVP Esteemed Contributor

Although not directly related to your question, I have been working on making some of my custom tools compatible in both ArcCatalog (not Map, that is, no map doc involved) and Pro. In ArcCatalog, it is an addin, but Pro does not allow python addins at lease yet so I will be using tools direct.

I have some custom modules that I load into each tool.  I found that Python 3.5 has to load them in a different manner than 2.7.  Although this is a work in progress (i.e. that is, my tool), and this has some path dependencies to be run from a script (not interactive window), I thought I would share this here.  It does seem to help me access which version of python I am using, and then go from there.  fwiw.

# Import system modules
import time, os
import arcpy

def myMsgs(message):
     arcpy.AddMessage(message)
     print(message)

# function to see if the masterGDB path is UNC or "local"
def isuncpath(a_path): 
     return os.path.splitunc(a_path)[0] != ''

# Set the necessary product code
arcpy.SetProduct("ArcInfo")

pathname, scriptname = (os.path.dirname(sys.argv[0]), os.path.basename(sys.argv[0]))     
# Check for path to current script...
myMsgs("\n{0}\n  {1}   \n{0}\n".format(("*" * 38), scriptname))

myMsgs("path = {0}".format(pathname))
myMsgs("scriptname = {0}".format(scriptname))
#myMsgs("full path  = {0}".format(os.path.abspath(pathname)))

scriptPath = r"\\<server>\<share>\Users\<user>\_MyPyAddins\dwcUpdateMasterFGDB_Pro\Install\scripts"
scriptPathforVer= (scriptPath.replace("\\", "/"))

myMsgs(scriptPathforVer)
myMsgs("Do both paths exist?\n    scriptPath {0}\n  scriptPathforVer {1}".format(arcpy.Exists(scriptPath), arcpy.Exists(scriptPathforVer)))

arcpy.env.scriptWorkspace = scriptPathforVer

sys.path.append(scriptPathforVer)
sys.path

pyVer = (sys.version[0:3])
myMsgs("the python version is: {0} ".format(pyVer))

if float(pyVer) == 3.5:
     myMsgs("Using Pro, python version {0}".format(pyVer))
     setVer = "3.5"
elif float(pyVer) < 3.0:
     myMsgs("Using Desktop, python version {0}".format(pyVer))
     setVer = "2.x"
else:
     myMsgs("Software unknown, but using python version {0}".format(pyVer))
     setVer = "other"

if setVer == "3.5":
     import importlib.util
     adfgUtilsPath = r"\\<server>\<share>\Users\<user>\_MyPyAddins\dwcUpdateMasterFGDB_Pro\Install\scripts\myUtils.py"
     arcpy.Exists(adfgUtilsPath)

     mymodList = ["myUtils", "gpdecorators", "StatusCheck", "sdeconnUtil"]
     for mymod in mymodList:
          try:
               spec = importlib.util.spec_from_file_location(mymod, adfgUtilsPath)
               module = importlib.util.module_from_spec(spec)
               spec.loader.exec_module(module)
               myMsgs("Successfully loaded {0}".format(mymod))
          except:
               myMsgs("-> Unable to load {0}".format(mymod))
elif setVer == "2.x":
     from myUtils import *
     from gpdecorators import *
     from StatusCheck import *
     from sdeconnUtil import *

scriptPath = sys.path[0]
arcpy.AddMessage("the scriptPath is: {0} ".format(scriptPath))
print("the scriptPath is: {0} ".format(scriptPath))
DanPatterson_Retired
MVP Esteemed Contributor

Kate... just create shortcuts on your desktop if you aren't sure what 'activate your conda' environment means

arcgis-pro-2-creating-desktop-shortcuts

Luke_Pinner
MVP Regular Contributor

The simplest way to activate your ArcGIS Pro conda environment is to start the ArcGIS Pro Python Command Prompt.  You can then just type python path\to\your\script.py to run your script.

Start Menu > ArcGIS > ArcGIS Pro > Pytthon Command Prompt

Start Menu