12

I'll prefix this question with: No, Setting IRONPYTHONPATH is not the answer.

Anyway...

I was planning on using IronPython as a replacement for Powershell for a project, but I've been stumped before I've even started.

The very first thing I tried to do was use os.path, resulting in:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named os

After messing around I finally discovered I could use the standard library by adding it manually to the path:

import sys
sys.path.append(r"C:\Program Files\IronPython 2.7\Lib")
import os

However, this is a daft idea. Hard coding the path to the python library inside my scripts is a 100% guaranteed way of making them not work at some point.

I discovered this almost immediately when I tried to use the script on a windows 7 machine and the path was slightly different ('Program Files (x86)').

So, a couple of questions here:

1) Why is it so hard to use the standard library? At the very least I would have thought the interactive prompt in VS and basic ipy.exe would have this.

2) How can I determine the directory that iron python is installed in regardless of the system I'm using? (IronPython installer setting a var perhaps?)

Just a note here; yes, I have seen some other posts saying "set your IRONPYTHONPATH". This in unhelpful. If I have a blank machine that means I have to:

1) Install IronPython

2) Run some crazy powershell script to search out where-ever-the-heck the standard library was installed and set a global IRONPYTHONPATH variable to it.

3) Run python scripts

I'm looking for a better way.

--

Edit:

The fact I'm using this to do powershell like things is basically irrelevant, but I'm trying to achieve something like:

import clr
from System.Management.Automation import RunspaceInvoke
import os

scriptRoot = os.getcwd()
runSpace = RunspaceInvoke()
cmdPath64 = os.join(scriptRoot, "..\java\...")
cmdPath32 = os.join(scriptRoot, "..\java\...")
proc = runSpace.Invoke("Get-WmiObject Win32_Processor ... ")
if proc.AddressWidth == 32:
  runSpace.Invoke(cmdPath32)
else:
  runSpace.Invoke(cmdPath64)
Doug
  • 32,844
  • 38
  • 166
  • 222
  • Could you show an example script of what you would be trying to do and how you intend to use it? I'm not sure I understand what the problem is. – Jeff Mercado Aug 10 '11 at 03:42
  • see edit above. This is basically irrelevant to my question though. – Doug Aug 10 '11 at 04:14

3 Answers3

2

I find that for ensuring that everything works for non-developer third parties, it's usually better to use pyc.py to create DLL's and and executable. I routinely create a DLL of the python standard modules and reference that in code. See my previous answer at this question IronPython: EXE compiled using pyc.py cannot import module "os"

Community
  • 1
  • 1
WombatPM
  • 2,561
  • 2
  • 22
  • 22
  • That doesn't quite answer my question, but it _does_ work, and its by far the best solution I've found so far. thanks! – Doug Aug 13 '11 at 02:12
1

It's a bit workaroundish but, given that the LIB directory of ironpython is installed under the x86 program files folder in 64bit systems and on the usual program files path on 32bit systems, you could do in this way:

import sys
import System
if System.IntPtr.Size * 8 == 32: # detect if we are running on 32bit process
    sys.path.append(System.Environment.GetEnvironmentVariable("ProgramFiles") + "\IronPython 2.7\Lib")
else:
    sys.path.append(System.Environment.GetEnvironmentVariable("ProgramFiles(x86)") + "\IronPython 2.7\Lib")

import os # it works !!

Here we use %ProgramFiles% and %ProgramFiles(x86)% to determine the path where IronPython is installed.

Quoting wikipedia about %ProgramFiles% variable (link):

%ProgramFiles%

This variable points to Program Files directory, which stores all the installed program of Windows and others. The default on English-language systems is C:\Program Files. In 64-bit editions of Windows (XP, 2003, Vista), there are also %ProgramFiles(x86)% which defaults to C:\Program Files (x86) and %ProgramW6432% which defaults to C:\Program Files. The %ProgramFiles% itself depends on whether the process requesting the environment variable is itself 32-bit or 64-bit (this is caused by Windows-on-Windows 64-bit redirection).

digEmAll
  • 56,430
  • 9
  • 115
  • 140
  • I had compiled my script using pyc.py but during execution, I kept getting this: Unhandled Exception: IronPython.Runtime.Exceptions.ImportException: No module named os. However, once I added sys.path.append(System.Environment.GetEnvironmentVariable("ProgramFiles(x86)") + "\IronPython 2.7\Lib") it worked! – Gezim Jul 31 '12 at 16:43
1

This is very odd, because if you run the the IronPython installer, and then run C:\Program Files\IronPython 2.7\ipy.exe or C:\Program Files (x86)\IronPython 2.7\ipy.exe, you shouldn't need to do anything to have the stdlib available.

My guess is that you have more than one IronPython and you're running the wrong one, but only because I can't think of another reason this would happen. It's supposed to Just Work.

Jeff Hardy
  • 7,632
  • 24
  • 24
  • I have the same problem: if I run a script from a VS 2010 C# project containing the line "import os" I get the same error; it's fixed if I add the Lib path manually. IronPython.dll in the projects reference is the 2.7. – digEmAll Aug 10 '11 at 21:13
  • 1
    I would love it to pieces if it Just Worked. :/ (and no, I don't have multiple copies of iron python). – Doug Aug 11 '11 at 00:29