Select to view content in your preferred language

Windows file associations not getting me to the correct Python

5356
4
09-12-2012 10:03 AM
curtvprice
MVP Alum
Update: This has to do with my two installed Pythons (ArcGIS and EPD) -- Windows isn't finding the right one. I am going to uninstall and reinstall Python using the ArcGIS installer and report back.
Update2: Uninstall and resinstall of Python using ArcGIS installer didn't help. Updated test script below. Any ideas?


[/HR]


I am having problems importing arcpy or arcgisscripting when launching python through windows file associations with Arc 10.1. This may have to do with me dinking with the file associations to try to get a specific python folder to be used by ArcGIS, but I'm back to normal now. ArcGIS and IDLE seem to be finding the ArcGIS Python fine.

E:\work>assoc .py
.py=Python.File

E:\work>ftype Python.File
Python.File="D:\Python27\ArcGIS10.1\python.exe" "%1" %*


Here's my test script:
import sys
print sys.version
print "hello there"
import arcpy


When I run this from windows, I get an error when I try to import arcpy because I'm getting the wrong Python

E:\work>x.py
2.7.3 |EPD 7.3-2 (32-bit)| (default, Apr 12 2012, 14:30:37) [MSC v.1500 32 bit (Intel)]
hello there
Traceback (most recent call last):
  File "E:\work\x.py", line 4, in <module>
    import arcpy
ImportError: No module named arcpy


However, if I specify the ArcGIS python version intepreter explictly, no problem:
E:\work>"D:\Python27\ArcGIS10.1\python.exe" x.py
2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]
hello there


Here's what the file association looks like in the Windows GUI:

[ATTACH=CONFIG]17634[/ATTACH]

Note, my DDE setting used have the text: "%1" %*
but this causing the following popup error - (so I cleared the DDE box):

[ATTACH=CONFIG]17635[/ATTACH]


I'm seeing this on XP64, Python 2.6, ArcGIS 10.1.
0 Kudos
4 Replies
StacyRendall1
Frequent Contributor
Just did a bit of research, and I believe that the Python in your system PATH will be used by default to run files from the command line, but the file association (as shown in your post) defines which Python is used when double clicking a file in Windows Explorer.

You can use set from the command line to list your current environment variables, scroll up to Path=... and see which Python is referenced there.

If your Path is referencing the wrong Python you have two options, both of which have potential problems:

  1. Temporarily change path for this command window: use set Path=D:\Python27\ArcGIS10.1\python.exe in the command prompt before running your script. The downside is that you have to do this every time you open a new command prompt.

  2. Change your system path: right click on My Computer, go to Properties, click the Advanced tab, click Environment Variables, then in the System variables box scroll down to Path and edit it; find the path to the incorrect Python and change it to the one you want (be careful not to delete or edit anything else, and don't add extra spaces). The problem with this solution is that EPD may require Python to be in the Path for it to run (although I would hope it has provision for you supply a path)...


Let me know how you get on!

Stacy
0 Kudos
by Anonymous User
Not applicable
Original User: mdenil

Rather than changing the path setting,
you could set an os evnvironment variable to the desired python instalation,
similar to the ARCHOME variable.

Then, you would issue your command line thus:
%PY26%\python.exe runthis.py

... save some keystrokes, anyway.
0 Kudos
curtvprice
MVP Alum
Just did a bit of research, and I believe that the Python in your system PATH will be used by default to run files from the command line, but the file association (as shown in your post) defines which Python is used when double clicking a file in Windows Explorer.


I'm pretty sure the file association is used at the command line as well. See the help for the ftype command:
> ftype /?
...
    ASSOC .pl=PerlScript
    FTYPE PerlScript=perl.exe %1 %*

would allow you to invoke a Perl script as follows:

    script.pl 1 2 3


I tried clearing my path to see if it would help just in case. Unfortunately it did not help. For some reason the system is still launching my EPD python (E:\python27_epd32\python.exe) instead of the ArcGIS one (D:\Python27\ArcGIS10.1\python.exe).

I'm wondering if the Python installer set something in the registry that is overriding the file association. A clue: WingIDE is also opening EPD unless I explicitly set the project properties to open the ArcGIS Python.

Note PYTHONHOME is not set (it would have come up with the "set py" command below).

>set py
PYTHONPATH=D:\ArcGIS\Desktop10.1\ArcToolbox\Toolboxes\USGS_EGISTools\scripts
> set PATH=.
> set path
Path=.
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.PSC1

> x.py
2.7.3 |EPD 7.3-2 (32-bit)| (default, Apr 12 2012, 14:30:37) [MSC v.1500 32 bit (Intel)]
hello there
Traceback (most recent call last):
  File "E:\work\x.py", line 4, in <module>
    import arcpy
0 Kudos
by Anonymous User
Not applicable
Original User: curtvprice

I solved my problem.

On our machines (government-wide standard security), our user file associations cannot be viewed or edited from Windows Explorer / Tools / Folder Options / File types tab.

However apparently my EPD python installer was writing to my user registry here, and directing me to open .py files using epd32:

Key: HKEY_USERS\S-1-5-21-3697291689-1161744426-439199626-5187\Software\Classes\Python.File\shell\open\commandValue: (default)
Data: "E:\python27_epd32\python.exe" "%1" %*

The three entries were named:

Python.File
Python.NoConFile
Python.CompiledFile

Apparently these settings were overriding what I could see using the assoc and ftype commands.

Because of our security restrictions, the only way I could fix this was by opening regedit as -pr user, searching for my own registyr area by looking for \username\ under HKEY_USERS, locating these registry keys, and updating the data values to point back to my ArcGIS python path.

Data: "D:\Python27\ArcGIS10.1\python.exe" "%1" %*

Now double-clicked .py files are launching from D:\Python27\ArcGIS10.1\python.exe
0 Kudos