无法在sys模块之前导入time。Python

0 投票
1 回答
3198 浏览
提问于 2025-04-19 09:17

我在打开ipython笔记本的时候遇到了一些问题,似乎在导入时间模块时出错了:

//anaconda/python.app/Contents/lib/python2.7/logging/__init__.py:26: RuntimeWarning: import threads:        
cannot import name time
(ImportError: cannot import name time)
import sys, os, time, cStringIO, traceback, warnings, weakref, collections
Traceback (most recent call last):
File "//anaconda/bin/ipython", line 4, in <module>
from IPython import start_ipython
File "//anaconda/lib/python2.7/site-packages/IPython/__init__.py", line 45, in <module>
from .config.loader import Config
File "//anaconda/lib/python2.7/site-packages/IPython/config/__init__.py", line 16, in <module>
from .application import *
File "//anaconda/lib/python2.7/site-packages/IPython/config/application.py", line 23, in <module>
import logging
File "//anaconda/python.app/Contents/lib/python2.7/logging/__init__.py", line 95, in <module>
_startTime = time.time()
AttributeError: 'module' object has no attribute 'time'

奇怪的是(对我来说),我在导入时间模块之前,必须先导入sys模块,否则就加载不进去!

Python 2.7.8 |Anaconda 2.0.1 (x86_64)| (default, Jul  2 2014, 15:36:00) 
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> import time
__main__:1: RuntimeWarning: import threads: cannot import name time
(ImportError: cannot import name time)
>>> import sys
>>> import time
>>> 

这个问题发生在我清理了我的.bash_profile和.profile文件,删除了那些注释的行之后。 如果有人能帮我解释一下,我会非常感激。

谢谢。

1 个回答

4

你有一个本地文件叫做 time.py,它和内置的时间类型冲突了。你需要把这个文件删除或者改个名字。你可以通过输入以下命令来查看这个冲突模块的文件名:

import time
print time

在你第二次导入的时候,你收到了一个警告,而不是错误。虽然 import time 这一行是成功的,但它触发了代码,导致错误的 time 模块被加载。再次导入的时候,它使用了已经导入的模块对象,即使它重新执行了导入,警告通常只会记录一次。

撰写回答