[英]how can I import all python classes in a folder?
This is the current structure of my project.这是我项目的当前结构。
cities
newyork.py
london.py
berlin.py
main.py
requirements.txt
Each of the city files contains a class. For instance Class Newyork:
is defined in newyork.py
每个城市文件都包含一个 class。例如
Class Newyork:
在newyork.py
中定义
In my main.py
I am importing all the classes for use in this manner:在我的
main.py
,我导入了所有以这种方式使用的类:
from cities.newyork import NewYork
from cities.london import London
from cities.berlin import Berlin
What I would like: To have something like this我想要什么:拥有这样的东西
from cities.* import NewYork, London, Berlin.
Is this possible in python?这在 python 中可能吗?
Add the following to cities/__init__.py
(creating the file if necessary):将以下内容添加到
cities/__init__.py
(必要时创建文件):
from newyork import NewYork
from london import London
from berlin import Berlin
Then you can write然后你可以写
from cities import NewYork, London, Berlin
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.