Hey all,

What does it mean when you get an error that says ImportError: no module named main. I wasn't aware I was trying to import a module named main. I don't even have a module named main.

I'm using Jython and I'm reflecting on an instance of a python class in java. Would that throw the error?

In python I have this:

map.put("ball",[ballCollisionListener()])
map.put("brick",[brickCollisionListener()])
map.put("paddle",[paddleCollisionListener()])

where these are all types of listeners that are python classes and that are of the java type CollisionListener (a custom class I made up).

In java I have this:

for(EventListener l:myMap.get(i.getType()))
               {
                   Class c = l.getClass();
                   EventListener newL=null;
                   try
                   {
                       newL = (EventListener)c.newInstance();
                   }
                #more code
               }

Recommended Answers

All 5 Replies

It means that one of the modules which you're using, directly or indirectly, tries to import a module name 'main' and can't find it. Do you have a more complete exception traceback ?

For some reason it only gives me this:
Traceback (innermost last):
(no code object) at line 0
ImportError: no module named main

But if I change the java code to explicitly use the default constructor when creating a class (newInstances uses it anyway by default) I get this:
Caused by: Traceback (innermost last):
(no code object) at line 0
ImportError: no module named main

at org.python.core.Py.ImportError(Unknown Source)
at org.python.core.imp.import_first(Unknown Source)
at org.python.core.imp.import_name(Unknown Source)
at org.python.core.imp.importName(Unknown Source)
at org.python.core.Py.initProxy(Unknown Source)
at org.python.core.Py.initProxy(Unknown Source)
at org.python.proxies.main$ballCollisionListener$0.__initProxy__(Unknown Source)
at org.python.proxies.main$ballCollisionListener$0.<init>(Unknown Source)
... 7 more

If you look at the names of the objects, they are from org.python.proxies.main. So it seems that the java code gets the class from the python script and then tries to make the object but can't find the package it needs. Seems like the main module is the actual python file. It can't find itself?

I don't know Jython, but looking at this org.python.proxies.main$ballCollisionListener$0.<init>(Unknown Source) , it seems that java tries to execute the python call __main__.ballCollisionListener.__init__() . Are you sure that you imported the name ballCollisionListener the right way ?

I think what I need to see is a good tutorial on python reflection

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.