django - ModuleNotFoundError Django 视图
问题描述
我在我的 django 项目中创建了一个单独的模块(sources.py),以保存函数并防止我的视图模块变成一个完整的噩梦。
当我将sources.py导入views.py时,调用它包含的函数并运行代码,并使用打印语句确保准确导入调用的内容,一切正常,我看到打印出我期望的内容。
但是,当我尝试在本地服务器中运行应用程序时,我得到了 ModuleNotFoundError。
如果我将函数中的代码包含在视图中,然后运行本地服务器,一切正常。
该模块显然正在被导入,否则打印语句将不起作用。但是,尝试在本地服务器上运行时会丢失某些内容。
我经历了大量的 stackoverflow 问题、python 和 django 模块教程,以及位于其他地方的问题,但似乎没有任何问题可以解决问题。以下是所有代码。
sources.py 和 views.py 都在同一个目录中。(betrTV/betrTV_app/file.py)
我也试过 "import sources *
提供original = sources.video()
了同样的问题以及import . sources
from django.shortcuts import render
from django.http import HttpResponse
from datetime import date
from sources import video
''' returns a dict of unique video identifiers (last 11 chars)'''
original = video()
print(original)
sources = []
# strip out all except the unique source code
for i in range(len(original)):
source = original[i]
source = source[30:]
sources.append(source)
# Checked out... print(sources)
# create day of week to pull video
day = date.today()
num = str(day)[9]
# pull source from list by using 'num' from 'day'
source = sources[int(num)]
def index(request):
''' The home page for betrTV_app '''
video = 'class="video-frame" width="1200" height="600"\
+ src="https://www.youtube.com/embed/%s"\
+ frameborder="0" allow="accelerometer; autoplay; encrypted-media; \
+ gyroscope; picture-in-picture" allowfullscreen' %source
html = '<html><body><link href="https://fonts.googleapis.com/css?family=Gugi"\
+ rel="stylesheet"><h1 style="color: grey; font-family: Gugi; font-size: \
+ 20px;">BetrTV... Empowerment on Demand</h1>\
<iframe %s></iframe></body></html>' %video
return HttpResponse(html)
INSTALLED_APPS = [
...
# My Apps
'betrTV_app',
]
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Prometheus\Dropbox\LVL6\Programming\Python\betrTV\betrTV\urls.py", line 21, in <module>
path('', include('betrTV_app.urls')),
File "C:\Users\Prometheus\Envs\betr_env\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\Prometheus\Envs\betr_env\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Prometheus\Dropbox\LVL6\Programming\Python\betrTV\betrTV_app\urls.py", line 4, in <module>
from . import views
File "C:\Users\Prometheus\Dropbox\LVL6\Programming\Python\betrTV\betrTV_app\views.py", line 4, in <module>
from sources import video
ModuleNotFoundError: No module named 'sources'
解决方案
推荐阅读
- android - Android webview 在“#”字符后什么也不渲染
- android - 我的应用程序在代码中使用 Kotlin 实验性功能是否健康?
- javascript - VueJS路由器espace“&”字符然后使查询参数失败
- javascript - 通过 keyup 事件搜索单个 Datatable 列有效,但 draw() 函数无效
- amazon-web-services - Amazon Simple Email Service (Amazon SES) 将邮件发送到垃圾邮件中
- hyperledger-fabric - 如何在 Hyperledger Fabric 上调试 nodejs 链代码
- r - 在哪里并行化?构建一个 bash 脚本以在多个节点上进行并行化
- kubernetes - 加载 kubelet 配置文件失败
- python - 从 Python 通过 whatsapp 发送文件
- android - MPAndroidChart 相对于绘制的值显示较少数量的 x 轴