0

I am writing a django project with follwing files:

ttam_container
    -utils.py
    -ttam
         -views.py

Codes within utils.py module:

def random_string():
    ...
def remove_blanks():
    ...


...other functions...

Codes within 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. I thought I didn't import the utils.py correcty in the first place, but the random_string works...

Any idea what's happening?

4

2 回答 2

2

导入应该是:

from utils import remove_blanks

没有 .py

于 2013-04-17T21:40:34.510 回答
0

正确的导入是:

import sys
sys.path.append("..")
from utils import random_string, remove_blanks

模块必须位于sys.path. 这被初始化为 的值,或者如果未设置,则为$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.

于 2013-04-18T00:19:15.167 回答