Importing class from another file

John Gordon gordon at
Tue Jan 22 20:47:34 CET 2013

In <mailman.830.1358883233.2939.python-list at> Kevin Holleran <kdawg44 at> writes:

> I have a class called My_Class in a subdir called Sub_Dir.

> in is the following

> class My_Class_Connector:
>     def __init__(self,un,pw,qs_srv=""):
>         self.username = un
>         self.password = pw

> Then I am trying to call from a script in the parent dir like this:

> from Sub_Dir.My_Class import *

> q_api = My_Class.My_Class_Connector(string1,string2)

Even if your import had worked, this would be wrong.  You're importing
everything from Sub_Dir.My_Class, so My_Class_Connector is in the current
namespace.  You don't need to add "My_Class." on the front (and in fact
it's an error to do so.)

> Traceback (most recent call last):
>   File "", line 1, in <module>
>     from Sub_Dir.My_Class import *
> ImportError: No module named Sub_Dir.My_Class

Is there a file named in Sub_Dir?  A directory must contain
that file in order to be considered a "module".  (If you don't know what
to put in the file, just leave it empty.)

John Gordon                   A is for Amy, who fell down the stairs
gordon at              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

More information about the Python-list mailing list