[英]unable to import function in django/python
我正在編寫一個帶有以下文件的django項目:
ttam_container
-utils.py
-ttam
-views.py
utils.py
模塊中的代碼:
def random_string():
...
def remove_blanks():
...
...other functions...
views.py
代碼:
from utils import *
def get_sequences(request):
...
string = random_string()
...
sequences = remove_blanks(sequences_with_blanks)
...
然后報告global name remove_blanks' is not defined
錯誤global name remove_blanks' is not defined
。 我以為我沒有首先導入utils.py
,但random_string
工作...
知道發生了什么事嗎?
導入應該是:
from utils import remove_blanks
沒有.py
正確的導入是:
import sys
sys.path.append("..")
from utils import random_string, remove_blanks
模塊必須位於sys.path
中的一個目錄中。 這被初始化為$PYTHONPATH
的值,或者如果未設置$PYTHONPATH
則默認$PYTHONPATH
某些值。 例如:
$ python
Python 2.6.5 (r265:79063, Jun 12 2010, 17:07:01)
[GCC 4.3.4 20090804 (release) 1] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python26.zip', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-cyg
win', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/pytho
n2.6/lib-dynload', '/usr/lib/python2.6/site-packages']
因此,如果您的模塊不在該路徑中,則需要將正確的路徑(在本例中為'..'
)附加到sys.path
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.