Search code examples
pythonimportdirectory-structure

Importing libraries for projects


I have a python project with multiple classes and files.

This is my folder structure:

main.py
directory
  |_ parent_class.py
  |_sub_directory_1
     |_child_class_1.py
  |_sub_directory_2
     |_child_class_2.py

I am using the same imports like pandas and numpy e.g. in all files. Is there a way to import those a single time so my code is cleaner? Many Thanks in advance.


Solution

  • There is a way to "refacto" your imports inside a parent module and then import * from parent_module.

    However I would not recommend such practice as the more your project grows the more likely you are to produce circular imports !

    I would say that the best practice is to only import your packages/modules where you really need them. If you want a cleaner code base, I can suggest you tools such as isort that will automatically format your imports !