Django Shell No module named settings

后端 未结 12 1363
温柔的废话
温柔的废话 2020-12-02 20:26

I\'ve deployed Django to Apache via mod_wsgi. Django is running fine when hosted from Apache. However, I\'m trying to do some maintenance via manage.py

相关标签:
12条回答
  • 2020-12-02 21:07

    I had a similar problem, where the same error was being returned when I tried to run django-admin.py startproject myapp. A previous answer here helped me figure it out. The problem was that I had previously pointed DJANGO_SETTINGS_MODULE to a certain file, which I had later deleted. To fix it, I just removed the pointer with this command:

    export DJANGO_SETTINGS_MODULE=

    0 讨论(0)
  • 2020-12-02 21:11

    If you are using wsgi/uwsgi in production...

    I was having the same error:

    If you renamed the folder that django startproject created that has setting.py files and wsgi.py , check in the wsgi.py file the line: os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<your_folder_name>.settings")

    In my case i had to rename < your_folder_name> also.

    0 讨论(0)
  • 2020-12-02 21:15

    This can happen if your root directory name is the same as the name of one of your apps. For example here I have a directory called bar containing a Django project with an app also called bar:

    Simons-MacBook-Pro ~/temp
    $ cd bar
    
    Simons-MacBook-Pro ~/temp/bar
    $ ./manage.py shell
    Error: Could not import settings 'bar.settings' (Is it on sys.path?): No module named settings
    
    Simons-MacBook-Pro ~/temp/bar
    $ ls -l
    total 48
    -rw-r--r--  1 simon  staff     0 25 Oct 10:46 __init__.py
    -rw-r--r--  1 simon  staff   130 25 Oct 10:46 __init__.pyc
    drwxr-xr-x  7 simon  staff   238 25 Oct 10:46 bar
    -rwxr-xr-x  1 simon  staff   503 25 Oct 10:46 manage.py
    -rw-r--r--  1 simon  staff  5025 25 Oct 10:46 settings.py
    -rw-r--r--  1 simon  staff  2658 25 Oct 10:46 settings.pyc
    -rw-r--r--  1 simon  staff   556 25 Oct 10:46 urls.py
    

    Changing the root directory's name to foo (or anything else other than bar) solves the problem:

    Simons-MacBook-Pro ~/temp/bar
    $ cd ..
    
    Simons-MacBook-Pro ~/temp
    $ mv bar foo
    
    Simons-MacBook-Pro ~/temp
    $ cd foo
    
    Simons-MacBook-Pro ~/temp/foo
    $ ./manage.py shell
    Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) 
    [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    (InteractiveConsole)
    >>> 
    
    0 讨论(0)
  • 2020-12-02 21:17

    It seems the path to your project isn't being recognized by wsgi. This has happened to me, and to solve it I added this to the top of my .wsgi file:

    import os
    import sys
    
    root_path = os.path.abspath(os.path.split(__file__)[0])
    sys.path.insert(0, os.path.join(root_path, 'project_name'))
    sys.path.insert(0, root_path)
    
    0 讨论(0)
  • 2020-12-02 21:17

    Somehow, if your project folder is the same name as the app that has the settings file in it, and if you have __init__.py in the project root folder, it will mess wsgi. I really dont understand why but removing this file solved this for me.

    0 讨论(0)
  • 2020-12-02 21:18

    In my case the wsgi.py file was working when the system was running normally, but I was getting the ImportError when trying to do a manual manage.py command like migrate or collectstatic.

    I checked wsgi.py for the way it imports the settings and noticed that it first adds the settings path to the sys.path as follows:

    import sys
    sys.path.append('/opt/server/settings')
    

    I added that to the top of the manage.py and it works.

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