Hello. I'm currently using arcgis pro 2.8.2. I go to "Analysis"-->"Python"-->"Python window". And I typed the following code in the screenshot, but I got the error message that "name arcpy not defined" (arcpy_not_defined.png). This is weird because I have read instructions saying that if you use python window within arcgis, you don't need to import arcpy. Even when I import arcpy, it does not work. I was wondering if anyone has any idea what is going on. Thank you.
Solved! Go to Solution.
@JoeBorgione& @DanPatterson : They are using the normal ArcGIS Pro Python window, just opening it in a slightly more cumbersome way.
@zhongying_gan: You are using a custom Python environment (see where it says "arcgispro-py3-clone" in your error message) and the numpy package in this environment seems to be broken. Because arcpy needs numpy, it isn't imported on startup (so that you don't explicitly need to import it), that's why you get the "name not defined" error.
Ways to fix this:
try
import arcpy
then do the line you want
Does it work then? (if it does, it means that arcpy isn't automagically available within the python window and you have to import the package before you use any of the modules within it)
Hello Dan. Thank you for your reply. I have tried typing "import arcpy", but it didn't work. Please refer to the error message in the screenshot attached under my question. Thank you.
I've never added a python window that way, but I have a python window open by going to the View Tab, and clicking on Python Window. I didn't need to import arcpy.
If it isn't the python window within Pro, then it won't work.
If you are using some version of python that can't even import arcpy, then remove it and use the version installed by ArcGIS Pro. If you are working outside of Pro and you want to run scripts or commands, then get a python IDE installed (like Spyder or VS or pycharm etc)
@JoeBorgione& @DanPatterson : They are using the normal ArcGIS Pro Python window, just opening it in a slightly more cumbersome way.
@zhongying_gan: You are using a custom Python environment (see where it says "arcgispro-py3-clone" in your error message) and the numpy package in this environment seems to be broken. Because arcpy needs numpy, it isn't imported on startup (so that you don't explicitly need to import it), that's why you get the "name not defined" error.
Ways to fix this:
Great catch @JohannesLindner !