Python error 'No module named arcpy'

5714
11
Jump to solution
12-29-2015 10:05 AM
PeachtreePTCGIS
New Contributor III

Hey folks,

New to python but not coding; I'm running the Python tutorial, Python for everyone, and outright have an error I can't seem to find a solution to. Using Python 2.7.10 I tried running the sample script and immediately got the 'No module named arcpy' error.

My path variables appear correct:

PYTHONPATH: C:\Python27\ArcGIS10.3\Lib\site-packages

My Desktop 10.x pth file:

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

I'm running Win 0S 7 Pro SP1 64-bit

The example script:

import arcpy  # script fails here

#set workspace

arcpy.env.workspace = r"C:\Student\PythEveryone10_1\RunningScripts\Oregon_Polk.gdb"

#set up a describe object for each fc in gdb

fcList = arcpy.ListFeatureClasses()

for fc in fcList:

    desc = arcpy.Describe(fc)

    print desc.spatialReference.name

print "Script completed"  

Looking for any suggestions for troubleshooting. Thanks!

(Curtis Price​ added formatting and put the import arcpy that is failing at the top)

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

just added an addendum...check my previous post so you can check your installation

View solution in original post

11 Replies
DanPatterson_Retired
MVP Esteemed Contributor

import arcpy  # add as first line .... then you can use arcpy

MatthewBaber
Esri Contributor

import arcpy
from arcpy import env

env.workspace = r"C:\Student\PythEveryone10_1\RunningScripts\Oregon_Polk.gdb"

PeachtreePTCGIS
New Contributor III

Dan/Matthew

Thank you for the response but I should note that the original script does include import arcpy and was missed in my poor copy/paste. Opening the script with 2.710 shell results in the error "No module named arcpy". Coincidentally, I downloaded pythonwin and tried to run the same script and of course because of different enviro variables it resulted in similar errors:

Traceback (most recent call last):

  File "C:\Users\twhitley\Downloads\Python27\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript

    exec codeObject in __main__.__dict__

  File "C:\Student\PythEveryone10_1\RunningScripts\SpatialRef.py", line 2, in <module>

    import arcpy

ImportError: No module named arcpy

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

If you run scripts within the python IDE inside of arcmap, then there is (apparently) no need to import arcpy.  I would not make this a habit.  I personally, use external IDEs such as PyScripter and PythonWin for all my scripting even when I have arcmap open.  Just a habit... because I often run different versions of python (ie 2.7.x and 3.4.x or 3.x.x etc) and I like different IDEs for different purposes and different reasons.

PS

pythonwin should be installed in the c:\python27 folder.  This is the executable from which I create a desktop shortcut.  Check to see if you indeed have a pythonwin folder inside of correct folder

C:\Python27\ArcGIS10.3\Lib\site-packages\pythonwin\Pythonwin.exe

PeachtreePTCGIS
New Contributor III

Thanks Dan, I'll work this through pythonwin and see what I can work out; much appreciated.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

just added an addendum...check my previous post so you can check your installation

View solution in original post

curtvprice
MVP Esteemed Contributor

There are so many ways to launch Python, and often many versions of Python installed, that this can get tricky. Use these Python commands to verify your environment:

import sys

import os

print (sys.executable)

print(sys.prefix)

print("PYTHONPATH: " + os.environ["PYTHONPATH"])

print("sys.path:")

print("\n".join(sys.path))

import arcpy

The results from these print statements will give you more confidence that the right environment is set up to import arcpy.

My guess is your PYTHONPATH is not getting into the sys.path for some reason.

The Windows env variable PYTHONPATH must be set before the shell that launches Python (or a parent process like ArcMap) is launched for the PYTHONPATH to be read. This may mean you need to log out and log in again.

For best results instead of running the Python you installed to your Downloads folder (C:\Users\twhitley\Downloads\Python27\ -- not a great place to install software!) you should just uninstall that and use Esri's.  Then you know all the python libraries will be compatible with ArcGIS and arcpy.

The command will below will launch the Python installed with ArcMap. If you are using PythonWin you should consider installing it there instead on another Python somewhere else on your computer.

C:\Python27\Desktop10.3\python.exe

DanPatterson_Retired
MVP Esteemed Contributor

ahh... so that explains why

print("PYTHONPATH: " + os.environ["PYTHONPATH"])

fails miserably on python 2.7.x on an iPad (Pythonista distribution) ... assuming that there is no equivalent for whatever iThingys run.  At least sys.path is informative ... sort of ...

0 Kudos
curtvprice
MVP Esteemed Contributor

if PYTHONPATH env variable (not required to run Python) Is not set you will get a KeyError (os.environ is a dict of all os env variables). On my Mac this works:

print("HOME: " + os.environ["HOME"])

0 Kudos