# Why would “import arcpy” hang Python script run from DOS/cmd prompt?

I have a very small test script (test.py) that I use to see if Python is installed and ready to use ArcPy. All it contains is:

print "Starting"
import os
print "OS imported"
import arcpy
print "ArcPy imported"


I’m running it under Vista SP1 with ArcGIS Desktop 10 SP1 and the only version of Python present is 2.6.5 which was installed by ArcGIS Desktop 10. ArcMap 10 SP1 is starting fine and the "import arcpy" and other arcpy functions work fine from the Python Window.

However, when I run “python test.py” from the DOS/cmd prompt, the first two print statements execute as expected, but the last one does not, so it seems to be failing/hanging on “import arcpy”. For a brief time on two separate days the script actually ran fine, but then it seems to stop working (which to me is bizarre). To try and figure what the problem is I have been reading Importing ArcPy and I think I have the system variables Path and PYTHONPATH set to appropriate values:

Path=C:\Program Files\weoapp;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\ATI Technologies\ATI.ACE\Core-Static;c:\Program Files\Common Files\Lenovo;C:\Program Files\Common Files\Roxio Shared\10.0\DLLShared\;C:\Program Files\Common Files\Roxio Shared\DLLShared\;C:\Program Files\Common Files\Roxio Shared\DLLShared\;C:\Program Files\Common Files\Roxio Shared\10.0\DLLShared\;C:\Program Files\ThinkPad\ConnectUtilities\;C:\Program Files\Lenovo\Client Security Solution;C:\Windows\System32\WindowsPowerShell\v1. 0\;C:\Program Files\QuickTime\QTSystem;C:\Program Files\Windows Live\Shared;C:\Program Files\QuickTime\QTSystem\;C:\Python26\ArcGIS10.0
PYTHONPATH=C:\Python26\ArcGIS10.0


but I’ve also tried:

PYTHONPATH=C:\Python26


When I first noticed the problem I had both Python 2.5.x (C:\Python25) and 2.6.5 (C:\Python26) present, so I went to considerable trouble to remove both, and uninstall ArcGIS Desktop 10 SP1 before reinstalling ArcGIS Desktop 10 (which installed Python 2.6.5) and then its SP1 again.

Does anyone have an idea as to what may be causing “import arcpy” to hang for me most of the time, or have any ideas as to what debugging steps may be available to me?

-
What happens if you delete the PYTHONPATH completely? –  Matthew Snape Jan 17 '11 at 13:56

arcpy (actually arcgisscripting which it wraps) extends a specific version of python. At arcgis 10.0 you need to run with python26. I'd make sure all the versions are matching.

C:\> test.py


Do this

C:\> C:\python26\Desktop10.0\python.exe test.py


Then you know it's starting the correct version of python. If that second line works, what's happening is that windows is starting python25, you'll need to do one of these

1) update windows file association for .py files (quick/technical)

2) uninstall python25 & 26 and install from the install disk (still pretty quick, less technical)

-
I found this to be a very useful answer so have upvoted it. When I do "C:\python26\ArcGIS10.0\python.exe test.py" on one laptop it still hangs (this one only has Python 2.6 on it). On the other (which has both Python 2.5 and 2.6 installed) that works but perplexingly "python test.py" has started to work again on that second laptop too. –  PolyGeo Jan 18 '11 at 10:28
I still have this issue but this was the answer I learned the most from. –  PolyGeo Jul 20 '11 at 23:33

This is more of a general troubleshooting help, than a python + arcgis answer. If it hangs it's executing the python module, does it ever timeout with an error?

So hopefully it's one of:

1. waiting for network connection
1. network port (check firewall settings)
2. DNS

If it's waiting for something to load (e.g. something in ArcGIS). But it should be possible to find out what by either running a python debugger on it or run System Monitor to see which files python loads/reads.

Good luck.

-
Thanks for trying to help with this Erik. However, I don't think it is a network port, firewall or DNS issue because on the two Vista SP2 laptops that I've tested it sometimes works on both but not always - and I'm confident that I am not altering network port, firewall or DNS on either. –  PolyGeo Jan 18 '11 at 10:17

Standard ArcMap installation doesn't add python to PATH. It associates .py with python executable. To check everything is as expected, type this in command prompt:

c:\Dev>assoc .py
.py=Python.File

c:\Dev>ftype Python.File
Python.File="C:\Python26\ArcGIS10.0\python.exe" "%1" %*


This is default for ArcGIS SP3.

Anyway, the simplest way to go is to use gotchula's advise and to explicitly specify python executable packed with ArcGIS to run against your script.

-
Thanks r.pankevicius - I'll try your advice if the problem crops up again. –  PolyGeo Feb 13 '12 at 22:49