Reputation: 1691
I'm new to python so this is probably something I've just missed... I'm just trying to run a file which calls another file.
I have a file like myfile.py:
#!/usr/bin/python
import another_file
things = ... """ some code """
def mystuff(text, th=things):
return another_def(text, th)
'another_file
' can compile/run fine by itself and has the def 'another_def
' and the variable 'th
' (These are just example names...)
So I run python
from the command line and then try:
>>> import myfile
>>> t = myfile.mystuff('some text')
and I get the error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "myfile.py", line 18, in mystuff
return another_def(text, th)
TypeError: 'module' object is not callable
I tried import another_file
even though it is in myfile.py
but that didn't seem to make any difference.
If it makes any difference, I tried:
print myfile
<module 'myfile' from 'myfile.py'>
print myfile.mystuff
<function mystuff at 0x7fcf178d0320>
so I assume that if it can find file and function, the problem is how it tries to call the other file.... maybe. Any help appreciated!
Upvotes: 1
Views: 9439
Reputation: 14360
You can do that using the so called wild import:
from other_file import *
And that way you can access all objects and functions in that file. Unless ofcourse, you have defined the
__all__
list wich restrict what can be exported.
Example:
#some_file.py
a = 3; b = 4
__all__ = ['a']
now with the wild import:
from some_file import *
you only see 'a'
Upvotes: 1
Reputation: 59974
I'm not exactly sure why you're getting a TypeError
(there's probably more to it than what you're showing), but if you want to access functions from another_file
, then you should do:
return another_file.another_def(text, th)
Upvotes: 3