Select to view content in your preferred language

Signing into ArcPro via python to access arcpy module

1634
11
12-22-2023 10:02 AM
GrantSmith122
Occasional Contributor

Hello, I am having an issue with accessing the arcpy module of my ArcPro's default python environment. I am trying to run a standalone python script that will be initiated by a service account via Task Scheduler. Whenever I use the python 3.7.11 environment to run the script, it fails out when trying to access the arcpy module, saying "Error: The Product License has not been initialized."

I can successfully run the script using an old ArcDesktop python instance (2.7.18.4), though that is not a long term solution since there are other modules that I would like to add to the script in order to increase the script functionality (these modules are not compatible with 2.7.18.4).

The only solution I can think of is trying to have the script authenticate my ArcPro account at the outset of the script, thereby 'unlocking' the 3.7.11 version of arcpy and also allowing me to use the additional modules that I would like.

NOTE: I don't need to post data to AGOL or an Enterprise Portal, so I don't think the ArcGIS API for Python is applicable, but I could be mistaken. I just need to sign into Pro in order to access a python 3 environment.

Does anyone know how this could be accomplished?

0 Kudos
11 Replies
VanessaSimps
Frequent Contributor

following this. I recently got into a discussion about this with a coworker and would really like to better understand this workflow/what is and is not needed! 

0 Kudos
Brian_Wilson
Honored Contributor

You could try the sign in to portal thing.

https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/signintoportal.htm

I pulled this from some working code here.. "portal" is returned as a string. You can just set it to whatever the URL is for your portal.

portal = arcpy.GetActivePortalURL()  # I wonder where it finds this.
result = arcpy.SignInToPortal(portal, Config.PORTAL_USER, Config.PORTAL_PASSWORD)

Hope this helps -- Brian

0 Kudos
Brian_Wilson
Honored Contributor

arcpy and arcgis modules work separately but the next lines in my script are a sign in via arcgis because I use both, arcpy for geoprocessing and arcgis to publish and do stuff on the portal.

 

gis = GIS(portal, Config.PORTAL_USER, Config.PORTAL_PASSWORD, verify_cert=False)
print("Connected to ", portal)
groups = gis.groups.search("GIS Team")

 

Maybe this is obvious but I keep my username and password in a separate file; Config.PORTAL_USER and Config.PORTAL_PASSWORD are just strings.

0 Kudos
GrantSmith122
Occasional Contributor

Hey Brian,

Thanks for this. When your script first starts, what modules do you have imported? I tried the Sign In To Portal tool and that seemed to be fine, but then when I tried to import the arcpy module after authenticating with the tool, I got the same error.

Also, the GetActivePortalIURL seems to only work if I'm already signed in. This script is on a shared server space, so ideally I'd be signed out of Pro at the get-go, script signs me in, runs, then I'm signed out again when the script completes.

0 Kudos
Brian_Wilson
Honored Contributor

Change out the first line with GetActivePortalURL for your URL

portal="https://portal.myowndomain.com/portal"

The first lines

import sys
import os
import pprint
import arcpy
from arcgis.gis import GIS
from arcgis.gis import Item as PORTAL_ITEM
from config import Config
0 Kudos
GrantSmith122
Occasional Contributor

Hey Brian, thanks. This is the crux of my issue. If I were to run your script with the modules as outlined, I would fail at line 4, "import arcpy". If I'm not actively signed in to Pro and try to import Pro's version or arcpy into a standalone script, it will fail. I have to find a way to activate my account before trying to access the module.

0 Kudos
Brian_Wilson
Honored Contributor

The wrong copy of python is starting, so it's not seeing the environment that has arcpy installed. It's finding some other copy that is on your system PATH.

I found this, https://www.askpython.com/python/examples/execute-python-windows-task-scheduler

I tried it out on my Server, which is running 10.9.1 (at least through the end of the week)

I wrote this little script and put it in C:\Temp\sysinfo.py on my Server. It will tell you the path to the python that is running, and attempt to load the arcpy module. If it succeeds it will tell you the version. If it's an old version it will still load but complain that __version__ is not there.

 

import sys
info = sys.executable
with open("c:/temp/sysinfo.log", "a") as fp:
    fp.write(info + '\n')
    try:
        import arcpy
        fp.write("arcpy version: %s\n"%arcpy.__version__)
    except Exception as e:
        fp.write(str(e) + '\n')

 

I  found my copy of Python, and set up a task using its location, like this. I set the path for Python, start up in C:\temp, and pass sysinfo.py as an argument.

Brian_Wilson_0-1703617818311.png

I set it to run at 10:36 and then went for coffee. When I got back it had created my sysinfo.log file which contains

 

 

C:\Python27\ArcGISx6410.9\python.exe
'module' object has no attribute '__version__'

 

 

The fact that it has no attribute means it's old. On my desktop it says this,

 

 

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe
arcpy version: 3.2

 

This script is written to run in either Python 2.7 or 3.x. If you are trying to develop code on Pro and run it in Python 2.7 on a server, good luck with that. Does Server 11 come with Python 3? I believe it does. Makes it worth switching.

Remember the step where you set the script to run and then go for coffee. This is important. I recommend Coffee OR Waffle. They will be open only until 1pm today though, business is slow right now. Liv is there waiting for you.

Brian_Wilson_1-1703618506333.png

(Today I learned how to run a Python script from the Task Scheduler on Windows Server. Thanks for asking the question.)

 

 

0 Kudos
Kara_Shindle
Frequent Contributor

Where are you accessing / editing?  If it's an enterprise GDB, do you have an sde connection specified in your script?

For example, when I'm doing database reconciliation for my versions, many of my arcpy require an input database parameter.  I've gotten that message before whenever my script wasn't looking for my DB connection file in the correct location.

Since I run this script on a server, I create a connection file to the specific database & store it on the server, where the scripts run with task scheduler can see it.

 

Edit: Arcpro creates these connection files in the individual APRX project file, whereas I believe ArcMap used to put them in a more centralized location.

 

 

#specify DB connection
sdeConnection = r"C:\Users\[username]\AppData\Roaming\ESRI\Server\[DB connection name].sde"

import arcpy

arcpy.ReconcileVersions_management(sdeConnection, "ALL_VERSIONS", "dbo.DEFAULT", "DBO.ParcelTraining;", "LOCK_ACQUIRED", "NO_ABORT", "BY_OBJECT", "FAVOR_TARGET_VERSION", "NO_POST", "KEEP_VERSION", Reconcile_Log_TEST2)

arcpy.Compress_management(sdeConnection)

 

 

0 Kudos
GrantSmith122
Occasional Contributor

Hey Kara,

Initially I am doing some data formatting in a standard FGDB, but then I eventually post the data to an SDE environment. All of my geoprocessing works fine when I access the python 2.7 environment, but I cannot seem to access the python 3 environment without being signed into ArcGIS Pro at the same time. 

0 Kudos