English

Problem: Unable to use the ArcPy module

Description

The ArcPy module is used in ArcGIS functionality and as a stand-alone script. In some cases, importing the ArcPy module fails while in others, import is successful, but the functions within the module are unavailable, and the script fails to perform a desired process, such as running a geoprocessing tool. In addition, when attempting to import ArcPy using 64-bit Python with ArcGIS Server, the following error message is returned:

Error:
"ImportError: No module named arcpy"

To access the Python interpreter and run Python scripts, the location of the required python.exe file must be known to the system. For the system to recognize the location, the path to the python.exe file is included in Windows Environment Variables. The path to the location is named PYTHONPATH.

Cause

The path defined in Environment Variables follows the latest installation of Python by default. So, if Python 3.7 is installed after Python 2.7, the path to the python.exe file version 3.7 is specified in Environment Variables. This causes Python processes running in ArcGIS Desktop to fail.

If importing the module fails, this may be due to:

  • A corrupt Python installation
  • The script referring to a wrong default Python path in Environment Variables. This may also affect the usage of Jupyter Notebook.
  • Multiple Python versions installed, for example, if both ArcGIS Desktop and ArcGIS Server are installed on the same machine.
Note:
Users can install different versions or bit versions of Python. ArcMap uses Python 2.7.x (32-bit), ArcGIS Server uses Python 2.7.x (64-bit), and ArcGIS Pro uses Python 3.x. For more information, refer to the following article, FAQ: What version of Python is used in ArcGIS?
Using a version of Python other than the version expected with ArcGIS can lead to compatibility issues.

If importing the arcpy module is successful, but the functions are unavailable, check the location of the module. Each module in the arcpy library has a unique name and resides in the folder where Python is installed. Below are common locations of the arcpy module:

  • For ArcGIS Desktop
C:\Program Files (x86)\ArcGIS\Desktop10.6\arcpy\arcpy
  • For ArcGIS Server
C:\Program Files\ArcGIS\Server\arcpy\arcpy
  • For ArcGIS Pro
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib

When running a Python script, the script first locates the matching module in the folder containing the script, and then imports the script. On the other hand, if there is a script named arcpy.py residing in a closer location such as in Desktop, the Python script grabs the one in Desktop instead. 

Solution or Workaround

If importing the arcpy module fails, edit or add the Python path in the Windows Environment Variables. The correct path for the Python installation is as follows:

  • For Python 2 with ArcGIS Desktop installed:
C:\Python27\ArcGIS10.x
  • For Python 2 with ArcGIS Server installed:
C:\python27\ArcGISx6410.x
  • For Python 3 with ArcGIS Pro installed:
C:\Program Files\ArcGIS\Pro\bin\Python

The following steps describe how to add the Python path to Environment Variables.

  1. Open the Environment Variables window, following the steps provided in the following documentation, How to: Add Tool Locations to the PATH Environment Variable.
  2. Under System variables, select Path and click Edit. This creates a new text box and the text cursor is automatically directed to the box.
  3. Enter the path specified according to the version of ArcGIS Desktop used. Click OK. The following image shows a sample ArcGIS Desktop 10.6 path. Add the path for Python 3 if ArcGIS Pro is installed
The image of the System variables window with the Path inserted.
Note:
For ArcGIS Desktop, open the Desktop10.x.pth file in the C:\Python27\ArcGIS10.x\Lib\site-packages folder using a text editor, for example, Notepad++, and check if the file contains the following three paths:
C:\Program Files (x86)\ArcGIS\Desktop10.x\bin
C:\Program Files (x86)\ArcGIS\Desktop10.x\arcpy
C:\Program Files (x86)\ArcGIS\Desktop10.x\ArcToolbox\Scripts
Note:
For ArcGIS Pro, open the ArcGISPro.pth file in the C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages using a text editor and check if the file contains the following syntax:

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 ""
Note:
For ArcGIS Server, open the Server10.x.pth file in C:\Python27\ArcGIS10.x\Lib\site-packages using a text editor and check if the file contains the following three paths:

C:\Program Files\ArcGIS\Server\arcpy
C:\Program Files\ArcGIS\Server\bin
C:\Program Files\ArcGIS\Server\ArcToolbox\Scripts
If an incomplete or a corrupted installation is suspected, repair the software.
  1. Remove any ArcGIS10.x folders from the following directory: C:\Python27.
  2. Rerun the installation tool, and click the Repair radio button.
The image of the ArcGIS Installation window

Repair the software if the cause might be importing the ArcPy module is successful but the functions are unavailable, maintain the desired .py module file and rename all the other files.

If importing fails when using the Jupyter Notebook, use the following steps to solve the issue:
  1. Open the Anaconda Prompt.
  2. Create a new Python environment using the following command.
conda create -n EnviornmentName
  1. Install the ArcGIS module.
conda install -c esri ArcGIS
  1. Install the arcpy module.
conda install -c esri arcpy

Related Information