我有2個python腳本。Python - 導入模塊只有一次
satellite_utils.py
apply_errata.py
,我運行的腳本是:在satellite_utils.py
定義
python3.4 apply_errata.py
apply_errata.py
通話功能。
現在我正在使用模塊logging
來記錄我的消息。我只想導入一次,而不必在每個腳本中聲明它。
如果我定義apply_errata.py
logging
和參考在satellite_utils.py
發微博,我得到:
Traceback (most recent call last):
File "apply_errata.py", line 20, in <module>
satellite_utils.applyErrata(args.release, args.all, args.rollback)
File "/root/config-3.1.21/automated-os-patching/satellite_utils.py", line 34, in applyErrata
applyErrataOnSystem(system, release, automaticRollback, [erratum])
File "/root/config-3.1.21/automated-os-patching/satellite_utils.py", line 39, in applyErrataOnSystem
logging.warning('is when this event was logged.')
NameError: name 'logging' is not defined
什麼辦法可以避開everyfile import語句?
爲什麼你不想在每個文件中導入它? – bereal
如果你想使用一個模塊,你需要導入它。導入模塊並不意味着它被多次加載。這只是意味着你正在代碼中引用它。 – MrE
每個需要'logging'的模塊都應該'導入日誌記錄'。 – user2357112