Home > Mobile >  Python/Django - not able to run stand alone script / classes
Python/Django - not able to run stand alone script / classes

Time:02-23

I have a problem with running parts of script in Django as I can't get it to run the classes. The class I would like to import is this:

class Location(models.Model):
    City = models.CharField(max_length=200)
    Province = models.CharField(max_length=200)
    Country = models.CharField(max_length=10)

I have found some things when I tried to find a solution and at this point I have this:

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'portal.portal.settings'
import django
django.setup()
from portal.core.models import *

When I run this, it gives an error for django.setup():

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "C:\Users\User\Documents\Portal\venv\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\User\Documents\Portal\venv\lib\site-packages\django\apps\registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "C:\Users\User\Documents\Portal\venv\lib\site-packages\django\apps\config.py", line 211, in create
    mod = import_module(mod_path)
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'core'

'core' is the package module where all the functions are located the build up is like this: there is:

  • portal > portal > settings
  • portal > core

So I thought, maybe the whole directing goes wrong, I'm not sure. I did change it to:

os.environ['DJANGO_SETTINGS_MODULE'] = 'portal.settings' 

but then it says ModuleNotFoundError: No module named 'portal.settings'

Could someone help me to understand what is happening here and how I can really run the models?

CodePudding user response:

I guess you couldn't run the script alone. Because its having Django Framework's dependency.

Example: Models

CodePudding user response:

So I found out what I did wrong: I thought I could just use the interactive Django version in my python console, but apparently this is not possible :P. So I found out what I actually wanted: to create the interactive shell to run my python scripts of Django in.

in cmd prompt:

python manage.py shell

and from there just import and run all the functions I want. I really didn't know I couldn't run individual scripts in my python console so that's why I kept getting error messages

  • Related