Debugging ImportError: No module named arcpy
This error is discussed near the bottom of the Importing ArcPy documentation page where there are some additional notes about ArcGIS 10.2.x (if that is the version that you are using):
Tip: If importing ArcPy produces either of the following errors, the required modules could not be found:
- ImportError: No module named arcpy
- ImportError: No module named arcgisscripting
To address this, browse using Windows Explorer to the
python27\Lib\site-packages
folder and add or edit the Desktop10.2.2.pth file. The file should contain the three lines shown below (corrected to your system's path if they do not match):
c:\Program Files\ArcGIS\Desktop10.2.2\arcpy c:\Program Files\ArcGIS\Desktop10.2.2\bin c:\Program Files\ArcGIS\Desktop10.2.2\ArcToolbox\Scripts
@PolyGeo's solution is top-notch, but not perfect for every use case.
For example, in my use case, I had a Python 2.7.11 install on my machine and then I installed ArcGIS which came with Python 2.7.8. When I followed the instructions at Importing ArcPy, I found that the path C:\Python27\ArcGIS10.3\Lib\site-packages
had a file called Desktop10.3.pth
in it with the following lines:
C:\Program Files (x86)\ArcGIS\Desktop10.3\bin
C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy
C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcToolBox\Scripts
Yet, I still could not get ArcPy to import w/o error. I found that this is because the Python path being used by my machine is the 2.7.11 path, rather than the 2.7.8 path (the ArcGIS-specific Python install). Even if you include the correct path inside your Windows Environment Variables, it will not work because when you type python
into the Command Line Interface, Windows does not know which Python install to use.
This is my work-around:
Navigate to
C:\Python27
. You should see a folder calledArcGIS10.3
or whichever version you have installed.Click into
ArcGIS10.3
and locate the Python.exe executable. Copy/paste this exe and rename itarcpy
or something else that you can remember.Make sure you have added the
C:\Python27\ArcGIS10.3
directory into your path in your Windows Environment Variables.Open a new
cmd
and type the name you used (i.e.arcpy
). You should see a Python interpreter open up and you should see the correct version 2.7.8. If this is all correct, try typingimport arcpy
into the interpreter. If it works, you have been successful!
This problem happens for me because the path of ArcGIS
was in Program File(x86)
instead of normal place(program file). there are two methods to fix the problem:
you can change the path of python for example by
PyScripter
(it has a function to change the path) orthe easiest way is: change the name of python27's folder to python27_old. in control panel click on
program and feature
, chooseuninstall/change
and repairArcGIS
. it will install python again with correct path. It worked for me.