ImportError: No module named arcpy

9633
16
Jump to solution
07-24-2013 07:27 AM
ionarawilson1
Occasional Contributor III
I am following the exercise on chapter, Python Primer, to run scripts in the command window. When I do the second exercise, I get a message:


C:\PYTHON_PRIMER\Chapter11>python CommandLine_Clip.py 'c:\PYTHON_PRIMER\Chapter1
1\Data' City_Facilities.shp Central_City_CommPlan.shp c:\PYTHON_PRIMER\Chapter11
\MyData' out_file.shp
Traceback (most recent call last):
  File "CommandLine_Clip.py", line 15, in <module>
    import arcpy, sys, os, traceback, datetime
ImportError: No module named arcpy


However I have no problem running the arcpy command in PyScripter or the Python Window, just in the command prompt.

The computer system variable path is pointing to python:

........................;C:\Program Files (x86)\ArcGIS\EsriProductionMapping\Desktop10.1\Bin;C:\Program Files (x86)\ArcGIS\Desktop10.1\Bin;c:\Python27\ArcGIS10.1

What could be the problem? Thanks!
Here is the script

# Batch and Schedule a Geoprocessing Script # Created by: Nathan Jennings #             www.jenningsplanet.com # Created on: 05.24.2011 # Update on: 10.24.2011 # Copyright: 2011  ''' !!!!  Change the output and log file paths as needed where the Chapter 11 folders exist.!!!  Save these changes.  Test the script to make sure it executes properly. '''   import arcpy, sys, os, traceback, datetime  CURDATE = datetime.date.today()  try:      # workspace for input data     # e.g c:\\pythonprimer\\chapter11\\data     arcpy.env.workspace = arcpy.GetParameterAsText(0)      # name of input shapefile feature class to be clipped     # i.e. city_facilities.shp     infile = arcpy.GetParameterAsText(1)      # name of clip shapefile feature class to use as the clip file     # i.e. central_city_commplan.shp     clipfile = arcpy.GetParameterAsText(2)      # output folder name for output     # e.g. c:\\pythonprimer\\chapter11\\mydata     output_ws = arcpy.GetParameterAsText(3)      # name of the output feature class     # NOTE: the output path is hard coded to the following path     #       the user will need to change this location     outfile = output_ws + os.sep + arcpy.GetParameterAsText(4)      # location of log file for tracking messages     logfile = output_ws + '\\log' + str(CURDATE) + '.txt'      if arcpy.Exists(logfile):         arcpy.Delete_management(logfile)      # Open the log file for writing     log = open(logfile, 'a')        print '\nRunning Clip Routine...'     print >> log, 'Running Clip Routine...'     print 'Input workspace is: ' + arcpy.env.workspace     print >> log, 'Input workspace is: ' + arcpy.env.workspace     print 'Input file is: ' + infile     print >> log, 'Input file is: ' + infile     print 'Clip file is: ' + clipfile     print >> log, 'Clip file is: ' + clipfile     print 'Output workspace is: ' + output_ws     print >> log, 'Output workspace is: ' + output_ws     print 'Output file is: ' + outfile     print >> log, 'Output file is: ' + outfile      if arcpy.Exists(outfile):         arcpy.Delete_management(outfile)      arcpy.Clip_analysis(infile, clipfile, outfile)      print 'Completed Clip'     print >> log, 'Completed Clip'      log.close()  except:     print arcpy.GetMessages(2)     tb = sys.exc_info()[2]     tbinfo = traceback.format_tb(tb)[0]     pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n     " +        str(sys.exc_type) + ": " + str(sys.exc_value) + "\n"     msgs = "arcpy ERRORS:\n" + arcpy.GetMessages(2) + "\n"      arcpy.AddError(msgs)     arcpy.AddError(pymsg)      print msgs     print pymsg      arcpy.AddMessage(arcpy.GetMessages(1))     print arcpy.GetMessages(1) 
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
I am a little confused...If python 2.7.3 is indeed the version that came with your ArcGIS install, it should not ever fail when you import arcpy.  Mine came with 2.7.2 but those who have installed more recently may get 2.7.3.  I have no problems importing arcpy from the command window.

Earlier, you said:


It seems I have two versions of Idle. I think I installed the 2.7.3 by downloading and installing it by myself. I am not sure about the other version though.


So maybe if you installed this separately, it probably would have looked to see if you already had a Python27 folder, which you would have.  I am thinking that maybe it just installed this extra IDLE 2.7.3 inside the ArcGIS folder.  That may have overwritten some of the arcpy stuff because you seem to be missing a lot of things from your "site-packages" folder (compared to mine anyways).  I have installed quite a few third party modules, but you are missing numpy and some other stuff that comes with the ArcGIS python install.

This is probably not what you want to hear, but you may have to uninstall ArcGIS and reinstall again to get the python folders in order...Before doing the reinstall, it may be a good idea to check to make sure that there is no Python27 folder left over after the uninstall.

I am pretty much out of ideas.  Does anyone else know what could be going on here?

View solution in original post

0 Kudos
16 Replies
StacyRendall1
Occasional Contributor III
Please test two things:

  1. Navigate into your ArcGIS Python folder, double click that python.exe enter 'import arcpy'

  2. Go the the command line and type in 'python' then 'import arcpy' what happens? I.e.:


C:\>python
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>import arcpy


If the first thing works then your ArcGIS Python is working fine (which the fact that PyScripter works also corroborates).

If the second thing fails I guess that your Path might have another Python on it, located before (to the left) of the ArcGIS Python. As the system works from left to right and takes the first python.exe it finds, you might inadvertently be loading the other one. The one that cannot access Arcpy.

If this is the case you can do one of:

  1. delete the other Python from your path

  2. move ArcGIS Python to the start of your path

  3. allow the other Python to access Arcpy


I would recommend either the first or second option. Details for doing the third can be found here.
0 Kudos
ionarawilson1
Occasional Contributor III
Hi Stacy,

I tried that and I still got an error. All the python.exe versions I have gave me an error. I am not sure which version ArcGIS is using now. It seems I have an installation of Python for ArcGISx6410.1 also, but if I delete this folder, arcpy still works in ArcGIS.
Can you tell which python is running by looking at the software installed? Is is python for 32 or 64?

I did not have another python from the path. However, I moved the one you suggested to the front (please see images in this post and next).


Also, is it ok that Python27\Lib has a folder called site-packages and python27\arcgis10.1\Lib also has a folder site-packages?
I also had a version of python26 that I removed yesterday because 10.1 uses 26 and I figured I don't need it anymore. Could the python be in another variable besides path? Not sure if any of this is contributing to the problem. Thank you!

[ATTACH=CONFIG]26196[/ATTACH][ATTACH=CONFIG]26197[/ATTACH][ATTACH=CONFIG]26198[/ATTACH][ATTACH=CONFIG]26199[/ATTACH][ATTACH=CONFIG]26200[/ATTACH]
0 Kudos
ionarawilson1
Occasional Contributor III
And here are the other images
[ATTACH=CONFIG]26205[/ATTACH][ATTACH=CONFIG]26201[/ATTACH][ATTACH=CONFIG]26202[/ATTACH][ATTACH=CONFIG]26203[/ATTACH]




I think what is making arcgis work with arcpy is that insided Python\Lib\site-packages there is a file called DEsktop10.1.pth pointing to arcpy:

C:\Program Files (x86)\ArcGIS\Desktop10.1\bin
C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy
C:\Program Files (x86)\ArcGIS\Desktop10.1\ArcToolbox\Scripts



And here are all the paths in the path variable. I looked at all the system variables and there is not a python anywhere else

C:\Python27\ArcGIS10.1;%CommonProgramFiles%\Microsoft Shared\Windows Live;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\DMIX;C:\Program Files (x86)\Common Files\Roxio Shared\DLLShared\;C:\Program Files (x86)\Common Files\Roxio Shared\OEM\DLLShared\;C:\Program Files (x86)\Common Files\Roxio Shared\OEM\DLLShared\;C:\Program Files (x86)\Common Files\Roxio Shared\OEM\12.0\DLLShared\;C:\Program Files (x86)\Roxio\OEM\AudioCore\;c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;c:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\jZip;C:\Program Files\ArcGIS\ArcSDE\sqlexe\bin;C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files (x86)\ArcGIS\EsriProductionMapping\Desktop10.1\Bin;C:\Program Files (x86)\ArcGIS\Desktop10.1\Bin;
0 Kudos
by Anonymous User
Not applicable
What does it look like when you print off your PYTHONPATH from within the interactive window of IDLE?  I see that you are on python 2.7.3.  Did you install that separately? Or is that what was installed with your 10.1 install?  (I only ask because I also have 10.1 and mine came with python version 2.7.2)

import sys
for p in sys.path:
    print p



Here is a screenshot of mine (both 32 bit and 64 bit version, also I added a few folders to my .pth file for 64 bit)

[ATTACH=CONFIG]26208[/ATTACH][ATTACH=CONFIG]26209[/ATTACH]
0 Kudos
ionarawilson1
Occasional Contributor III
It seems I have two versions of Idle. I think I installed the 2.7.3 by downloading and installing it by myself. I am not sure about the other version though.

When I enter the expression I get this for the Idle version 2.7.2:
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\ArcGIS\Python 2.7
C:\Python27\ArcGIS10.1\Lib\idlelib
C:\Windows\system32\python27.zip
C:\Python27\ArcGIS10.1\DLLs
C:\Python27\ArcGIS10.1\lib
C:\Python27\ArcGIS10.1\lib\plat-win
C:\Python27\ArcGIS10.1\lib\lib-tk
C:\Python27\ArcGIS10.1
C:\Python27\ArcGIS10.1\lib\site-packages
C:\Python27\ArcGIS10.1\lib\site-packages\win32
C:\Python27\ArcGIS10.1\lib\site-packages\win32\lib
C:\Python27\ArcGIS10.1\lib\site-packages\Pythonwin

And this for the version idle version 2.7.3.
C:\Python27\Lib\idlelib
C:\Python27\lib\site-packages\distribute-0.6.25-py2.7.egg
C:\Windows\system32\python27.zip
C:\Python27\DLLs
C:\Python27\lib
C:\Python27\lib\plat-win
C:\Python27\lib\lib-tk
C:\Python27
C:\Python27\lib\site-packages
C:\Program Files (x86)\ArcGIS\Desktop10.1\bin
C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy
C:\Program Files (x86)\ArcGIS\Desktop10.1\ArcToolbox\Scripts
C:\Python27\lib\site-packages\setuptools-0.6c11-py2.7.egg-info



[ATTACH=CONFIG]26210[/ATTACH]
[ATTACH=CONFIG]26211[/ATTACH]
[ATTACH=CONFIG]26213[/ATTACH]
[ATTACH=CONFIG]26212[/ATTACH]
[ATTACH=CONFIG]26214[/ATTACH]
0 Kudos
by Anonymous User
Not applicable
Ok, I think the problem is coming from the 2.7.3.  Whenever you install a new version of python/IDLE, it will always default to the most recent install.  Since you installed 2.7.3 AFTER the ArcGIS install that had 2.7.2, your default python is 2.7.3.  Your PYTHONPATH for 2.7.3 has some of the folders, but not all and it is not accessing the arcpy DLL's.  I think you should delete this and that *should* fix the problem. 

Was there a particular reason for the 2.7.3 install?  I do not think there would be a big difference in functionality between 2.7.2 and 2.7.3.  There is a pretty big difference in 2.7 vs 3.x, but I think you would be best off by unistalling 2.7.3.  Then, your default python should go back to 2.7.2 and arcpy should work no problem.
0 Kudos
ionarawilson1
Occasional Contributor III
So Caleb, the install you see in this image,is this the one I should uninstall?
I am not sure where 2.7.2 is installed
Why don't I have the 2.7.2  in this image?
Also, which one is the pythonpath for 2.7.3 that I should delete?

Because it seems the python in ArcGIS is also 2.7.3. I check the version in the python window and I got:

2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]



So why arcpy works in 2.7.3 but not in the command window?

Also, where do I get 2.7.2? should I get it from the arcgis installation disc if I have it, should I reinstall arcgis or should I download 2.7.2 from the web?  It is weird that idle has 2.7.2 but I can't find where it is installed



Thank you!!!

Thanks[ATTACH=CONFIG]26215[/ATTACH]
0 Kudos
by Anonymous User
Not applicable
I am a little confused...If python 2.7.3 is indeed the version that came with your ArcGIS install, it should not ever fail when you import arcpy.  Mine came with 2.7.2 but those who have installed more recently may get 2.7.3.  I have no problems importing arcpy from the command window.

Earlier, you said:


It seems I have two versions of Idle. I think I installed the 2.7.3 by downloading and installing it by myself. I am not sure about the other version though.


So maybe if you installed this separately, it probably would have looked to see if you already had a Python27 folder, which you would have.  I am thinking that maybe it just installed this extra IDLE 2.7.3 inside the ArcGIS folder.  That may have overwritten some of the arcpy stuff because you seem to be missing a lot of things from your "site-packages" folder (compared to mine anyways).  I have installed quite a few third party modules, but you are missing numpy and some other stuff that comes with the ArcGIS python install.

This is probably not what you want to hear, but you may have to uninstall ArcGIS and reinstall again to get the python folders in order...Before doing the reinstall, it may be a good idea to check to make sure that there is no Python27 folder left over after the uninstall.

I am pretty much out of ideas.  Does anyone else know what could be going on here?
0 Kudos
ionarawilson1
Occasional Contributor III
I think you got it Caleb. So should I delete that entire python folder before installing ArcGIS? Do you know if I need to reinstall the extensions? Should I delete anything from the registry to make sure that python version is really gone! Thank you Stacy and Caleb! You both helped me with this!
0 Kudos