Misunderstanding of Python imports
I am having a recurring problem in the way I program and I need help
restructuring the way I do things.
Warning: This is a long question. I want to include as much information
about the nature of the error as possible to exhaust possible causes of
it.
TL;DR Version: import/include order isn't letting my main file see
functions defined in other files. How can I improve my programming style
to prevent this later?
Long version: I'll use Python 2.7 as an example. Let's say I have a file,
main.py, and another file, foo.py. main.py has all my main code, and
foo.py has a class that contains data used in main.py. What I do is I
define the class's object in that same file, foo.py, right after the class
definition. I figure if I just instantiate the class there, I can simply
import that file into any other files that need it, and the object will
already exist to be used immediately. No mucking about with instantiating
the class at some point and worrying whether or not something before the
class's instantiation needs it before it's actually defined.
The problem is that this normally works, until I do that with another
class defined in another file. The interpreter can't find the object of
the second class, even if I import it once in main.py (therefore this does
not seem to be an issue of duplicate objects stemming from multiple
imports). So, in essence, main.py can see foo.py's class object, but not,
say, bar.py's object.
I normally set up a project so that I have one main file, and other files
that define data and the uses and handling for that data. The main file
calls one function or method that gets the ball rolling by calling other
functions and methods throughout the program. I import just the data for
the class whose method I'm calling, and in the file that has the data used
in the main file, I import all the other stuff that particular file needs.
This seems logical to me if I'm to make a memory-efficient program, but
there is obviously something wrong with the way I'm doing things, because
it never seems to work with bigger programs.
Here is a simple example in code of (a. how I set up my file hierarchies,
(b. how I set up my imports, and (c. an example of the types of errors I
get:
main.py:
import foo
import bar
print foo.bar
print bat.bar
foo.py:
class Foo:
def __init__(self):
bar = 5
foo = Foo()
bar.py:
class Bar:
def __init__(self):
bar = "Hello"
bat = Bar()
And here's the error I get when I run this program:
Traceback (most recent call last):
File "C:\Users\User\Desktop\main.py", line 4, in <module>
print foo.bar
AttributeError: 'module' object has no attribute 'bar'
When I change main.py to this:
from foo import *
from bar import *
print foo.bar
print bat.bar
it gets a different error:
Traceback (most recent call last):
File "C:\Users\User\Desktop\main.py", line 4, in <module>
print foo.bar
AttributeError: Foo instance has no attribute 'bar'
Am I misunderstanding how to use imports or what? Because if I step back
and make main.py like this:
from foo import *
from bar import *
foo = Foo()
bat = Bar()
print foo.bar
print bat.bar
while commenting out the instantiation of the classes in the other files,
I get the same error: Foo instance has no attribute 'bar'.
Why is this?
This doesn't just happen with Python, it happens to me with C++ too, so
this is why I suggest it may be my programming style that is an issue here
and not so much anything syntactical.
No comments:
Post a Comment