Why am I getting this error in Django?

后端 未结 6 2038
暗喜
暗喜 2021-02-01 06:25

I have a script that imports a models.py from an app, but it will not import! I don\'t believe I am supposed to manually create an \"export DJANGO...\" environment variable...I\

相关标签:
6条回答
  • 2021-02-01 06:31

    You need to properly import your settings file. If you don't import the django environment variables first, your import will fail as you've noticed. You need to code something like:

    import os
    
    # Set the DJANGO_SETTINGS_MODULE environment variable.
    os.environ['DJANGO_SETTINGS_MODULE'] = "myproject.settings"
    
    #do your stuff
    
    
    0 讨论(0)
  • 2021-02-01 06:38

    From command line, please execute this command. This may help you:

    export DJANGO_SETTINGS_MODULE=projectname.settings
    
    0 讨论(0)
  • 2021-02-01 06:39

    I had this similar error, and I realized that I forgot to create the file "init.py" in to the app folder:

    myapp:
        manage.py
        myapp:
            __init__.py
            settings.py
            ....
    
    0 讨论(0)
  • 2021-02-01 06:41

    One alternative to messing about with the environment is to just write your script as a Django custom management command, which takes care of setting up the environment for you (along with other things like option parsing, etc).

    0 讨论(0)
  • 2021-02-01 06:50

    from command line ran "python manage.py shell" should include the right settings for you and give you the python prompt.

    0 讨论(0)
  • 2021-02-01 06:56

    I don't believe I am supposed to manually create an "export DJANGO..." environment variable...

    Manually or otherwise, you are supposed to ensure that variable is in the environment before you import a Django models file -- not sure what the causes are for your disbelief, but, whatever they may be, that disbelief is ill-founded.

    0 讨论(0)
提交回复
热议问题