gpt4 book ai didi

python - Django ImportError : No module named tango_with_django_project. 设置

转载 作者:太空宇宙 更新时间:2023-11-04 08:44:32 24 4
gpt4 key购买 nike

在 Tango With Django 教程的第 5 章接近尾声时,我创建了一个脚本,用于使用名为的测试数据填充 SQLite 数据库:
~/Workspace/wad2/tango_with_django_project/populate_rango.py
报错后,我尝试直接从书上复制粘贴脚本,仍然报错如下:

H:\Workspace\wad2\tango_with_django_project>python populate_rango.py
Traceback (most recent call last):
File "populate_rango.py", line 7, in <module>
django.setup()
File "C:\Python27\lib\site-packages\django\__init__.py", line 22, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 53, in __ge
tattr__
self._setup(name)
File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 41, in _set
up
self._wrapped = Settings(settings_module)
File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 97, in __in
it__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named tango_with_django_project.settings

populate_rango.py

import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE',
'tango_with_django_project.settings')

import django
django.setup()
from rango.models import Category, Page

def populate():

python_pages = [
{"title": "Official Python Tutorial",
"url":"http://docs.python.org/2/tutorial/"},
{"title":"How to Think like a Computer Scientist",
"url":"http://www.greenteapress.com/thinkpython/"},
{"title":"Learn Python in 10 Minutes",
"url":"http://www.korokithakis.net/tutorials/python/"} ]

django_pages = [
{"title":"Official Django Tutorial",
"url":"https://docs.djangoproject.com/en/1.9/intro/tutorial01/"},
{"title":"Django Rocks",
"url":"http://www.djangorocks.com/"},
{"title":"How to Tango with Django",
"url":"http://www.tangowithdjango.com/"} ]

other_pages = [
{"title":"Bottle",
"url":"http://bottlepy.org/docs/dev/"},
{"title":"Flask",
"url":"http://flask.pocoo.org"} ]

cats = {"Python": {"pages": python_pages},
"Django": {"pages": django_pages},
"Other Frameworks": {"pages": other_pages} }

for cat, cat_data in cats.items():
c = add_cat(cat)
for p in cat_data["pages"]:
add_page(c, p["title"], p["url"])

# Print out the categories we have added.
for c in Category.objects.all():
for p in Page.objects.filter(category=c):
print("- {0} - {1}".format(str(c), str(p)))

def add_page(cat, title, url, views=0):
p = Page.objects.get_or_create(category=cat, title=title)[0]
p.url=url
p.views=views
p.save()
return p

def add_cat(name):
c = Category.objects.get_or_create(name=name)[0]
c.save()
return c

# Start execution here!
if __name__ == '__main__':
print("Starting Rango population script...")
populate()

模型.py

from __future__ import unicode_literals
from django.db import models

class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
views = models.IntegerField(default=0)
likes = models.IntegerField(default=0)

class Meta:
verbose_name_plural = 'Categories'

def __str__(self):
return self.name

class Page(models.Model):
category = models.ForeignKey(Category)
title = models.CharField(max_length=128)
url = models.URLField()
views = models.IntegerField(default=0)

def __str__(self):
return self.title

settings.py(相关代码)

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, 'tango_with_django_project', 'templates')
STATIC_DIR = os.path.join(BASE_DIR, 'tango_with_django_project', 'static')
MEDIA_DIR = os.path.join(BASE_DIR, 'tango_with_django_project', 'media')

地点

~/Workspace/wad2/manage.py  
~/Workspace/wad2/tango_with_django_project/populate_rango.py
~/Workspace/wad2/tango_with_django_project/settings.py

有谁知道这些错误的可能原因是什么?这似乎是文件夹名称错误的常见原因,但我的脚本位于 tango_with_django_project 文件夹中,该文件夹已在我的 os.environ.setdefault 行中指定。如果任何其他文件有用,我可以添加它们。

最佳答案

为了让您的 Django 项目将设置加载为 tango_with_django_project.settingswad2 需要位于 Python 路径上。

最简单的解决方法是将 populate_rango.pywad2/tango_with_django_project/ 目录向上移动到 wad2

或者,您可以将父目录添加到 populate_rango.py 中的 python 路径

import sys
sys.path.append('..')

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE',
'tango_with_django_project.settings')

关于python - Django ImportError : No module named tango_with_django_project. 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41893837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com