Python project architecture

I am a java developer for python. In java, you can access all classes in one directory without having to import them.

I am trying to achieve the same behavior in python. Is it possible?

I tried various solutions, for example, importing everything into a file, which I import everywhere. This works, but I have to type myClass = rootFolder.folder2.folder3.MyClass() every time I want to access a foreign class.

Could you show me an example of how python architecture works on multiple directories? Do you really need to import all the classes you need in each file?

Imagine I'm writing a web framework. Will users of the framework import everything they need into their files?

+4
source share
1 answer

Put everything in a folder (the name is not important) and make sure that there is a file with the name __init __. Py in this folder (the file may be empty).

Then you can add the following line at the beginning of your code:

 from myfolder import * 

This should give you access to everything that is defined in this folder, without having to give a prefix every time.

You can also have several depths of such folders:

 from folder1.folder2 import * 

Let me know if this is what you were looking for.

+2
source

Source: https://habr.com/ru/post/1314553/


All Articles