所以我有一個應用程序使用pymysql
,這是純粹的Python mysql客戶端實現。在我進入我的回覆之前,我想強調一個事實,即我不打算使用不同的mysql驅動程序。模塊如何從一個導入中可見並且從另一個導入中不可見?
我有一個模塊實現了MySQL支持的數據結構。該模塊的要點如下:
import pymysql
class Whatever:
def __init__(self):
# Debug statement
print dir(pymysql)
# use the cursors submodule
self.conn = pymysql.connect(... , cursorclass=pymysql.cursors.DictCursor)
當我在我的測試文件中導入這一切,一切都很好。以下是print
聲明的輸出。我特別提請您注意光標模塊:
['BINARY', 'Binary', 'Connect', 'Connection', 'DATE', 'DATETIME', 'DBAPISet', 'DataError',
'DatabaseError', 'Date', 'DateFromTicks', 'Error', 'FIELD_TYPE', 'IntegrityError',
'InterfaceError', 'InternalError', 'MySQLError', 'NULL', 'NUMBER', 'NotSupportedError',
'OperationalError', 'ProgrammingError', 'ROWID', 'STRING', 'TIME', 'TIMESTAMP', 'Time',
'TimeFromTicks', 'Timestamp', 'TimestampFromTicks', 'VERSION', 'Warning', '__all__',
'__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__',
'__version__', 'apilevel', 'charset', 'connect', 'connections', 'constants', 'converters',
'cursors', 'err', 'escape_dict', 'escape_sequence', 'escape_string', 'get_client_info',
'install_as_MySQLdb', 'paramstyle', 'sys', 'thread_safe', 'threadsafety', 'times', 'util',
'version_info']
當我導入從我的主文件的模塊,我得到一個AttributeError
:
Traceback (most recent call last):
File "xxx.py", line 72, in <module>
passwd='', db='test_db')
File "yyy.py", line 26, in __init__
passwd=passwd, db=db, cursorclass=pymysql.cursors.DictCursor)
AttributeError: 'module' object has no attribute 'cursors'
的dir
打印的輸出如下:
['BINARY', 'Binary', 'Connect', 'Connection', 'DATE', 'DATETIME', 'DBAPISet',
'DataError', 'DatabaseError', 'Date', 'DateFromTicks', 'Error', 'FIELD_TYPE',
'IntegrityError', 'InterfaceError', 'InternalError', 'MySQLError', 'NULL', 'NUMBER',
'NotSupportedError', 'OperationalError', 'ProgrammingError', 'ROWID', 'STRING', 'TIME',
'TIMESTAMP', 'Time', 'TimeFromTicks', 'Timestamp', 'TimestampFromTicks', 'VERSION',
'Warning', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__',
'__path__', '__version__', 'apilevel', 'charset', 'connect', 'constants', 'converters',
'err', 'escape_dict', 'escape_sequence', 'escape_string', 'get_client_info',
'install_as_MySQLdb', 'paramstyle', 'sys', 'thread_safe', 'threadsafety', 'times',
'version_info']
值得注意的是,cursors
缺席。檢查pymysql.__file__
是在兩種情況下是相同的,輸出是:
__init__.py charset.py connections.py constants converters.pyc cursors.pyc err.pyc times.py util.py
__init__.pyc charset.pyc connections.pyc converters.py cursors.py err.py tests times.pyc util.pyc
顯然cursors.py
是存在的。那麼是什麼給了?
你的代碼實際上是什麼樣子?可能是循環導入問題。 –