F
Frantisek Fuka
This thing keeps bugging me. It's probably some basic misunderstanding
on my part but I am stumped. Let's say I have two Python files: file.py
and file2.py. Their contents is as follows:
file.py:
---------------------
import file2
def hello():
print "Hello"
file2.hello2()
file2.py:
---------------------
import file
def hello2():
print "Hello2"
file.hello()
Now, when I run "file.py", I get the following error:
Traceback (most recent call last):
File "file.py", line 1, in ?
import file2
File "/cygdrive/t/python/file2.py", line 1, in ?
import file
File "T:\python\file.py", line 4, in ?
file2.hello2()
AttributeError: 'module' object has no attribute 'hello2'
I think I grasp why I got this error (file2.hello2() is referenced
before its definition was "compiled" by Python) but I am puzzled as to
how to do this properly. Isn't it fairly common for two source files to
reference each other in this way? I know I can solve this by putting the
import statement after the def statement, but I have the similar problem
in much larger project, with many files and many includes and I'd like
to keep all my includes at the beginning of the file.
on my part but I am stumped. Let's say I have two Python files: file.py
and file2.py. Their contents is as follows:
file.py:
---------------------
import file2
def hello():
print "Hello"
file2.hello2()
file2.py:
---------------------
import file
def hello2():
print "Hello2"
file.hello()
Now, when I run "file.py", I get the following error:
Traceback (most recent call last):
File "file.py", line 1, in ?
import file2
File "/cygdrive/t/python/file2.py", line 1, in ?
import file
File "T:\python\file.py", line 4, in ?
file2.hello2()
AttributeError: 'module' object has no attribute 'hello2'
I think I grasp why I got this error (file2.hello2() is referenced
before its definition was "compiled" by Python) but I am puzzled as to
how to do this properly. Isn't it fairly common for two source files to
reference each other in this way? I know I can solve this by putting the
import statement after the def statement, but I have the similar problem
in much larger project, with many files and many includes and I'd like
to keep all my includes at the beginning of the file.