According to the documentation: https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#viewmanager, to create a hosted feature layer view in code with the overwrite option:
create(name, spatial_reference=None, extent=None, allow_schema_changes=True, updateable=True, capabilities='Query', view_layers=None, view_tables=None, *, description=None, tags=None, snippet=None, overwrite=None, set_item_id=None, preserve_layer_ids=False, visible_fields=None, query=None)¶
overwrite Optional Boolean. If true, the view is overwritten, False is the default.
In my code:
the_view = the_item.view_manager.create(name=the_view_name, view_layers=the_layers, overwrite=True)
Throws an error:
local variable 'logging' referenced before assignment
and does not complete.
The same issue is exposed with creating the view using this method:
flc = FeatureLayerCollection.fromitem(the_item)
the_view = flc.manager.create_view(name=the_view_name, view_layers=the_layers, overwrite=True)
Can someone lend me a hand?
TIA
Do you have an exception to work with? Seems like a missing import in the source unless you are using the logging package in your script.
the exception is:
local variable 'logging' referenced before assignment
I mean the complete traceback showing where it failed. I want to see the line numbers and paths. If that's really all there is, then the problem must be in your code.
Traceback (most recent call last):
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "c:\Users\dvandervoort\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\__main__.py", line 39, in <module>
cli.main()
File "c:\Users\dvandervoort\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy/..\debugpy\server\cli.py", line 430, in main
run()
File "c:\Users\dvandervoort\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy/..\debugpy\server\cli.py", line 284, in run_file
runpy.run_path(target, run_name="__main__")
File "c:\Users\dvandervoort\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
return _run_module_code(code, init_globals, run_name,
File "c:\Users\dvandervoort\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "c:\Users\dvandervoort\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
exec(code, run_globals)
File "C:\temp\CreateAView\Project_Maker_BIG_TEST.py", line 62, in <module>
## create a FLC object
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\__init__.py", line 17449, in create
return mgr.create_view(
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\managers.py", line 2228, in create_view
logging.warning(
UnboundLocalError: local variable 'logging' referenced before assignment
Exception has occurred: UnboundLocalError
local variable 'logging' referenced before assignment
File "C:\temp\CreateAView\Project_Maker_BIG_TEST.py", line 62, in <module>
the_view_item = the_item.view_manager.create(name=the_view_name, view_layers=the_layers, overwrite=True)
UnboundLocalError: local variable 'logging' referenced before assignment
Thanks, Earl!
Ah, thanks. From looking at the source, it looks like the "logging" package is conditionally imported in a few places. In version 2.3.0.2 of the API, I don't see any obvious issues - our line numbers don't match up so I assume we are on different versions. Which version of the API are you using? You can look this up in the Pro package manager, doing running "conda list -n arcgispro-py3" or via "arcgis.__version__"
My hope is whatever problem exists in your version can be easily fixed by upgrading the arcgis package. I don't really understand what the purpose of the conditional imports are since logging is imported at the top of "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\arcgis\features\managers.py" in my version. Perhaps this was a temp fix someone put in between versions? You should be able to add "import logging" at the very top of this file (I would assume it's missing) and it should fix this particular error you're encountering.
I'm struggling to update the arcgis package. I upgraded ArcGIS Pro to 3.3 which upgraded the package to 2.3.0. Now ArcGIS Pro Package Manager won't give me an option to update. pip install arcgis assumes I have the latest version, which I do not. This appears to be an unrelated issue.
Nonethless, 2.3 0 of the arcgis package still throws an error, slightly different, but still present.
Traceback (most recent call last):
File "C:\Users\dvandervoort\AppData\Local\ESRI\conda\envs\arcgispro-py3-DEV\Lib\runpy.py", line 198, in _run_module_as_main
return _run_code(code, main_globals, None,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\dvandervoort\AppData\Local\ESRI\conda\envs\arcgispro-py3-DEV\Lib\runpy.py", line 88, in _run_code
exec(code, run_globals)
File "c:\Users\dvandervoort\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\__main__.py", line 39, in <module>
cli.main()
File "c:\Users\dvandervoort\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 430, in main
run()
File "c:\Users\dvandervoort\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 284, in run_file
runpy.run_path(target, run_name="__main__")
File "c:\Users\dvandervoort\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
return _run_module_code(code, init_globals, run_name,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\dvandervoort\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "c:\Users\dvandervoort\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
exec(code, run_globals)
File "C:\temp\CreateAView\Project_Maker_BIG_TEST.py", line 79, in <module>
the_view_item = the_item.view_manager.create(name=the_view_name, view_layers=the_layers, overwrite=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\dvandervoort\AppData\Local\ESRI\conda\envs\arcgispro-py3-DEV\Lib\site-packages\arcgis\gis\__init__.py", line 18328, in create
return mgr.create_view(
^^^^^^^^^^^^^^^^
File "C:\Users\dvandervoort\AppData\Local\ESRI\conda\envs\arcgispro-py3-DEV\Lib\site-packages\arcgis\features\managers.py", line 2683, in create_view
_log.warning(
^^^^
UnboundLocalError: cannot access local variable '_log' where it is not associated with a value
Open this file:
C:\Users\dvandervoort\AppData\Local\ESRI\conda\envs\arcgispro-py3-DEV\Lib\site-packages\arcgis\features\managers.py
At the top, do you see
import logging
and
_log = logging.getLogger()
You should be seeing those somewhere in the first 30 lines.
You know what? The more closely I look at the source the more it's apparent that "overwrite" option doesn't do anything!
This is the only thing it actually does:
if overwrite:
_log.warning(
"overwrite is currently not supported on this platform, and will not be honored"
)
And reading the documentation:
I'm so sorry to have led you astray here. I didn't run create with overwrite myself and so didn't catch this earlier. It seems absolutely pointless to add this parameter if it doesn't do anything at all.
Thank you for all your help, Earl, I really appreciate it.
I can cut to the chase, maybe you can give some direction on where to go from here.
I need a Hosted Feature Layer View to persist by itemID, where the content might change. The HFLVs are "sophisticated" in that we use Python to construct and define them. Properties available only available in the API.
Under the specter of a persisted HFLV is the fact that schema will change in the source HFL (layers get reindexed by additions, etc.) and the layers and definitions in the HFLV will change with a cadence of about once every 3 months or so. That's a requirement of our business. We have several applications that use the HFLV as a layer source - that's why they need to be persisted.
Thank you again!