简体   繁体   中英

How to set environment variable DJANGO_SETTINGS_MODULE

I've bee trying to send a pandas dataframe to the django database, though when I try this:

user = settings.DATABASES['default']['USER']
password = settings.DATABASES['default']['PASSWORD']
database_name = settings.DATABASES['default']['NAME']
# host = settings.DATABASES['default']['HOST']
# port = settings.DATABASES['default']['PORT']

database_url = 'postgresql://{user}:{password}@localhost:5432/{database_name}'.format(
    user=user,
    password=password,
    database_name=database_name,
)

engine = create_engine(database_url, echo=False)

I get this:

ImproperlyConfigured: Requested setting DATABASES, 
but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE 
or call settings.configure() before accessing settings.

What could I do to fix this?

Edit: When I try the following command on python mamange.py shell :

set DJANGO_SETTINGS_MODULE=main_project.settings

I get:

File "<ipython-input-5-d6c3b615ad79>", line 1
    set DJANGO_SETTINGS_MODULE=main_project.settings
        ^
SyntaxError: invalid syntax

thanks

Assuming your server is Linux, you can set environment variables by typing export DJANGO_SETTINGS_MODULE=your_value into your bash or sh console.

Edit: For Windows:

If you're using Powershell, the command should be $env:DJANGO_SETTINGS_MODULE= 'your_value' .

If you're using batch (aka "cmd"), it's set DJANGO_SETTINGS_MODULE="your_value"

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