2013-10-20 24 views
0

得到一些AppEngine上的代碼一個小問題,我不能工作了(可能只是因爲它的晚)進口*造成「全球名‘emailtools’沒有定義」

我有一個文件夾,名爲模塊有以下三個項目:

-modules 
: __init__.py (blank) 
: checklogin.py 
: customhandlers.py 
: datastoretools.py 
: emailtools.py 

在我的代碼使用所有這些,所以我這樣做是將其導入:

from modules import * 

一切正常吧emailtools。這:

emailtools.sendNotificationEmail('assignee',report,True) 

導致此錯誤:

File "/home/tom/dev/ad-project/handlers/reporterhandler.py", line 42, in get 
emailtools.sendNotificationEmail('assignee',report,True) 
NameError: global name 'emailtools' is not defined 

發生這種情況,無論我嘗試使用它,但其他三項工作完全正常的,任何想法,爲什麼?或者我應該只導入它們而不是使用*?

+0

另外:「模塊」!=「模塊」。這不是今天問題的原因,但是試圖在兩個地方拼寫相同的名字。 –

+1

你是否確定** __init __。py'是空白的?我無法用空白的'__init __。py'來重現你的行爲。請製作並複製粘貼顯示錯誤的最短程序。有關更多詳細信息,請參閱http://SSCCE.ORG/。 –

+0

某些__init__.py是空的。而'模塊'是一個錯字,目錄名爲「模塊」。 –

回答

1

這應該不起作用。看來這個通配符不是你使用的唯一一個。也許其他模塊來自其他通配符。

在任何情況下,通配符是不希望的 根據PEP 8

Wildcard imports (from import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools. There is one defensible use case for a wildcard import, which is to republish an internal interface as part of a public API (for example, overwriting a pure Python implementation of an interface with the definitions from an optional accelerator module and exactly which definitions will be overwritten isn't known in advance).

P.S.我假設你使用Windows,否則Modules不能導入爲modules我認爲。

+0

我在試圖決定如何導入模塊時正在尋找這個確切的語句,但無法找到它如此使用的導入*。感謝您解決問題,我會按名稱導入它們。 另外,我在Linux上,從不假設。而「模塊」是一個錯字,「模塊」是目錄的名稱。 –

+0

此外,這是我用過的唯一通配符,我只用它作爲此目錄只包含這些模塊,我正在使用所有這些模塊。 –