如何指示python忽略导入

时间:2013-10-04 22:11:44

标签: python python-import python-module

我有一个私人模块,无论好坏,我打电话给time。 我有另一个模块,我称之为graph,它位于使用redis-py模块的同一目录中。

不幸的是,我的time模块与标准time模块完全不同,因此我收到以下错误:

$ /opt/stuff/bin/python /opt/stuff/lib/utils/graph.py
Traceback (most recent call last):
  File "/opt/stuff/lib/utils/graph.py", line 6, in <module>
    import redis
  File "/opt/stuff/lib/python2.6/site-packages/redis/__init__.py", line 1, in <module>
    from redis.client import Redis, StrictRedis
  File "/opt/stuff/lib/python2.6/site-packages/redis/client.py", line 6, in <module>
    import time as mod_time
  File "/opt/stuff/lib/utils/time.py", line 5, in <module>
    import pytz
  File "/usr/lib/python2.6/site-packages/pytz/__init__.py", line 32, in <module>
    from pkg_resources import resource_stream
  File "/opt/stuff/lib/python2.6/site-packages/pkg_resources.py", line 761, in <module>
    class Environment(object):
  File "/opt/stuff/lib/python2.6/site-packages/pkg_resources.py", line 764, in Environment
    def __init__(self, search_path=None, platform=get_supported_platform(), python=PY_MAJOR):
  File "/opt/stuff/lib/python2.6/site-packages/pkg_resources.py", line 140, in get_supported_platform
    plat = get_build_platform(); m = macosVersionString.match(plat)
  File "/opt/stuff/lib/python2.6/site-packages/pkg_resources.py", line 282, in get_build_platform
    from distutils.util import get_platform
  File "/opt/stuff/lib64/python2.6/distutils/__init__.py", line 25, in <module>
    from distutils import dist, sysconfig
  File "/usr/lib64/python2.6/distutils/dist.py", line 21, in <module>
    from distutils.fancy_getopt import FancyGetopt, translate_longopt
  File "/usr/lib64/python2.6/distutils/fancy_getopt.py", line 32, in <module>
    longopt_xlate = string.maketrans('-', '_')
AttributeError: 'module' object has no attribute 'maketrans'

删除我的time模块可以解决错误。有没有办法我可以破解我的graph.py基本上忽略我的time模块并使用系统?我尝试过使用from __future__ import absolute_import,但这似乎没有什么区别。

1 个答案:

答案 0 :(得分:1)

导入graph的{​​{1}}模块不是;这是time

您的redis.client 也是顶级模块。其他Python代码无法区分您的模块和标准库版本。

您可以更改time订单并确保首先找到标准库模块,但实际上您应该只重命名模块。很容易给它一个不同的名字。