ImportError: No module named arcpy

9992
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
16 Replies
by Anonymous User
Not applicable
Here is what I would do:

1. Uninstall ArcGIS
2. Delete the "C:\Python27" folder if it still exists after the uninstall
3. Reinstall ArcGIS


To answer your questions:

Do not change anything in the registry; the new install should take care of everything.

You do not have to "reinstall" the extensions, but rather just provide your license number for each extension.  Be sure to paste all of your license numbers in a text file before uninstalling.  Otherwise, I think Esri usually sends you an email with all the license numbers for your reference.

EDIT:  I just noticed looking at your screenshots of your folders/PYTHONPATH again, something is definitely not right.  For instance, your "Lib" folder is not even inside of an ArcGIS folder.  The install of 2.7.3 definitely took over the folders that shipped with the ArcGIS install.  Now I am convinced the reinstall is your best bet.  Good luck!
0 Kudos
ionarawilson1
Occasional Contributor III
Thank you Caleb! I will do that and I will let you know how it goes! Thank you so much again! I love Python!!!
0 Kudos
StacyRendall1
Occasional Contributor III
Ionara, Caleb is on the right track.

Do you need any Python other than the Arcpy one? If the Arcpy one is the only one you need you should also uninstall every Python and Python related thing you can find, so from your image of installed programs that is:

  • PyScripter 2.5.3

  • Python 2.7 matplotlib-1.1.0

  • Python 2.7 numpy-1.7.1

  • Python 2.7 pywin32-218

  • Python 2.7.3

  • Python tools for Visual Studio 2010 (only if you do not use this - if you don't know what it is, you can probably uninstall it)



Then:

  1. uninstall ArcGIS

  2. delete Python27 folder, and any other Python folders (in local drives like C: or D:) if you have them

  3. restart the computer

  4. reinstall ArcGIS 10.1

  5. install 10.1 Service Pack 1


This whole process will leave you with one single Python. Add this to the start of your system PATH (you have already done this, so maybe no change is required). Then if you need other libraries, i.e., matplotlib they should automatically install to that Python.

If you need 64 bit background processing, install that, and make that your one and only Python (i.e. add it to the PATH instead of 32 bit and install 64 bit libraries to it only)...

Hope this sorts all your problems out!
0 Kudos
ionarawilson1
Occasional Contributor III
Thank you Stacy! Why do I need to uninstall all of them? (just curious) and how do I use the python 64? Basically, what is the advantage of using it and how do I choose that when running the scripts? Thanks!!!
0 Kudos
StacyRendall1
Occasional Contributor III
Thank you Stacy! Why do I need to uninstall all of them?


I am quite a fan of keeping everything as clean as possible, so I tend to uninstall/delete anything that I am not using. In your case it seems like the number of Pythons was adding to the overall confusion. As you can get by perfectly fine on just one, and thus avoid any future complications, I would strongly recommend doing so.

(just curious) and how do I use the python 64? Basically, what is the advantage of using it and how do I choose that when running the scripts? Thanks!!!


Do not install 64-bit background geoprocessing unless you really need to.
If you don't need it, but do install it, it will just be yet another Python in the way and adding to the confusion. To make use of 64-bit background geoprocessing your computer needs to have more than 4GB of RAM, less than this it won't solve any of your problems (not entirely true, but for most practical purposes it is accurate). Things that might indicate 64-bit background geoprocessing will help:

  1. you frequently have Arc programs or scripts crash due to MemoryError

  2. you do large operations, but have implemented a workflow of breaking your inputs into smaller parts to avoid crashes and instability


For me 64-bit background geoprocessing has been incredibly useful. I used to use Arcs 32-bit Python as my default and have a 64-bit Python installed to do non-GIS parts of some of my work. For some large workflows I would have scripts running one after the other that switched between these two, doing any non-GIS processing in the 64-bit Python, and each GIS script would have to be very carefully designed to break everything up into much smaller parts that could be handled by 32-bit. No longer having to worry about all this has saved a lot of time, and made everything a lot simpler to both write and maintain. Most GIS users won't have these issues; so, like I said above, don't bother unless you really need it!

To choose the 64-bit Python when running scripts, you simply pass the script to the 64-bit python.exe rather than the old 32-bit one...
0 Kudos
ionarawilson1
Occasional Contributor III
Thank you Stacy! I don't need it yet, so I am not going to install it but it is good to know.
0 Kudos
ionarawilson1
Occasional Contributor III
Hi guys,

This post is just to let you know that I had the courage to uninstall ArcGIS and delete the Python folder (I also deleted all the python2.7 software, just keeping pyscripter and the tool for Visual Studio). I redid the exercise and everything worked like a charm so it was really a problem with having that other Python installation. Thank you so much for all your help!!!:D
0 Kudos