I highly recommend checking out the official documentation or a tutorial to learn about the import
command.
You can split them into individual files and import them if they are all in the same directory without any problems.
This answer assumes your directory has the following structure:
.
| - file1.py
| - file2.py
| - ...etc
| - main.py
If you wish to nest your directories, see the other answer. Their answer covers how to use __init__.py
to take care of that.
If you have the classes defined in individual files, you only need to import standard libraries that are required for that script. For example, if CustomDataFrame
required RequiredModule1
and RequiredModule2
but not matplotlib
, you would write this in file1.py
.
file1.py
import RequiredModule1
import RequiredModule2
#etc
class CustomDataFrame():
code
You can either separate them out into individual files, or group modules together into a single file and import each of them.
file2.py
class CustomDataFrameItem():
code
class Program():
code
main.py
from file1 import CustomDataFrame
from file2 import CustomDataFrameItem, Program
if __name__ == "__main__":
p = Program()
p.run()
Note that you do not put the file extension .py
on the from fileX import xxx
. The .py
is inferred by the interpreter.