I am currently doing a python tutorial, but they use IDLE, and I opted to use the interpreter on terminal. So I had to find out how to import a module I created. At first I tried
import my_file
then I tried calling the function inside the module by itself, and it failed. I looked around and doing
my_file.function
works. I am very confused why this needs to be done if it was imported. Also, is there a way around it so that I can just call the function? Can anyone point me in the right direction. Thanks in advance.
If you wanted to use
my_file.functionby just callingfunction, try using thefromkeyword.Instead of
import my_filetryfrom my_file import *.You can also do this to only import parts of a module like so :
from my_file import function1, function2, class1To avoid clashes in names, you can import things with a different name:
from my_file import function as awesomePythonFunctionEDIT:
Be careful with this, if you import two modules (
myfile, myfile2) that both have the samefunctioninside,functionwill will point to thefunctionin whatever module you imported last. This could make interesting things happen if you are unaware of it.