Import error

24 views
Skip to first unread message

Emily N

unread,
Jun 22, 2012, 8:10:36 AM6/22/12
to django...@googlegroups.com
Hi django users,
I seem to have gotten a problem.

I created a helper class in my project and I have failed to import
the methods in it. Please help...

This is the error I get...

ImportError at /admin/

cannot import name random_password
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/
Django Version: 1.3.1
Exception Type: ImportError
Exception Value:
cannot import name random_password
Exception Location: /home/emily/Documents/AskJarvis/Etutor/../Etutor/Prototype/views.py in <module>, line 1
Python Executable: /usr/bin/python
Python Version: 2.7.2
Python Path:
['/home/emily/Documents/AskJarvis/Etutor',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-linux2',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PIL',
 '/usr/lib/python2.7/dist-packages/gst-0.10',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/pymodules/python2.7',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
 '/usr/lib/python2.7/dist-packages/ubuntuone-client',
 '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel',
 '/usr/lib/python2.7/dist-packages/ubuntuone-couch',
 '/usr/lib/python2.7/dist-packages/ubuntuone-installer',
 '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol']

Marcin Tustin

unread,
Jun 22, 2012, 8:27:02 AM6/22/12
to django...@googlegroups.com
Please take a look at: Short, Self Contained, Correct Example

There's also some good advice at: What have you tried? - Matt Gemmell


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/C1nnWqPBZkwJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Marcin Tustin
Tel: 07773 787 105

Emily

unread,
Jun 22, 2012, 8:43:41 AM6/22/12
to django...@googlegroups.com
Dear Marcin,

I do not understand.
I think I have done everything right and I am trying to find
the source of the error where if there is someone who has
experienced it they might be knowing where it is coming from..

Emily

Marcin Tustin

unread,
Jun 22, 2012, 8:48:58 AM6/22/12
to django...@googlegroups.com
Dear Emily,

Do you expect us to already have a copy of your code? If not, we need to have a short, self-contained, and correct example demonstrating your issue.

Marcin

Cal Leeming [Simplicity Media Ltd]

unread,
Jun 22, 2012, 8:58:07 AM6/22/12
to django...@googlegroups.com
Hi Emily,

Here is a really good article (written by the very people who contribute to this list) on how to ask questions on the mailing list:


It tells you what you can do to try and resolve the issue yourself, and what sort of information you need to provide so we can assist.

Cal

Emily

unread,
Jun 22, 2012, 10:19:00 AM6/22/12
to django...@googlegroups.com
so your point is that I should post my code.
When I did that some time, I was told that
the people who were trying to help me did not
want to know what is in my code so I should just
explain what I want them to help me with.
But thank you any way, you point has been driven home.

Kurtis Mullins

unread,
Jun 22, 2012, 10:25:18 AM6/22/12
to django...@googlegroups.com
Hey emily,

I think the other guys were just wanting to see your import statement that's broken. It should be something along these lines if you want to give it a try:

from myproject.myapp.myhelpers import random_password

Of course you'd substitute your project name, the application name, and then the python file that your "random_password" function/class is saved in.

Let me know if you need any more help!

Good luck!
- Kurtis Mullins

Emily

unread,
Jun 22, 2012, 10:46:15 AM6/22/12
to django...@googlegroups.com
This is the views.py file

from helpers import random_password
from django.views.generic.edit import CreateView
from django.views.generic.list import ListView
from Prototype.forms import StudentForm, LecturerForm, PaymentForm
from Prototype.models import Guideline, Student, Lecturer, Payment
from Prototype.models import Course

class GuidelineListView(ListView):
    model = Guideline
    template_name = 'content.html'
    context_object_name = 'guidelines'

class Registration(CreateView):
    model = Student
    form_class = StudentForm
    template_name = "newAccount.html"
    success_url = "/Payment/"
    context_object_name = 'form'

    def send_sms(self):
        #send sms
        password = random_password()

    def post(self):
    '''generate password and send it to the user.'''
        return Super(Register, self).post()
       
class NewLecturer(CreateView):
    model = Lecturer
    form_class = LecturerForm
    template_name = "newAccount.html"
    success_url = "/login/"
    context_object_name = 'lecturer_form'

class CourseListView(ListView):
    model = Course
    template_name = 'index.html'
    context_object_name = 'courses'

class Payment(CreateView):
    model = Payment
    form_class = PaymentForm
    template_name = 'Payment.html'
    success_url = "/login/"
    context_object_name = 'payments'

Emily

unread,
Jun 22, 2012, 10:46:56 AM6/22/12
to django...@googlegroups.com
This is the class I created...

import string
import random

class Helpers:

    def random_password():
        '''a method to generate random password'''
        lengthOfPassword = random.randint(6,10)
        password_len = lengthOfPassword
        password = []

        for group in (string.ascii_letters, string.punctuation, string.digits):
            password += random.sample(group, 3)

            password += random.sample(
                 string.ascii_letters + string.punctuation + string.digits,
                 password_len - len(password))

            random.shuffle(password)
            password = ''.join(password)

            return password

    def sendSMS():
        '''a method to send sms to a user'''
        pass

Adrian Bool

unread,
Jun 22, 2012, 10:49:31 AM6/22/12
to django...@googlegroups.com

Hi Emily,

On 22 Jun 2012, at 15:46, Emily wrote:

> This is the class I created...
>
> import string
> import random
>
> class Helpers:
>
> def random_password():

Take random_password outside of the Helpers class and you should be OK. On the import statement you need to refer to a 'top level object' within the imported file. The only top level object you have is the class Helpers - which it doens't look like you really need.

Cheers,

aid

Daniel Roseman

unread,
Jun 22, 2012, 10:54:17 AM6/22/12
to django...@googlegroups.com
+1. Python is not Java. Don't use a class unless you're encapsulating data.
--
DR. 

Kurtis Mullins

unread,
Jun 22, 2012, 10:58:39 AM6/22/12
to django...@googlegroups.com
+1 on pulling the "random_password()" method out of the Class and setting it up as a module object.
I'd still suggest using this sort of an import statement on it, though:

from myproject.myapp.helpers import random_password

Where in this example you'd substitute your Project name with "myproject", the app name with "myapp" and the this particular python file with "helpers.py".

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/eV6jxgDm7cgJ.

Emily

unread,
Jun 22, 2012, 11:14:34 AM6/22/12
to django...@googlegroups.com
Kurtis, Adrian and Daniel,

thank you so much for your help, it actually worked
I have programmed in java 70% of my life

Now I want to send sms to my users when they sign up
in the website. There is some package I have seen which looks
like it is from an email to the phone. Does it work with messages
from a website to a phone...

Emily

Derek

unread,
Jun 25, 2012, 4:26:01 AM6/25/12
to django...@googlegroups.com
Emily

If you are starting a new topic, please start a new thread - and again, as per the guidelines for using the mailing list that Cal referred to, the more specific your question, the more chance of getting a reply. :)
Reply all
Reply to author
Forward
0 new messages