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?

2
  • Ensure that each package has a __init__.py file as well Commented Apr 17, 2013 at 23:52
  • i did have... didn't show it here Commented Apr 18, 2013 at 0:06

2 Answers 2

2

The import should be:

from utils import remove_blanks

without .py

Sign up to request clarification or add additional context in comments.

2 Comments

And also without the wildcard. :) +1
also, when i try to wrote it as from utils import remove_blanks, ImportError is reported
0

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. This is initialized to the value of $PYTHONPATH, or some default if $PYTHONPATH is not set. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.