Python Django:无法导入模块
我正在尝试在my views.py中导入一个模块Python Django:无法导入模块,python,django,Python,Django,我正在尝试在my views.py中导入一个模块 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 r
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})
下面是我的项目设置的样子
为什么我不能导入视图中的模块?我不想在models.py中创建这些类。这些类只是用来运行一些计算并返回两个json对象,然后将它们输入我的网页
这是我的设置.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/'
我是否缺少任何配置
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'
但现在我明白了
from propreader import ReadProp
ImportError: No module named 'propreader'
基本上,我有四个包裹
viewcreator
propreader
esconnector
ping
这些包中的类根据这两个文件夹中的属性文件执行一些计算
props
resources
我把它放在与src
文件夹相同的级别
由于这些是一次性计算,我不想为这些创建模型。在这种情况下,我配置Django项目的正确方法是什么?我需要Django项目托管一个网页,该网页将显示我的计算结果。在您安装的应用程序中
INSTALLED_APPS = [
...,
'HdfsstatsConfig',
]
在您的视图中.py
from .viewcreator import Builder
更新,DJANGO导入
django中有3种导入模块的方法
1。绝对进口:
从当前应用程序外部导入模块
例子
从myapp.views导入HomeView
2。显式导入:
从当前应用程序内部导入模块
3。相对导入:
与显式导入相同,但不推荐使用
from models import MyModel
请添加完整的回溯。请查看编辑。我想做的有意义吗?谢谢!有什么方法可以将我所有的包添加到src文件夹中吗?我需要更多的软件包让我所有的课程都能正常工作。现在,我的视图能够检测到构建器类,但在我的构建器中,我有类似于“from propreader import ReadProp”的东西。现在,我再次遇到来自propreader import ReadProp ImportError的错误
:没有名为“propreader”的模块与ViewCreator处于同一级别。在项目设置中,我看不到它,但在这种情况下,您可以从hdfsstatconfig使用。propreader导入ReadProp,你需要了解django上的导入是如何工作的,我会编辑我的答案让我稍等一下,请同时查看编辑。我在hdfsstatconfig中添加了一个更详细的描述,描述了我试图实现的。propreader导入ReadProp
我从hdfsStatConfig获取。propreader导入ReadProp导入错误:没有名为“hdfsStatConfig”的模块
from models import MyModel