Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError and Django driving me crazy

OK, I have the following directory structure (it's a django project):

-> project
--> app

and within the app folder, there is a scraper.py file which needs to reference a class defined within models.py

I'm trying to do the following:


import urllib2
import os
import sys
import time
import datetime
import re
import BeautifulSoup

sys.path.append('/home/userspace/Development/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'

from project.app.models import ClassName

and this code just isn't working. I get an error of:

Traceback (most recent call last):
  File "scraper.py", line 14, in 
    from project.app.models import ClassName
ImportError: No module named project.app.models

This code above used to work, but broke somewhere along the line and I'm extremely confused as to why I'm having problems. On SnowLeopard using python2.5.

like image 785
John Peebles Avatar asked Nov 24 '25 12:11

John Peebles


1 Answers

import sys
sys.path.append ('/path/to/the/project')
from django.core.management import setup_environ
import settings
setup_environ(settings)

from app.models import MyModel
like image 53
Anatoly Rr Avatar answered Nov 26 '25 00:11

Anatoly Rr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!