2

I have installed ArcGIS Pro 2.0 which runs Python 3.5.3. I am trying to get started with the Arcpy library using the IDE PyCharm. I started a new project and pointed to the python file of the ArcGIS Pro installation (interpreter):

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe

Then I tried to execute the following two lines of code:

from arcpy import env
arcpy.env.workspace = "C:/Users/me"

And I get an error:

 from arcpy import env
 ImportError: cannot import name 'env'

I tried as well with different modules from arcpy but they all return the same error; meaning that they can not import each module.

Then I tried to run the same lines of code through the Python Console embedded in PyCharm and it works.

What am I missing here?

enter image description here

1

A couple of errors jump out at me here. Note that the line numbers I reference below refer to the line numbers in your screenshot.

Firstly, arcpy in line 3 is undefined. Since you called from arcpy import env, you should set your workspace by changing env.workspace, not arcpy.env.workspace. If instead line 2 was import arcpy, your line 3 would be appropriate.

Second, according to your screenshot, it seems that the file you have created is called arcpy.py. What is likely happening is that, upon calling from arcpy import env, your script is trying to import env from the arcpy module. However, because your script's filename is arcpy.py, it thinks that it is the arcpy module you are seeking, and it is trying to import env from itself, hence the ImportError! Renaming your script so that it doesn't conflict with the name of a module you wish to import, such as should solve the problem.

PyCharm has highlighted arcpy in line 2 (your first non-blank line). If you hover over the highlight, PyCharm should display something like: Unused import statement 'from arcpy import env' and Import resolves to its containing file.

Hopefully you solved your problem a couple of years ago, but I post this today with hope that it might help someone in the future!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.