Trouble accessing ArcPy in script outside of ArcGIS Pro

336
6
11-25-2020 06:35 PM
by
New Contributor III

I'm having no luck calling arcpy from a script that uses ArcGIS API for Python as well.

I've added every single path I can think of to the PYTHONPATH and path environment variables.  No luck.  Runs fine with ArcPy and imports arcgis fine as well when run in a Script tool in Pro, or in a Notebook from Pro. 

Doesn't run in a Notebook outside of Pro, or called in a bat file where the exe used is here: D:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe 

Here's the err (I've also seen no _base.py err):

E:\START\CalFire2020_New\PythonScripts\AGOL_Management>"D:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe" E:\START\CalFire2020_New\PythonScripts\AGOL_Management\GroupExport.py
Traceback (most recent call last):
  File "E:\AGOL_Management\GroupExport.py",
line 12, in <module>
    import arcpy
  File "D:\Program Files (x86)\ArcGIS\Desktop10.6\arcpy\arcpy\__init__.py", line
 22, in <module>
    from arcpy.geoprocessing import gp
ImportError: cannot import name 'gp'

Reply
0 Kudos
6 Replies
Esri Contributor

Hi @sofoo ,

 

It could possibly be the python environment variable is referencing to a different version.

 

You can try the following in a bat file

 

"c:\Program Files\ArcGIS\Pro\bin\Python\scripts\propy.bat" "D:\scriptfile.py"

Cheers,
Manish
Reply
0 Kudos
by
New Contributor III

Hi Manish,

What does propy.bat do?

Thanks!

Reply
0 Kudos
Esri Contributor

Hi @sofoo ,

propy.bat acts as a replacement for calling python.exe

 

This link will provide you some background info:

https://pro.arcgis.com/en/pro-app/arcpy/get-started/using-conda-with-arcgis-pro.htm

 

Cheers,
Manish
Reply
0 Kudos
MVP Frequent Contributor

What do you get when you run these from sys?  This is my default path structure minus personal ones.

# ---- NOTE : replace C:\\arc_pro with your installation path
sys.executable
'C:\\arc_pro\\bin\\Python\\envs\\arcgispro-py3\\python.exe'

sys.path
['C:\\arc_pro\\Resources\\ArcPy',
 ...snip...,
 'C:\\arc_pro\\bin\\Python\\envs\\arcgispro-py3\\DLLs',
 'C:\\arc_pro\\bin\\Python\\envs\\arcgispro-py3\\lib',
 'C:\\arc_pro\\bin\\Python\\envs\\arcgispro-py3',
 '',
 'C:\\arc_pro\\bin\\Python\\envs\\arcgispro-py3\\lib\\site-packages',
 'C:\\arc_pro\\bin',
 'C:\\arc_pro\\Resources\\ArcToolbox\\Scripts',
 'C:\\arc_pro\\bin\\Python\\envs\\arcgispro-py3\\lib\\site-packages\\future-0.18.2-py3.7.egg',
'C:\\arc_pro\\bin\\Python\\envs\\arcgispro-py3\\lib\\site-packages\\pytz-2020.1-py3.7.egg',
 'C:\\arc_pro\\bin\\Python\\envs\\arcgispro-py3\\lib\\site-packages\\pywin32_ctypes-0.2.0-py3.7.egg',
 'C:\\arc_pro\\bin\\Python\\envs\\arcgispro-py3\\lib\\site-packages\\pywin32security',
 'C:\\arc_pro\\bin\\Python\\envs\\arcgispro-py3\\lib\\site-packages\\sympy-1.5.1-py3.7.egg',
 'C:\\arc_pro\\bin\\Python\\envs\\arcgispro-py3\\lib\\site-packages\\IPython\\extensions',
]

 


... sort of retired...
Reply
0 Kudos
by
New Contributor III

Hi Dan!  

From the cmd prompt:

>>> sys.executable
'D:\\Python27\\ArcGISx6410.6\\python.exe'

>>> sys.path
['', 'C:\\Program Files\\ArcGIS\\Pro\\bin\\Python', 'D:\\Python27\\ArcGISx6410.6
', 'D:\\Python27\\ArcGISx6410.6\\Scripts', 'D:\\Program Files (x86)\\ArcGIS\\Des
ktop10.6\\arcpy', 'D:\\Program Files (x86)\\ArcGIS\\Desktop10.6\\bin', 'D:\\Prog
ram Files\\ArcGIS\\Pro\\bin', 'D:\\Program Files\\ArcGIS\\Pro\\Resources\\ArcPy\
\arcpy', 'D:\\Program Files\\ArcGIS\\Pro\\Resources\\ArcPy', 'D:\\Program Files\
\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3', 'C:\\Program Files\\ArcGIS\\Pro
\\bin\\Python\\envs\\arcgispro-py3\\Lib', 'C:\\Windows\\SYSTEM32\\python27.zip',
'D:\\Python27\\ArcGISx6410.6\\DLLs', 'D:\\Python27\\ArcGISx6410.6\\lib', 'D:\\P
ython27\\ArcGISx6410.6\\lib\\plat-win', 'D:\\Python27\\ArcGISx6410.6\\lib\\lib-t
k', 'C:\\Users\\####\\AppData\\Roaming\\Python\\Python27\\site-packages', 'D:\\P
ython27\\ArcGISx6410.6\\lib\\site-packages', 'D:\\Program Files\\ArcGIS\\Server\
\bin', 'D:\\Program Files\\ArcGIS\\Server\\ArcPy', 'D:\\Program Files\\ArcGIS\\S
erver\\ArcToolBox\\Scripts']

However, trying to run as a batch file like this:

"D:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe" E:\START\CalFire2020_New\PythonScripts\AGOL_Management\GroupExport.py

Thanks for the response!

Reply
0 Kudos
MVP Frequent Contributor

You mention ArcGIS Pro, but the executable is for python 2.7 which is for arcmap. Which means that you don't have the correct environment activated.

For jupyterlab I use a desktop shortcut to set everything up

# Target 
C:\arc_pro\bin\Python\envs\arcgispro-py3\pythonw.exe "C:\arc_pro\bin\python\envs\arcgispro-py3\Scripts\jupyter-lab-script.py"
# Start in
C:\Arc_projects\JupyterNoteBooks
# ---- NOTE \arc_pro\ is the ArcGIS Pro installation folder
# ----- For jupyter notebook
# Target
C:\arc_pro\bin\Python\envs\arcgispro-py3\pythonw.exe "C:\arc_pro\bin\Python\envs\arcgispro-py3\Scripts\jupyter-notebook-script.py"

... sort of retired...
Reply
0 Kudos