ist_lion
ist_lion

Reputation: 3209

Python Import Error - Cannot import name

Brand new to Python & JYthon. I'm going through a simple tutorial and am struggling with the basics and am hoping for some insight.

Created a PyDev project called 'PythonTest' In that I created a module called test (test.py) and the code looks like this

 class test():
     def __init__(self,name,number):
         self.name = name
         self.number = number
     def getName(self):
         return self.name
     def getNumber(self):
         return self.number

I then created a java project called pythonJava and in it created three classes.

ITest.java which looks like this

package com.foo.bar;

public interface ITest {
    public String getName();

    public String getNumber();

}

TestFactory.java which looks like this

package com.ngc.metro;

import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;

public class TestFactory {
    private final PyObject testClass;

    public TestFactory() {
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.exec("from test import test");
        testClass = interpreter.get("test");
    }

    public ITest create(String name, String number) {
        PyObject testObject = testClass.__call__(new PyString(name),
                new PyString(name), new PyString(number));

        return (ITest) testObject.__tojava__(ITest.class);
    }
}

And finally Main.java

public class Main {

private static void print(ITest testInterface) {
    System.out.println("Name: " + testInterface.getName());
    System.out.println("Number: " + testInterface.getNumber());
}

public static void main(String[] args) {
    TestFactory factory = new TestFactory();
    print(factory.create("BUILDING-A", "1"));
    print(factory.create("BUILDING-B", "2"));
    print(factory.create("BUILDING-C", "3"));
}

}

When I run Main.java I get the following error:

Exception in thread "main" Traceback (most recent call last): File "", line 1, in ImportError: cannot import name test

Can someone advise me on what I'm doing wrong? I was under the impression I needed two imports one for the module (test.py) and the one for the class "test"

EDIT 1:

To avoid the easy question of my sys.path I have the following from IDLE

Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 18 2013, 21:18:40) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. import sys print(sys.path) ['', 'C:\Python33\Lib\idlelib', 'C:\Python33', 'C:\Python33\Lib', 'C:\Python33\DLLs', 'C:\workspace\myProject\src', 'C:\Windows\system32\python33.zip', 'C:\Python33\lib\site-packages']

from test import test t = test.test(1,2) t.getName() 1

Upvotes: 0

Views: 2709

Answers (1)

Fabio Zadrozny
Fabio Zadrozny

Reputation: 25332

Actually, it seems a PYTHONPATH issue... as you're getting it from IDLE I can't say how things are actually in your Java env (and IDLE is using Python, but in your run you should be using Java+Jython -- in which case you're probably not using the proper sys.path for Jython -- at least I don't see any place having the PYTHONPATH defined in the code above to include the path to your .py files).

Also, if you're doing Java+Jython, see the notes on the end of: http://pydev.org/manual_101_project_conf2.html for configuring the project in PyDev.

Upvotes: 1

Related Questions