简体   繁体   中英

no module named pars when executing python3 script directly

pars in the django app I have made under INSTALLED_APPS within settings.py

When i run this python script directly from bash that includes:

   #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

    #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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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