[英]unable to import function in django/python
I am writing a django project with follwing files: 我正在编写一个带有以下文件的django项目:
ttam_container
-utils.py
-ttam
-views.py
Codes within utils.py
module: utils.py
模块中的代码:
def random_string():
...
def remove_blanks():
...
...other functions...
Codes within views.py
: views.py
代码:
from utils import *
def get_sequences(request):
...
string = random_string()
...
sequences = remove_blanks(sequences_with_blanks)
...
The error global name remove_blanks' is not defined
is then reported. 然后报告global name remove_blanks' is not defined
错误global name remove_blanks' is not defined
。 I thought I didn't import the utils.py
correcty in the first place, but the random_string
works... 我以为我没有首先导入utils.py
,但random_string
工作...
Any idea what's happening? 知道发生了什么事吗?
The import should be: 导入应该是:
from utils import remove_blanks
without .py 没有.py
The correct import would be: 正确的导入是:
import sys
sys.path.append("..")
from utils import random_string, remove_blanks
Modules must be in one of the directories in sys.path
. 模块必须位于sys.path
中的一个目录中。 This is initialized to the value of $PYTHONPATH
, or some default if $PYTHONPATH
is not set. 这被初始化为$PYTHONPATH
的值,或者如果未设置$PYTHONPATH
则默认$PYTHONPATH
某些值。 For example: 例如:
$ 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']
So if your module isn't in that path, you need to append the right path ( '..'
in this case) to sys.path
. 因此,如果您的模块不在该路径中,则需要将正确的路径(在本例中为'..'
)附加到sys.path
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.