简体   繁体   中英

python manage.py runserver error : ModuleNotFoundError: No module named 'settings'

I am in the process on getting started with python and have run into a problem using django 2.1 and python 3.7 that many other people seem to have had as well.

Below is my process for getting here:

  • started a virtual environment

  • started a django project

  • attempted to run:

python manage.py runserver

I consistently get the error: ModuleNotFoundError: No module named 'settings'

I've researched extensively to and came across a few solutions in the following SO questions, none of which were effective solutions for me. Has anyone come across this issue?

First question researched

Second Question researched

Any insight would be helpful. Thanks in advance.

EDIT: my manage.py file looks like this:

#!/usr/bin/env python import os import sys

if __name__ == '__main__':
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mytodoapp.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)

my file structure looks like:

mytodoapp

mytodoapp

>__init__.py >settings.py >urls.py >wsgi.py

manage.py

Screenshot of my console

Manage.py file

  1. open yr project folder.

  2. search for the wsgi.py file and add this line.

     from django.conf import settings.
  3. Then save.

  4. locate yr manage.py file edit the

    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'projectname.settings')

    To

    os.environ['DJANGO_SETTINGS_MODULE' ]='projectname.settings'
  5. save and walla problem fixed.

  6. cd to yr app folder and then runserver with

    python manage.py runserver.

In my case the issue ended up being because I had modified the tree structure in a way so that manage.py didn't have access to some of the resources it needed. My solution ended up recreating a scrap django project and looking at the project structure. You should follow this structure

yourproject/
├── db.sqlite3
├── manage.py
└── yourproject/
    ├── __init__.py
    ├── app1/
    ├── settings.py
    ├── urls.py
    └── wsgi.py

It seems like it's important that manage.py stay outside of the inner yourproject folder.

I faced a similar issue. But I was creating the django project while using Anaconda. Once I installed sqlparse using below command the issue resolved.

conda install sqlparse

django 2.1 requires python 3.5+ which support the syntax in the manage.py file.

Activate your environment and verify that your python is 3.7 If it is not, then specify your python version when you create your virtualenv.

virtualenv -p /path/to/your/python3.7 code

Try to install 'sqlparse'. It will work in Anaconda then.

pip install sqlparse

Try opening your directory and right clicking the setting. When you do open it with pycharm and then run the code on the terminal again

This is how your dirctory should appear nameofyourproject\

|-- init .py

|-- manage.py

|-- settings.py

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