May-19-2021, 04:46 PM
(May-19-2021, 04:26 PM)Gribouillis Wrote: To understand how it works you need to have some basic notions of how import statements work. When it executes sayimport food_bot.price
, Python looks in the dictionarysys.modules
if it already contains a module namedfood_bot.price
. If it is not the case, a new empty module object is created an inserted immediately insys.modules
. Then Python starts executing in this module's namespace the code that in finds in__init__.py
. There it encounters the linefrom .pricing import Pricing
. It inserts immediately a module namedfood_bot.price.pricing
insys.modules
and it executes the code ofpricing.py
in that module's namespace. After that, this module contains the class named Pricing and Python can complete the import. At that point, the modulefood_bot.price
exists and contains the namePricing
.
Then Python runsfrom .item_pricing import ItemPricing
. This executes the code fromitem_pricing.py
in a new module's namespace. That code contains the linefrom . import Pricing
. The.
module here isfood_bot.price
which is already insys.modules
and already contains the symbolPricing
. Hence the line suceeds.
Thanks a lot!