PDA

View Full Version : [SOLVED] [Python] Calling functions from other files



ki4jgt
June 18th, 2011, 12:18 PM
I have a file with about 20 def statements. Is there anyway I can call these statements into another .py file? I don't want to import them into Python. I want to distribute the file with my program and call it into the program from the same folder.

simeon87
June 18th, 2011, 12:19 PM
You can do in the other file:


import modulename

and then call them with:


modulename.function1()

geirha
June 18th, 2011, 12:22 PM
What's wrong with importing it?


#foo.py
def hello():
print "Hello, World!"




>>> from foo import hello
>>> hello()
Hello, World!
>>>

ki4jgt
June 18th, 2011, 12:29 PM
I tried that all night :-( but with subtle differences. Now that I know how, nothing's wrong with that.

cgroza
June 18th, 2011, 05:13 PM
Do you have an __init__.py in the folder where the files are located?

ki4jgt
June 18th, 2011, 05:57 PM
Do you have an __init__.py in the folder where the files are located?

No, just two different files. Guessing I need to look into __init__.py. . . AAAAAAAHHHHH LOL, thanks for the info though, I'll look into that ASAP.