简体   繁体   中英

Python - import module once only

I have 2 python scripts.

satellite_utils.py
apply_errata.py

The script that I run is :

python3.4 apply_errata.py

apply_errata.py calls functions defined in satellite_utils.py .

Now I am using the module logging to log my messages. I want to import this only once rather than having to declare this in every script.

If i define logging in apply_errata.py and a reference is made to it in satellite_utils.py , I get :

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

Any way I can avoid an import statement in everyfile ?

You can use the logging instance in the satellite_utils like this:

# in apply_errata.py
import satellite_utils
satellite_utils.logger.info('hello there')

or

# in apply_errata.py
from satellite_utils import logger
logger.info('hi there')

This works because any name defined in a Python file is attached to the global scope of that file (in Python files are modules, so file == module), and is accessible to anyone.

It's import to point out that this is, shall we say, not the canonical and preferred way to do things. You're a grown-up, you can decide for yourself.

Why it's not bad to import modules multiple times: they get cached by Python in the sys.modules dict, so next time when you import you'll just get that cached copy. Python docs on sys.modules

You can do it by importing the necessary libraries in a script and then import everything in that script into another script using wildchar. By doing this you are not importing all of them again instead you are referencing them and they can be used in the second script as if you were using them in the friest script.

For example: 1. Script1.py

import logging
import something
.....
...
log_i=logging.info
log_d=logging.debug
  1. Script2.py

     from Script1 import * #import all in Script1 log_i("this is info log") log_d("this is debug log")#use the imported data

Here logging is imported in Script1 and I'm importing all from Script1 to Script2 which means all the libraries,variables,function definitions used in Script1 is accessible and modifiable from Script2. Hence I'm using logging directly without any declaration/assignment in Script2.

As per @anugrah's comment you can use __init__.py to initialise the modules in a directory so that they can be imported and used like the above method. So if you choose this method then it will be like

abc/__init__.py

abc/modules.py

import logging,os,sys
log_i=logging.info
log_d=logging.debug

Script1.py

from abc.modules import log_* #now I'm importing only log_i,log_d
log_i("this is info log")
log_d("this is debug log")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM