I have a bot that is based on a ready-made project, and now it's time to write my own. I decided to do the same import of plugins:
# содержимое /plugins/__init__.py import importlib import pkgutil __all__ = [] def import_plugins(package): if isinstance(package, str): package = importlib.import_module(package) for loader, name, is_pkg in pkgutil.walk_packages(package.__path__): full_name = package.__name__ + '.' + name module = importlib.import_module(full_name) for name in dir(module): if name[0] == "_": continue e = module.__getattribute__(name) if e in __all__: continue if True: __all__.append(e.__name__) globals()[e.__name__] = e if is_pkg: import_plugins(full_name) import_plugins(__name__) So, there is no problem running from PyCharm, but if you run from the command line, you will get an error:
Traceback (most recent call last):
File "Camelia.py", line 12, in
settings = Settings.Settings ()
File "C: \ Users \ ShadowOfLife \ PycharmProjects \ AeterrnoPy \ Settings.py", line 10, in __init__
self.inner_data = InnerDataPlugin ()
NameError: name 'InnerDataPlugin' is not defined
What am I doing wrong? Is it possible to force such an import to work from the command line?
Project structure:
/ Plugins
- / AeterrnoPy
- / Base
- / Data
-------- __init__.py
-------- InnerData.py # here is the InnerDataPlugin ()
- / ezpy
- __init__.py # here is the code to import
/ utils
Camelia.py # bot
Settings.py # here plugins are imported
And the contents of Settings:
from Plugins import * class Settings: __slots__ = ("inner_data", "plugins", "user_plugins") def __init__(self): self.plugins = () self.user_plugins = () self.inner_data = InnerDataPlugin() self.plugins = ( AntiFloodPlugin(), self.inner_data, PairPlugin("pair test", "pt"), TestingPlugin("test", "tst"), ) self.user_plugins = ( PairUP("pair test", "pt"), ) UPD: I apologize for using images, corrected
InnerDataPlugindefined? - Bogdan