Manually running a script that imports a Django model

405 views
Skip to first unread message

Sourish Kundu

unread,
Feb 13, 2020, 8:04:13 AM2/13/20
to Django users
So I am trying to access one of my models created in views.py in another script. This second script is the one I would like manually run. It imports the model without any errors; however, when I try to run it using PyCharm, I get this error:

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.

Any help on resolving this issue would be very much appreciated.


Thanks,
Sourish

Adam Mičuda

unread,
Feb 13, 2020, 8:59:27 AM2/13/20
to django...@googlegroups.com
Hi,
the solution is written in the error message "You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure()...". But I would recommend you to use https://django-extensions.readthedocs.io/en/latest/runscript.html. I think it is what you are want. ;)

Regards.

Adam

čt 13. 2. 2020 v 14:03 odesílatel Sourish Kundu <sourish...@gmail.com> napsal:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2c9b2d34-a9ab-4a2f-a1e4-e2694c265ea0%40googlegroups.com.

onlinejudge95

unread,
Feb 13, 2020, 9:05:18 AM2/13/20
to django...@googlegroups.com
On Thu, Feb 13, 2020 at 6:34 PM Sourish Kundu <sourish...@gmail.com> wrote:
So I am trying to access one of my models created in views.py in another script. This second script is the one I would like manually run. It imports the model without any errors; however, when I try to run it using PyCharm, I get this error:

Firstly, what are your models doing in views.py? I am assuming it was a typo.
 

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.

Any help on resolving this issue would be very much appreciated.

For scripts like these that are not part of your web server or that do not take part in the request-response cycle, I like to implement them as custom Django commands. You can read more about them here.

maninder singh Kumar

unread,
Feb 13, 2020, 1:47:33 PM2/13/20
to django...@googlegroups.com
Could it be an improperly configured database ?
Also check installed apps.  
It is most likely a syntax error in settings like a comma, colon or apostrophe

Sent from my iPad
Reply all
Reply to author
Forward
0 new messages