简体   繁体   中英

Python: How to distinguish logging of python and a custom package “logging”

Normaly, I used "import logging" to use logging of python.

If I created a package: "logging". In this package I created a module: log.py In this file, I used "import logging", python will auto reference to my package "logging", but I want to used logging of system (/usr/lib64/logging.py)

How I can distinguish them: logging (/usr/lib64/logging.py) and my package "logging"

I try as below:

from "usr/lib64/python2.7/logging.py" import logging

but it still references to my package logging.

First of all, I'm not sure that method of importing is fully supported and is against the PEP8 standard. The correct way (assuming you want to keep the package out of your path) is:

import sys
sys.path.append('path/to/custom/module/dir')
import logging

However, to then solve your issue you will want to "rename" the module on import by replacing that last line with:

import logging as foo #system logging
sys.path.append('path/to/custom/module/dir')
import logging as bar #your module

The easiest and cleanest solution though, would be to just rename your module.

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