tags:

views:

431

answers:

3

I have some types in a C# library I wrote, e.g.:

namespace SprocGenerator.Generators
{
    public class DeleteGenerator : GeneratorBase
    {
     public DeleteGenerator(string databaseName, string tableName) : base(databaseName, tableName)

I want to use them in an IronPython script:

import clr
import sys

clr.AddReferenceToFile("SprocGenerator.dll")
# problem happens here:
from SprocGenerator.Generators import *

generator = DeleteGenerator("a", "b")

When the line below the comment happens, I get:

ImportError: No module named Generators

I have verified that the file I am loading is what I expect by renaming it and verifying the script throws an error when trying to load the assembly. I have verified the namespace is in the assembly via Reflector. I have also tried specifying a fully-qualified classname to work around my import issue, e.g.

 generator = SprocGenerator.Generators.DeleteGenerator("a", "b")

But I get:

 NameError: name 'SprocGenerator' is not defined

Even if I have this in C#:

namespace SprocGenerator
{
    public static class GeneratorHelper
    {
     public static string GetTableAlias(string tableName)

And this in IP:

import clr
import sys
from System import *

clr.AddReferenceToFile("SprocGenerator.dll")
from SprocGenerator import *

print "helper = " + GeneratorHelper.GetTableAlias("companyBranch")

I get this error:

 NameError: global name 'GeneratorHelper' is not defined

What am I doing wrong?

+1  A: 

Check your namespaces. The fact that it complains:

ImportError: No module named Generators

instead of:

ImportError: No module named SprocGenerator.Generators

tells us that it found the SprocGenerator namespace. Is there a misspelling either in C# or Python in the inner namespace, Generators?

Ray Vernagus
Does the Default Namespace setting for the assembly maybe have some effect on my issue? It's set to the default: "SprocGenerator"
Josh Kodroff
I don't believe so. The default namespace just tells Visual Studio how to generate new code files. Either way, Reflector would show you the namespace where the class is found. I replicated your example exactly and I only get an error when I change the namespaces to be different than what you show. =/
Ray Vernagus
IronPython 2.0.3?
Josh Kodroff
I'm using 2.6 which is in RC2 now. Would you be able to update?
Ray Vernagus
Updated for 2.6 error message: I can't even get a static method on a class in the default namespace to work.
Josh Kodroff
+2  A: 

Could you be picking up the DLL from a different location then you're expecting? AddReferenceToFile will search sys.path and load the first file it finds which matches that filename. Depending on where you expect to find the DLL and where it may exist earlier on the path you could be getting a version which you've compiled earlier. You can also do:

dir(clr.LoadAssemblyFromFile('SprocGenerator.dll'))

to see what types exist in the DLL which you are actually getting back or:

clr.LoadAssemblyFromFile('test.dll').CodeBase

to see where the file is actually being loaded from.

Dino Viehland
A: 

Place the assembly in one of the locations specified in sys.path. On my machine:

['C:\Windows\system32', 'C:\Program Files (x86)\IronPython 2.6\Lib', 'C:\Program Files (x86)\IronPython 2.6\DLLs', 'C:\Program Files (x86)\IronPython 2.6', 'C:\Program Files (x86)\IronPython 2.6\lib\site-packages']

JulianM