All things are completely different between PHP and Python, and there are many reasons.
But it seems that the recommended way to do things in python is from file import , but it seems like more to include libraries and other things?
Indeed, import statements are designed to import objects from another module into the current module. You can import all objects of the imported module into the current module:
import foo print foo.bar
or you can choose what you want from this module:
from foo import bar print bar
and even better, if you import a module twice, it will be imported only once:
>> import foo as foo1 >> import foo as foo2 >> foo1 is foo2 True
How do you separate your code from multiple files?
You need to think about your code ... This is called software design, and here are a few rules:
- you never write an algorithm at the module level; instead make it a function and call that function
- you never instantiate an object at the module level; you must embed it in a function and call that function
- If you need an object in several different functions, create a class and encapsulate this object in this class, and then use it in your functions associated with this class (so now they are called methods)
The only exception is when you want to run the program from the command line by adding:
if __name__ == "__main__":
at the end of the module. And my best advice would be to just call your first function right away:
if __name__ == "__main__": main()
Is this the only way to do this, to have one file with a whole set of function calls, and then import 15 other files?
This is not the only way to do this, but it is the best way to do it. You do all your algorithms in function and object libraries, and then import exactly what you need in other libraries, etc. This is how you create an entire reusable code universe and you should never reinvent the wheel! Therefore, forget about files and think about modules containing objects.
Finally, my best tip for you learning python is to unlearn every habit and use that you used when coding PHP, and learning all this differently. In the end, it can make you a better software engineer.