Subscribe to this thread
Home - General / All posts - How do I import Python modules into Manifold 8?
ancimmar2 post(s)
#03-Feb-15 21:21

The question says it all, really. Manifold 8, with the IronPython 2.6 distribution.

I've gotten Manifold to at least be able to run IronPython scripts (courtesy ofhttp://www.georeference.org/Forum/t100925.11) by deleting the Scripts > Reference to IronMath.dll and changing the IronPython.dll reference to full file path.

However, despite being able to load up IronPython itself outside of Manifold and import, say, urllib2 just fine, I can't do the same in Manifold. Instead I get:

"Exception has been thrown by the target of an invocation. No module named urllib2" [sic]

At the same time, I can do import math and then junk like math.sqrt(), and if I don't import math then math.sqrt() throws an error. I can also import sys no problem. But I can't get urllib2 or cmd or whatever to import correctly.

These and the fact that Manifold knows the error suggests that the software should be able to import modules, but I don't know why it can't get urllib2 or cmd.

I've tried adding a reference to IronPython.Modules.dll in references, as well as a bunch of other DLLs that are in the IronPython 2.6 folder. No dice.

Thoughts?

Vaguely related/why I'm doing this: I'm hoping to use urllib2 to do some junk with APIs, so maybe I'll need the json module too.

tjhb
10,098 post(s)
#03-Feb-15 23:30

Try

# IronPython

import sys

# Application.MessageBox(repr(sys.path), 'Script') # initially just ['.']

sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')

import urllib2

# Application.MessageBox(repr(dir(urllib2)), 'Script')

# ...

tjhb
10,098 post(s)
#04-Feb-15 02:52

P.s. it's the same story for importing your own modules, whether .py or compiled to .dll, even if they are in (or under) the Manifold Config folder. Append to sys.path.

tjhb
10,098 post(s)
#04-Feb-15 03:12

...and if you don't need the whole sys namespace, just

from sys import path

path.append('C:\\Program Files\\Manifold System\\Config')

for example.

ancimmar2 post(s)
#04-Feb-15 14:34

Awesome! That was easy; I was worried the answer was going to be "well, you can't".

artlembo


3,417 post(s)
#04-Feb-15 15:02

can this work with regular python 2.6 or 2.7?

tjhb
10,098 post(s)
#04-Feb-15 19:46

It's built in to Python, so yes, anywhere you can use any flavour of Python.

I think double backslashes in the path are usually proper, though they don't seem to be necessary with IronPython (at least). (I sloppily left them out the first time above but put them in the second.)

It's easier than playing around with the PATH environment variable.

Python Tools for Visual Studio adds some obvious locations (such as \Lib) to the sys.path list by default, which is why you can just "import urllib2" (for example) from there out of the box.

Manifold User Community Use Agreement Copyright (C) 2007-2021 Manifold Software Limited. All rights reserved.