[Tutor] confusion about imports
Chris Hare
chare at labr.net
Mon Jul 9 20:33:56 CEST 2012
On Jul 9, 2012, at 12:42 PM, Walter Prins wrote:
> Hi Chris
>
>> So, I have to admit, imports have me really confused. I am trying to break apart a 10,000+ line single file into various files, one for each class, and one containing a whole bunch of functions which are used by a lot of classes. Some of those functions use calls to methods in a Class. Even though the Class has been imported, I get a nameError where trying to use the class. I have read about Classes and packages and modules, but import just has me confused.
>
> How did you import the class? Or did you perhaps not import the Class
> itself, but rather the module containing the class?
I am doing import NAME where name is not only the name of the class, but also the name of the file containing the class. The only reasons I am trying to break up the file is because it is getting to difficult to find stuff. Some of the content hasn't change while other parts are still in flux a lot. I figured that splitting it into the various files will make it easier to edit.
so I think I have figured out the problem based upon the resources you specified - the first one helped a bunch.
I was using
import functions
import os
import db
when everything was all in one file, that worked just fine. Now, with it all split up, once I changed
r = DbPath()
to
r = functions.DbPath()
things seems to work now. I hope this is it!!!
Now, I have a bunch of smaller, more manageable files instead of trying to edit one ginormous one :-)
Thanks!
>
> Read this article which explains a bit about Python namespaces:
> http://is.gd/e8PAZW (but I note there's a bit of conflation of
> "class" and "class instance" going on at the end.)
>
> Also read this page from the Python documentation, section "Python
> Scopes and Namespaces": http://docs.python.org/tutorial/classes.html
>
> If a class is defined in a module (e.g. in the namespace of the
> module), and you "import module" the module into the your current
> namespace, then from within the current namespace you can access the
> class with "module.Class". If however you import the class itself,
> e.g. "from module import Class", into your current namespace, then the
> Class itself is directly part of your local namespace and you must
> therefore access it unqualified as just "Class".
Thanks for the links -- yep - it helped, although I haven't solved my specific problem yet.
>
> HTH,
>
> Walter
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list