Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a django python file in terminal

Tags:

python

django

I have a Python file thats part of the Django framework called facade.py

from django.conf import settings
from .gateway import Gateway

class Facade(object):


def __init__(self):
    self.gateway = Gateway(
        settings.password,
        settings.username,
        )

I want to test this code in my terminal window. This is what I have tried:

within virutal-env
python
>>> from my.apps.app.facade import Facade
>>> object = Facade()

but this give me the error ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

Why?

like image 357
GrantU Avatar asked Feb 22 '26 11:02

GrantU


2 Answers

You should not do this in a plain Python shell. Start the shell with Django configured by doing ./manage.py shell instead of just python.

like image 166
Daniel Roseman Avatar answered Feb 25 '26 01:02

Daniel Roseman


Try adding this:

from django.conf import settings

if not settings.configured:
    settings.configure()

Good luck!

like image 43
Marek Kowalski Avatar answered Feb 25 '26 00:02

Marek Kowalski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!