My python script for ArcGIS API work perfect in jupyter notebook. But to automate the task, I downloaded it as a python script and run from a bat file on Windows 7. But the call "from arcgis.gis import GIS" fails with these errors. Any idea?
Traceback (most recent call last):
File "W:\gis_data_admin\Admin\Scripts\BHP_AGOL\BHP_AGOL_Users_Mig.py", line 8, in <module>
from arcgis.gis import GIS
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\arcgis\__init__.py", line 3, in <module>
from . import features, geoanalytics, geocoding, geometry, geoprocessing, network, raster, realtime, schematics, mapping
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\arcgis\features\__init__.py", line 21, in <module>
from .feature import Feature, FeatureSet, FeatureCollection
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\arcgis\features\feature.py", line 16, in <module>
from arcgis.gis import Layer
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\arcgis\gis\__init__.py", line 929
SyntaxError: Non-ASCII character '\xe2' in file C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\arcgis\gis\__init__.py on
line 930, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
Can you share the contents of the Windows batch file?
set PATH=C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3;C:\Program Files\ArcGIS\Pro\bin\Python\Scripts;C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Scripts;%PATH%
set PYTHONPATH=C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages;%PYTHONPATH%
set WORK_DIR=W:\gis_data_admin\Admin\Scripts\BHP_AGOL
w:
cd %WORK_DIR%
python --version
BHP_AGOL_Users_Mig.py
----- below is the content of this py file generated from jupyter download -------
# coding: utf-8
# In[1]:
from arcgis.gis import GIS # failed here
..................................
Ok, don't know why, but if I replace my python script call with
"C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe" BHP_AGOL_Users_Mig.py
Then everything works fine, both arcgis.gis and pandas
As Jeff Bigos suggests in your related question https://community.esri.com/thread/218174-how-to-make-python-calls-to-arcgisgis-and-pandas-work-outsi... , see Run stand-alone scripts—ArcPy Get Started | ArcGIS Desktop . The documented way to run a stand-alone script using ArcGIS Pro Python distribution is:
How do I run a stand-alone script?
"c:\Program Files\ArcGIS\Pro\bin\Python\scripts\propy.bat" my_script.py
Here, propy.bat acts as a replacement for calling python.exe.
Why would I want to use propy.bat?
While working in an ArcGIS Pro project, you could create and use the new conda environment with specific sets and versions of Python packages. For example, when running stand-alone scripts, if you run c:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe (the default conda environment for ArcGIS Pro), you would not necessarily have the same set of packages as you are using inside the ArcGIS Pro application. This is why you would want to use propy.bat: it determines the application’s active conda environment and activates it in your stand-alone script.