0

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.

Le Phong Vu
  • 156
  • 2
  • 12

1 Answers1

0

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.

Zigsaz
  • 435
  • 3
  • 15
  • I tried with this code: --- import sys sys.path.append('usr/lib64/python2.7/') import logging as lg ---- But python still can not reference to logging of system. – Le Phong Vu Aug 12 '13 at 04:38
  • @VũNgâyThơ Did you make sure to import the system module, append the path, and _then_ import your module? – Zigsaz Aug 12 '13 at 04:50
  • I think have a little misunderstand here. I have a package logging/log.py In log.py I want to import logging (from usr/lib64/python2.7 ) But when I used import logging or try your way, it will references to my logging package. – Le Phong Vu Aug 12 '13 at 04:56
  • Sorry for my lack description – Le Phong Vu Aug 12 '13 at 05:06
  • Ah, I understand what you mean. Where are you placing your custom package? It seems it is appearing before the system modules in your path. You'll have to either move it outside your path entirely, or perhaps try something as described here: http://stackoverflow.com/questions/6031584/python-importing-from-builtin-library-when-module-with-same-name-exists – Zigsaz Aug 12 '13 at 05:08