importing module with same name as file [duplicate]

梦想与她 提交于 2020-06-12 05:03:54

问题


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?


回答1:


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']



回答2:


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']


来源:https://stackoverflow.com/questions/34000610/importing-module-with-same-name-as-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!