0

I am trying to import arcpy in a script, which is running in a PyCharm environment using the Python 3.6.6 installation that comes bundled with ArcGIS Pro 2.3.2 as the base interpreter. I get the following error:

Traceback (most recent call last):
    File "C:/Users/lharris/PycharmProjects/general/test.py", line 1, in <module>
        import arcpy
    File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\__init__.py", line 72, in <module>
        from arcpy.geoprocessing import gp
    File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\__init__.py", line 14, in <module>
        from ._base import *
    File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 14, in <module>
        import arcgisscripting
ImportError: DLL load failed: The specified module could not be found.

As per the only answer to Importing ArcPy in PyCharm? I have created a new PyCharm project using the Python install described above, which for my default installation is located here:

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

Based on the answers to Why can't I import arcpy? and other similar issues, I have opened that Python interpreter on the command line, ensured that I can import arcpy (which I can) and then checked sys.path, which gives me:

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python36.zip
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\DLLs
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages
C:\Program Files\ArcGIS\Pro\bin
C:\Program Files\ArcGIS\Pro\Resources\ArcPy
C:\Program Files\ArcGIS\Pro\Resources\ArcToolbox\Scripts
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\future-0.16.0-py3.6.egg
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\pytz-2018.5-py3.6.egg
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\sympy-1.2-py3.6.egg

I have created a .pth file in venv\Lib\site-packages that contains all of these paths. Without this file, the error given was "no module named arcpy" and with only 'C:\Program Files\ArcGIS\Pro\Resources\ArcPy' on the list, the error was "no module named arcgisscripting". With the complete list above, the error is the one I give at the beginning of this question.

Is there anything I've not tried that might help me to get arcpy imported in a Python 3 script in PyCharm using ArcGIS Pro?

2 Answers 2

0

You seem to be on the right track. On my computer I found the arcgisscripting library here:

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages

perhaps you could try your .pth file with just two entries:

C:\Program Files\ArcGIS\Pro\Resources\ArcPy C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages

see if that works for you. I'd try it myself, but I'm afraid I will mess up my ArcMap 10.6 python scripting environment in PyCharm.

3
  • OK, I tried it out - created a new interpreter and pointed it to C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3 Then I tried to import arcpy - it worked. Tried to import arcgisscripting, it failed. Then I looked at a bunch of settings without changing anything, came back and tried again and it can find arcgisscripting because it can auto-complete my statements as I type, but it still fails on importing it when I run my little import test script. For me it seems that arcgisscripting can't load because it imports somehting called multiarray, and it can't find that.
    – Colin Lang
    Commented Oct 7, 2019 at 13:55
  • Thanks for the answer. With just C:\Program Files\ArcGIS\Pro\Resources\ArcPy and C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages in the .pth file I still get the "ImportError: DLL load failed: The specified module could not be found." error. But following your suggestion, I have found the arcgisscripting library in the same place as yours. Commented Oct 7, 2019 at 14:03
  • It seems to me, that if you add C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\numpy\core\include\numpy, to the .pth, it might be able to load multiarray, which would let it load arcgisscripting, which would let it load arcpy. Maybe. But it sure seems like that should NOT be necessary.
    – Colin Lang
    Commented Oct 7, 2019 at 14:06
-1

I found the answer - I found the .pth file located within the ArcGIS Pro installation itself (default location C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\ArcGISPro.pth) and used it as the basis for the .pth file in my virtual environment, which now looks like this:

import os; d = r"C:\Program Files\ArcGIS\Pro\bin"; os.environ["PATH"] = r"{};{}".format(d, os.environ["PATH"])
import sys; d = r"C:\Program Files\ArcGIS\Pro\bin"; sys.path.append(d)
import sys; d = r"C:\Program Files\ArcGIS\Pro\Resources\ArcPy"; sys.path.append(d)
import sys; d = r"C:\Program Files\ArcGIS\Pro\Resources\ArcToolbox\Scripts"; sys.path.append(d)
import os; e = "FOR_DISABLE_CONSOLE_CTRL_HANDLER"; os.environ[e] = '1' if (not e in os.environ) else ""
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages

I think the difference is that C:\Program Files\ArcGIS\Pro\bin has been appended to both os.environ["PATH"] and sys.path.

Whilst I don't quite understand why this work, I can now successfully import arcpy in Python 3.

1
  • 1
    ArcGISPro and python 3 now use Virtual Environments. You could hack it as you have done here, but there is a much easier way. Check up what the virtual environment is set in ArcGISPro in the settings and set the same virtual environment in PyCharm properties. This is why we had to change to Pycharm - it supports virtual environments. Because both systems insist on setting up their own "Projects" you are not integrated.
    – KimO
    Commented Oct 31, 2019 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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