2012-12-26 36 views
0

所以我有一個應用程序使用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是存在的。那麼是什麼給了?

+0

你的代碼實際上是什麼樣子?可能是循環導入問題。 –

回答

2

您需要在文件頂部添加一個明確的import pymysql.cursors

cursors子包未在pymysql__all__中列出,因此在您僅做import pymysql時不會導入。

+0

是的,至少根據pymysql的github上的最新版本。 https://github.com/petehunt/PyMySQL/blob/master/pymysql/__init__.py看看底部。 –

+0

它確實在'__all__'中提到它,但它並不實際將子包導入到名稱空間中,因此實際上不會將其導出爲屬性。 – Wessie

+0

我可以確認'import pymysql'不會爲我自動導入'pymysql.cursors'。當我被毆打時,我正在寫相同的答案,所以我改寫了這個。 – DSM

相關問題