I want to import logging https://docs.python.org/3/library/logging.html into a document named logging.py . When I try to import logging.handlers though, it fails because I believe it's searching the document for a handlers function, instead of importing from the module. How can I fix this so it will look for the higher level logging instead of looking inside the file?
            Asked
            
        
        
            Active
            
        
            Viewed 4,371 times
        
    5
            
            
        - 
                    Are you using python 2 or 3? – bgusach Nov 30 '15 at 14:14
- 
                    Any reason why you can't rename the locally made `logging.py`? – Anshul Goyal Nov 30 '15 at 14:15
- 
                    python 3, working on a project so I wanted to find a solution rather than get everyone to rename the file – Rob Nov 30 '15 at 14:16
- 
                    I would totally go for changing the name to `logger.py` or whatever. Anyway, theoretically, in py3 or in py2 (with absolute import declaration), I guess it should work as long as the `logging.py` is not in the same path as the main script. – bgusach Nov 30 '15 at 14:18
- 
                    @en_Knight, I tried it on my python 2.7 interpreter and `logging.py` imported itself again. – bgusach Nov 30 '15 at 14:27
2 Answers
2
            You can do it by removing current directory (first in sys.path) from python path:
import sys
sys.path = sys.path[1:]
import logging
print dir(logging)
test:
$ python logging.py 
['BASIC_FORMAT', 'BufferingFormatter', 'CRITICAL', 'DEBUG', 'ERROR',
 'FATAL', 'FileHandler', 'Filter', 'Filterer', 'Formatter', 'Handler',
 'INFO', 'LogRecord', 'Logger', 'LoggerAdapter', 'Manager', 'NOTSET', 
 'NullHandler', 'PlaceHolder', 'RootLogger', 'StreamHandler', 'WARN',
 'WARNING', '__all__', '__author__', '__builtins__', '__date__',
 '__doc__', '__file__', '__name__', '__package__', '__path__',
 '__status__', '__version__', '_acquireLock', '_addHandlerRef',
 '_checkLevel', '_defaultFormatter', '_handlerList', '_handlers',
 '_levelNames', '_lock', '_loggerClass', '_releaseLock',
 '_removeHandlerRef', '_showwarning', '_srcfile', '_startTime',
 '_unicode', '_warnings_showwarning', 'addLevelName', 'atexit',
 'basicConfig', 'cStringIO', 'captureWarnings', 'codecs', 'critical',
 'currentframe', 'debug', 'disable', 'error', 'exception', 'fatal',
 'getLevelName', 'getLogger', 'getLoggerClass', 'info', 'log',
 'logMultiprocessing', 'logProcesses', 'logThreads', 'makeLogRecord',
 'os', 'raiseExceptions', 'root', 'setLoggerClass', 'shutdown', 'sys',
 'thread', 'threading', 'time', 'traceback', 'warn', 'warning',
 'warnings', 'weakref']
 
    
    
        ndpu
        
- 22,225
- 6
- 54
- 69
1
            
            
        If you are using a module with the same name as a standard lib you could insert your standard lib packages directory first in your path using distutils.sysconfig.get_python_lib to locate the directory, that will still enable you to use the any packages in your scripts directory:
import sys
import distutils.sysconfig as sysconfig
sys.path.insert(0, sysconfig.get_python_lib(standard_lib=1))
import logging
print(logging.__file__)
Output:
$ python3 logging.py 
/usr/lib/python3.4/logging/__init__.py
$ python logging.py 
/usr/lib/python2.7/logging/__init__.pyc
If you want to set the path back to normal you can pop and the import will still work:
import sys
import distutils.sysconfig as sysconfig
sys.path.insert(0, sysconfig.get_python_lib(standard_lib=1))
print(sys.path)
import logging
sys.path.pop(0)
print("")
print(logging.__file__)
print("")
print(sys.path)
Output:
$ python3 logging.py 
['/usr/lib/python3.4', '/home/padraic', '/home/padraic/mymods', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/home/padraic/.local/lib/python3.4/site-packages', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.4/dist-packages']
/usr/lib/python3.4/logging/__init__.py
['/home/padraic', '/home/padraic/mymods', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/home/padraic/.local/lib/python3.4/site-packages', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.4/dist-packages']
 
    
    
        Padraic Cunningham
        
- 176,452
- 29
- 245
- 321
