简体   繁体   English

直接执行python3脚本时没有名为pars的模块

[英]no module named pars when executing python3 script directly

pars in the django app I have made under INSTALLED_APPS within settings.py我在 settings.py 中的INSTALLED_APPS下创建的 django 应用程序中的pars

When i run this python script directly from bash that includes:当我直接从 bash 运行这个 python 脚本时,其中包括:

   #below 5 lines to run scripts 'standalone' in django
   import django
   from django.conf import settings
   from pars import pars_defaults
   settings.configure(default_settings=pars_defaults, DEBUG=True)
   django.setup()
   from pars.models import tb1, tb2

   technologytitle=soup.find("h1").text
    
   try:
       technology=CMS(nm=technologytitle)
       technology.save()
       print("saved techology: {}".format(technologytitle))
   except IntegrityError as e:
        print("Integrity error occurring with saving: {} {}".format(technologytitle, e))

UPDATED Im getting an error of:更新我收到以下错误:

ModuleNotFoundError: No module named 'pars'

ive done a ton of research, but cant seem to figure out why this is happening.我做了大量的研究,但似乎无法弄清楚为什么会这样。

UPDATED: settings.py更新: settings.py

    #code before

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'pars',
    ]
    
    ROOT_URLCONF = 'secenumproject.urls'
    WSGI_APPLICATION = 'secenumproject.wsgi.application'

directory structure:目录结构:

secenumproject/ 
    settings.py
    # etc
    secenumproject/pars
        models.py
        parse.py
        # etc

Thanks谢谢

you can not run this script directly without changes.如果不进行更改,您将无法直接运行此脚本。

Read more here: https://docs.djangoproject.com/en/4.1/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage在此处阅读更多信息: https://docs.djangoproject.com/en/4.1/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage

i can not understand structure of your project.我无法理解您项目的结构。 But it works with my project:但它适用于我的项目:

# file my_test.py
import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings.settings')
django.setup()

from core.models import Shop

additional info about folder:关于文件夹的附加信息:

project/
    __init__.py
    my_test.py
    manage.py

    core/
        __init__.py
        apps.py
        models.py

    settings/
        __init__.py
        settings.py

how i run it:我如何运行它:

project>python my_test.py

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM