开发者

Python project architecture

开发者 https://www.devze.com 2023-01-06 07:17 出处:网络
I\'m a java developer new to python. In java, you can access all classes in the same directory without having to import them.

I'm a java developer new to python. In java, you can access all classes in the same directory without having to import them.

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

I've tried various solutions, for example by importing everything in a file which I import everywhere. That works, but I have to type myClass = rootFolder.folder2.folder3.MyClass() each time I want to access a foreign class.

Could you show me an example for how a python architecture over several directories works? Do you really have to import all the classes you need in each file?

Imagine that I'm writing a web framework. Will the user开发者_StackOverflow中文版s of the framework have to import everything they need in their files?


Put everything into a folder (doesn't matter the name), and make sure that that folder has a file named __init__.py (the file can be empty).

Then you can add the following line to the top of your code:

from myfolder import *

That should give you access to everything defined in that folder without needing to give the prefix each time.

You can also have multiple depths of folders like this:

from folder1.folder2 import *

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

0

精彩评论

暂无评论...
验证码 换一张
取 消