[英]I'm having an error “ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package”
I'm creating an app directory for web scraping which is scrape inside my django_project. 我正在创建一个用于Web抓取的应用程序目录,该目录在django_project中被抓取。 I'm having an an error in importing a class from my models.py module into my views.py module. 将我的models.py模块中的类导入到views.py模块中时出现错误。
this is my project structure: 这是我的项目结构:
Here's my code inside models.py in scrape app 这是我在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
and this code inside views.py in scrape app 和此代码在scrape应用程序的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/')
after try run python views.py from cmd this error is appeared 尝试从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
I also try this in my views.py 我也在views.py中尝试
from scrape.models import Headline
but I get the following error 但我收到以下错误
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
also if i change from models import Headline 另外,如果我从模型导入标题更改
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.
When you want to import your model
you should use the path to, in this case, models.py
to specificy where Python should look. 当您要导入model
,应使用路径(在这种情况下,即models.py
来具体指定Python的外观。 Then you can import one or multiple models by specifying their class names like this: 然后,您可以通过指定其类名来导入一个或多个模型,如下所示:
from <path_to_models.py> import <your_model_name>
To solve your problem, change: 要解决您的问题,请更改:
from .models import Headline
to 至
from models import Headline
In all your files where you are importing your Headline
model 在您要导入Headline
模型的所有文件中
EDIT 编辑
When you are using an IDE that has autofill, Pycharm for example. 当您使用具有自动填充功能的IDE时,例如Pycharm。 You can import the model in an easy way: 您可以通过简单的方式导入模型:
alt + enter
按alt + enter
import model
点击import model
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.