Unable to use arcpy in Python window within arcgis pro

1332
6
Jump to solution
08-19-2021 10:04 AM
zhongying_gan
New Contributor III

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.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

@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:

  • reinstall the numpy package in your cloned environment (talk to your admin)
  • go back to the default environment (see this link)
  • as a last resort, reinstall ArcGIS Pro

Have a great day!
Johannes

View solution in original post

6 Replies
DanPatterson
MVP Esteemed Contributor

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)


... sort of retired...
zhongying_gan
New Contributor III

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.

0 Kudos
JoeBorgione
MVP Emeritus

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.

That should just about do it....
0 Kudos
DanPatterson
MVP Esteemed Contributor

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)


... sort of retired...
0 Kudos
JohannesLindner
MVP Frequent Contributor

@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:

  • reinstall the numpy package in your cloned environment (talk to your admin)
  • go back to the default environment (see this link)
  • as a last resort, reinstall ArcGIS Pro

Have a great day!
Johannes
JoeBorgione
MVP Emeritus

Great catch @JohannesLindner !

That should just about do it....
0 Kudos