导入与文件同名的模块
importing module with same name as file
我想将日志记录 https://docs.python.org/3/library/logging.html 导入到名为 logging.py 的文档中。但是,当我尝试导入 logging.handlers 时,它失败了,因为我相信它是在文档中搜索处理程序函数,而不是从模块中导入。我该如何解决这个问题,以便它查找更高级别的日志记录而不是查看文件内部?
您可以通过从 python 路径中删除当前目录(sys.path 中的第一个目录)来完成此操作:
import sys
sys.path = sys.path[1:]
import logging
print dir(logging)
测试:
$ 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']
如果您使用的模块与标准库同名,您可以首先在路径中插入标准库包目录,使用 distutils.sysconfig.get_python_lib
定位目录,这样您仍然可以使用脚本目录中的任何包:
import sys
import distutils.sysconfig as sysconfig
sys.path.insert(0, sysconfig.get_python_lib(standard_lib=1))
import logging
print(logging.__file__)
输出:
$ python3 logging.py
/usr/lib/python3.4/logging/__init__.py
$ python logging.py
/usr/lib/python2.7/logging/__init__.pyc
如果你想将路径设置回正常,你可以 pop 并且导入仍然有效:
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)
输出:
$ 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://docs.python.org/3/library/logging.html 导入到名为 logging.py 的文档中。但是,当我尝试导入 logging.handlers 时,它失败了,因为我相信它是在文档中搜索处理程序函数,而不是从模块中导入。我该如何解决这个问题,以便它查找更高级别的日志记录而不是查看文件内部?
您可以通过从 python 路径中删除当前目录(sys.path 中的第一个目录)来完成此操作:
import sys
sys.path = sys.path[1:]
import logging
print dir(logging)
测试:
$ 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']
如果您使用的模块与标准库同名,您可以首先在路径中插入标准库包目录,使用 distutils.sysconfig.get_python_lib
定位目录,这样您仍然可以使用脚本目录中的任何包:
import sys
import distutils.sysconfig as sysconfig
sys.path.insert(0, sysconfig.get_python_lib(standard_lib=1))
import logging
print(logging.__file__)
输出:
$ python3 logging.py
/usr/lib/python3.4/logging/__init__.py
$ python logging.py
/usr/lib/python2.7/logging/__init__.pyc
如果你想将路径设置回正常,你可以 pop 并且导入仍然有效:
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)
输出:
$ 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']