简体   繁体   中英

IronPython: import random fails in IronPython Interactive

This fails in IronPython Interactive inside Visual Studio 2010:

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

The same works fine in IronPython code in Visual Studio. It also works fine if I launch ipy.exe inside a Windows command prompt. What is the problem with IronPython Interactive and import? How do I fix Python Interactive so that it can import Python modules?

sys.path in IronPython Interactive gives this:

» import sys
» sys.path
['.',
 'C:\\PROGRAM FILES (X86)\\MICROSOFT VISUAL STUDIO 10.0\\COMMON7\\IDE\\EXTENSIONS\\MICROSOFT\\IRONSTUDIO\\0.4\\Lib',
 'C:\\PROGRAM FILES (X86)\\MICROSOFT VISUAL STUDIO 10.0\\COMMON7\\IDE\\EXTENSIONS\\MICROSOFT\\IRONSTUDIO\\0.4\\DLLs']

sys.path in ipy.exe on Windows command prompt gives this:

>>> import sys
>>> sys.path
['.',
 'C:\\Users\\MyName\\Desktop',
 'C:\\Program Files (x86)\\IronPython 2.7\\Lib',
 'C:\\Program Files (x86)\\IronPython 2.7\\DLLs',
 'C:\\Program Files (x86)\\IronPython 2.7',
 'C:\\Program Files (x86)\\IronPython 2.7\\lib\\site-packages']

I am using IronPython 2.7, Visual Studio 2010 and Windows 7 64-bit.

As you can see sys.path directories are different;
I suspect random module is implemented in IronPython.Modules.dll , then you should check if such dll is present in the paths of IronPython interactive.

If it's present, the problem is another and I don't know what could be...

otherwise in Ironpython interactive, before import random do:

sys.path.append(path)

with path being the folder of IronPython.Modules.dll (I guess 'C:\\Program Files (x86)\\IronPython 2.7') and it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM