This project has moved. For the latest updates, please go here.
2

Closed

importing some modules (e.g.'import random') fails in IronPython Interactive window

description

At the IronPython Interactive prompt we get
 
» import sys
» import random
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named random
 
even though 'random' is one of the options offered by intellisense. This works fine when run from the IronPython Console or as an Visual Studio project
 
I think the problem is that the sys.path points to non-existent Lib and DLLs directories when in IronPythonInteractive:
» print sys.path
['.', 'C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\IronPython Tools for Visual Studio\0.4\Lib', 'C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\IronPython Tools for Visual Studio\0.4\DLLs']
 
running 'import sys / print sys.path' as Visual Studio project we get:
 
['c:\documents and settings\??????????\my documents\visual studio 2010\Proje
cts\ConsoleApplication1\ConsoleApplication1', '.', 'c:\documents and settings
\??????????\my documents\visual studio 2010\Projects\ConsoleApplication1\Co
nsoleApplication1', 'C:\Program Files\IronPython 2.7\Lib', 'C:\Program Files
\IronPython 2.7\DLLs', 'C:\Program Files\IronPython 2.7', 'C:\Program Files
\IronPython 2.7\lib\site-packages']
 
and running it from the IronPython Console we get pretty much the same (an
[ '.', '?????', 'C:\Program Files\IronPython 2.7\Lib', 'C:\Program Files
\IronPython 2.7\DLLs', 'C:\Program Files\IronPython 2.7', 'C:\Program Files
\IronPython 2.7\lib\site-packages']
 
 
Presumably the solution is either to change the sys.path or copy the Lib and DLLs directories to the 'IronPython Tool for Visual Studio\0.4' directory
Closed Aug 1, 2011 at 12:41 PM by slide_o_mix
IronPython Tools has been deprecated in favor of http://pytools.codeplex.com/ please try that toolset and file issues on the issue tracker there.

comments

DanielJennings wrote Oct 21, 2010 at 9:47 PM

Works fine on my machine:
import sys
import random

print sys.path
['.', 'C:\Program Files (x86)\IronPython 2.7', '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']

freen wrote Oct 22, 2010 at 11:25 AM

(i) Glad it works on your machine Daniel ;-), perhaps theproblem only affects some machines/installs. As a quick aside, are you sure you were using IronPython Interactive (ie. the Visual Studio addin) rather than IronPython Console? I only ask because I get a prompt of » in the IronPython Interactive window, not the >>> as per your sample.

(ii) Not sure that I would class this as a low impact issue (and I can't see how I can change the proposed priority). For affected installations, IronPython Interactive is installed in a broken state and it's not obvious how to change the value the sys.path defaults to. Nothing like working out of the box. Not to say that the problem might be very rare (or perhaps only restricted to just my machine).

zdtiger wrote Jan 23, 2011 at 7:10 AM

On my installation of 2.7 beta1, any module in the Lib and DLLs does not work (including import modules, such as os), because the ironpython installation path was not included in sys.path. This is not limited to the interactive console in visual studio, intellisence does not work for them neither.

» import sys
» sys.path
['.', 'C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO 10.0\COMMON7\IDE\EXTENSIONS\MICROSOFT\IRONSTUDIO\0.4\Lib', 'C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO 10.0\COMMON7\IDE\EXTENSIONS\MICROSOFT\IRONSTUDIO\0.4\DLLs']
»
Notice that the installation paths "C:\Program Files\IronPython 2.7[Lib|DLLs]" are not shown.

rjnienaber wrote Jan 28, 2011 at 8:25 PM

(Email excerpt from Dino Viehland):

I guess we're just setting the current working directory so that you can import
things but we still don't include the standard lib in the path. So the "." part is reasonable
now but we're picking up Lib/DLLs from where we have our RemoteScriptFactory
class.

We probably need to figure out where ipy.exe is installed and call SetSearchPaths
on the remote script engine in RemotePythonVsEvaluator.Initialize.

PythonRuntimeHost.GetPythonInstallDir() already has the code for finding IronPython
so it should be pretty easy to fix if anyone wants to give it a try.

(End email excerpt)

When dealing with the '.' as a path in sys.path, this issue needs to be taken into account: http://ironpython.codeplex.com/workitem/28235

wrote Mar 7, 2011 at 5:34 PM

wrote Mar 12, 2011 at 4:21 PM

wrote Mar 13, 2011 at 3:59 AM

demob wrote Mar 26, 2011 at 12:40 AM

i'm running windows 7 32bit.

it works fine on my machine

IronPython 2.7 (2.7.0.40) on .NET 4.0.30319.1
Type "help", "copyright", "credits" or "license" for more information.
import sys
import random
sys.path
['.', 'C:\Program Files\IronPython 2.7', 'C:\Program Files\IronPytho
.7\DLLs', 'C:\Program Files\IronPython 2.7\lib\site-packages']

freen wrote Mar 26, 2011 at 9:59 AM

demob, as I wrote to Daniel on Oct 22 2010:

Glad it works on your machine Daniel ;-), perhaps the problem only affects some machines/installs. As a quick aside, are you sure you were using IronPython Interactive (ie. the Visual Studio addin) rather than IronPython Console? I only ask because I get a prompt of » in the IronPython Interactive window, not the >>> as per your sample.

demob wrote Mar 26, 2011 at 11:11 PM

you are right freen. It only works when i do this:

» import sys
» sys.path.append('C:\Program Files\IronPython 2.7\Lib')
» sys.path
['.', 'C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO 10.0\COMMON7\IDE\EXTENSIONS\MICROSOFT\IRONSTUDIO\0.4\Lib', 'C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO 10.0\COMMON7\IDE\EXTENSIONS\MICROSOFT\IRONSTUDIO\0.4\DLLs', 'C:\Program Files\IronPython 2.7\Lib']
» import random
» random.randint(2,9)
3
»

wrote Mar 31, 2011 at 9:30 AM

ashwinn wrote May 21, 2011 at 5:28 AM

I am having this same problem with IronPython 2.7 and Visual Studio 2010. The problem occurs both with Windows 7 32-bit and 64-bit. The sys.path in the IronPython Interactive window is:
['.', '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']

I am able to import random, both in IronPython code inside Visual Studio and if I run ipy.exe inside a Command prompt. The sys.path when in a ipy.exe command prompt is:
['.', 'C:\Users\MyName\Desktop', 'C:\Program Files (x86)\IronPython 2.7\Lib', 'C:\Program Files (x86)\IronPython
s (x86)\IronPython 2.7', 'C:\Program Files (x86)\IronPython 2.7\lib\site-packages']

slide_o_mix wrote Jul 29, 2011 at 11:56 PM

When you say interactive window, do you mean the IronPython tools?

freen wrote Jul 30, 2011 at 1:31 AM

slide_o_mix : The Component field on this issue is set to "IPyTools" (which may not be easily visible to you), so in short: yes I do mean the interactive prompt you get into Visual Studio when you install the the IronPython Tools.

Perhaps this issue should now be migrated (together with the other issues for IPyTools) as "IronPython" and "Python Tools for Visual Studio" now seem to be organised as separate projects on codeplex.

wrote Aug 1, 2011 at 12:41 PM

wrote Feb 22, 2013 at 1:09 AM

wrote May 16, 2013 at 12:03 PM