Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django : Cannot import modules

Tags:

python

django

I am trying to import a module in my views.py as

from django.shortcuts import render

# Create your views here.
from viewcreator import Builder
import json
def index(request):

    a,b=Builder.buildChartJSON()
    print(json.dumps(a))  
    print(json.dumps(b))
    return render(request, 'hdfsStats/hdfscharts.html',
                  {'sourcepoints': a, 'sizepoints': b})

and here is how my project setup looks like

enter image description here

why cant i import the modules in my view? I do not want to create these classes in the models.py. These classes are just meant to run some calculations and return two json objects, which i then feed to my webpage

here is my settings.py

"""
Django settings for mysite project.

Generated by 'django-admin startproject' using Django 1.9.4.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '9mxuqginh(vhh*2eu6j58kbq+%+7ql4_pn3k#yf+n96uv0rymq'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'hdfsStats.apps.HdfsstatsConfig'
]

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATIC_URL = '/static/'

am i missing any configuration?

Stacktrace

File "XXX:\mysite\hdfsStats\urls.py", l
ine 3, in <module>
    from . import views
  File "XXX:\mysite\hdfsStats\views.py",
line 5, in <module>
    from viewcreator import Builder
    ImportError: No module named 'viewcreator'

Update

current structure

enter image description here

and my views.py

from django.shortcuts import render

# Create your views here.
from .src.viewcreator import Builder
import json
def index(request):

    a,b=Builder.buildChartJSON()
    print(json.dumps(a))  
    print(json.dumps(b))
    return render(request, 'hdfsStats/hdfscharts.html',
                  {'sourcepoints': a, 'sizepoints': b})

but now i get

from propreader import ReadProp
ImportError: No module named 'propreader'

basically, i have four packages

viewcreator
propreader
esconnector
ping

the classes in these packages perform some calculations based on the properties files in the these two folders

props
resources

which i have put at the same level as src folder

Since these are one time calculations, i dont want to create models for these. What is the proper way for me to configure my Django project in this scenario? I need the Django project to host a webpage that will display the results of my calculations.

like image 986
AbtPst Avatar asked Mar 30 '16 14:03

AbtPst


1 Answers

In your INSTALLED_APPS

INSTALLED_APPS = [
    ...,
    'HdfsstatsConfig',
]

in your views.py

from .viewcreator import Builder

UPDATE, DJANGO IMPORTS

There are 3 ways to imports module in django

1. Absolute import: Import a module from outside your current application Example from myapp.views import HomeView enter image description here

2. Explicit import: Import a module from inside you current application enter image description here

3. Relative import: Same as explicit import but not recommended

from models import MyModel

enter image description here

like image 80
DJeanCar Avatar answered Oct 16 '22 14:10

DJeanCar