2015-11-15 13 views
3

我在导入相同程序包中的类时遇到了问题,它似乎不是循环依赖项问题。所以我现在很困惑。ImportError:无法导入名称(不是循环依赖项)

my-project/ 
    lexer.py 
    exceptions.py 

exceptions.py声明的异常,并希望利用它在lexer.py

exceptions.py:

class LexError(Exception): 
    def __init__(self, message, line): 
     self.message = message 
     self.line = line 

和lexer.py:

import re 
import sys 

from exceptions import LexError 
... 

它不应该是循环依赖,因为lexer.py是只有文件有import

谢谢!

+0

你可以尝试重命名'exceptions.py'到别的东西,看看是否有帮助吗? – Adeeb

+0

@Adeeb它的作品!但为什么?在这个[回购](https://github.com/miguelgrinberg/flasky/search?utf8=%E2%9C%93&q=ValidationError)中,你可以看到exceptions.py应该会导致一个ImportError !? – Rundis

+0

@Adeeb这[回购](https://github.com/miguelgrinberg/flasky/search?utf8=%E2%9C%93&q=ValidationError)来自[Flask Web Development]一书(http://shop.oreilly .com/product/0636920031116.do) – Rundis

回答

2

exceptions与内建模块exception冲突。

>>> import exceptions 
>>> exceptions.LexError 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
AttributeError: 'module' object has no attribute 'LexError' 
>>> from exceptions import LexError 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ImportError: cannot import name LexError 

使用不同的模块名称。

相关问题