简体   繁体   English

我遇到一个错误“ ModuleNotFoundError:没有名为'__main __。models'的模块; “ __main__”不是软件包”

[英]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: 您可以通过简单的方式导入模型:

  1. Make sure the imported model is used in your file 确保在文件中使用了导入的模型
  2. Remove the import line 删除导入线
  3. Put your cursor at the end of the model you want to import in the file 将光标放在文件中要导入的模型的末尾
  4. Press alt + enter alt + enter
  5. Click on import model 点击import model

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ModuleNotFoundError:没有名为'__main __。models'的模块; '__main__'不是包 - ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package ModuleNotFoundError: 没有名为“__main__.gtts”的模块; &#39;__main__&#39; 不是一个包 - ModuleNotFoundError: No module named '__main__.gtts'; '__main__' is not a package ModuleNotFoundError:没有名为&#39;__main __。web_app&#39;的模块; 从子文件夹导入包时,&#39;__ main__&#39;不是包 - ModuleNotFoundError: No module named '__main__.web_app'; '__main__' is not a package while importing package from sub folder Python 3 - 在同一目录中导入.py文件 - ModuleNotFoundError:没有名为'__main __。char'的模块; '__main__'不是包 - Python 3 - importing .py file in same directory - ModuleNotFoundError: No module named '__main__.char'; '__main__' is not a package 在docker中运行龙卷风python:ModuleNotFoundError:没有名为&#39;__main __。config&#39;的模块; &#39;__main__&#39;不是一个包 - running tornado python in docker: ModuleNotFoundError: No module named '__main__.config'; '__main__' is not a package 解决“ModuleNotFoundError: ...'__main__' is not a package”错误 - Working around "ModuleNotFoundError: ...'__main__' is not a package" error 错误:没有名为“__main__.forms”的模块; &#39;__main__&#39; 不是 Python3 中的包 - Error: No module named '__main__.forms'; '__main__' is not a package in Python3 ModuleNotFoundError:__main__ 不是包是什么意思? - ModuleNotFoundError: What does it mean __main__ is not a package? 基准安装错误,ModuleNotFoundError:没有名为“main”的模块 - benchmark installation error, ModuleNotFoundError: No module named 'main' 如何解决 Django 'ModuleNotFoundError' 没有名为 '___main___.models' 的模块 - How to solve Django 'ModuleNotFoundError' No module named '___main___.models'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM