gpt4 book ai didi

python - 我遇到错误 "ModuleNotFoundError: No module named ' __main__.models'; '__main__' 不是一个包”

转载 作者:太空宇宙 更新时间:2023-11-03 20:38:19 24 4
gpt4 key购买 nike

我正在创建一个用于网页抓取的应用程序目录,该目录是在我的 django_project 中抓取的。我在将 models.py 模块中的类导入到views.py 模块中时遇到错误。

这是我的项目结构:

enter image description here

enter image description here

这是我在 scrape 应用程序中 models.py 中的代码

from django.db import models

# Create your models here.

# model -- headline (title, url, date)

class Headline(models.Model):
title = models.CharField(max_length=120)
url = models.TextField()
event_date = models.TextField()

def __str__(self):
return self.title

以及抓取应用程序中views.py内的这段代码

from django.shortcuts import render, redirect
import requests
requests.packages.urllib3.disable_warnings()
from bs4 import BeautifulSoup
from .models import Headline

# Create your views here.
def scrape():
# Use the session to get the URL
session = requests.Session()
url = 'https://allevents.in/malacca/all?ref=cityhome-popular'
# Content is basically grabs all the HTML that comes from the session
content = session.get(url, verify=False).content

soup = BeautifulSoup(content, "html.parser")

# Return a list
#item = soup.find_all('div', class_="event-item")
for item in soup.find_all('div', class_="event-item"):
title = item.find("h3").text.strip()
linkhref = item.find("h3").find("a").get('href')
date_posted = item.find("div", {"class":"right"})

new_headline = Headline()
new_headline.title = title
new_headline.url = linkhref
new_headline.event_date = date_posted
new_headline.save()

return redirect('/event/')

尝试从 cmd 运行 python views.py 后出现此错误

Traceback (most recent call last):
File "views.py", line 5, in <module>
from .models import Headline
ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package

我也在我的views.py中尝试了这个

from scrape.models import Headline

但我收到以下错误

Traceback (most recent call last):
File "views.py", line 5, in <module>
from scrape.models import Headline
File "C:\Users\USER\django_project\scrape\scrape.py", line 2, in <module>
from .models import Headline
ImportError: attempted relative import with no known parent package

如果我从模型导入标题更改

Traceback (most recent call last):
File "views.py", line 6, in <module>
from models import Headline
File "C:\Users\USER\django_project\scrape\models.py", line 7, in <module>
class Headline(models.Model):
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\base.py", line 103, in __new__
app_config = apps.get_containing_app_config(module)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\apps\registry.py", line 252, in get_containing_app_config
self.check_apps_ready()
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\apps\registry.py", line 134, in check_apps_ready
settings.INSTALLED_APPS
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\conf\__init__.py", line 79, in __getattr__
self._setup(name)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\conf\__init__.py", line 64, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

最佳答案

当您想要导入 model 时在本例中,您应该使用 models.py 的路径明确 Python 应该查看的位置。然后,您可以通过指定类名来导入一个或多个模型,如下所示:

from <path_to_models.py> import <your_model_name>

要解决您的问题,请更改:

from .models import Headline

from models import Headline

在您导入 Headline 的所有文件中型号

编辑

当您使用具有自动填充功能的 IDE 时,例如 Pycharm。您可以通过简单的方式导入模型:

  1. 确保您的文件中使用了导入的模型
  2. 删除导入行
  3. 将光标置于文件中要导入的模型的末尾
  4. alt + enter
  5. 点击import model

关于python - 我遇到错误 "ModuleNotFoundError: No module named ' __main__.models'; '__main__' 不是一个包”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57015358/

24 4 0