2

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

Red Cricket
  • 9,762
  • 21
  • 81
  • 166
N00b_Overflow
  • 53
  • 1
  • 2
  • 9
  • 1
    Make sure the settings file your manage.py file references is where it should be. It would help for you to post how your directories are organized, where your settings file(s) are. Also what you have in manage.py. The error in your console, etc. Also, second question mentions having an app the same name as your project. Does that apply to you? –  Dec 16 '18 at 04:45
  • 1
    When you say "started a Django project", how did you start your Django project? I asked because if you 1) create your virtual env. 2) execute `pip install Django` 3) `django-admin startproject mynewproj` 4) `cd mynewproj` 5) `python manage.py runserver`, everything should work. You must have done something else that is causing the problem. – Red Cricket Dec 16 '18 at 04:50
  • @Whodini please take a look at the edit above – N00b_Overflow Dec 16 '18 at 05:03
  • Are you running server after activating your virtual environment? If your virtual environment is activated your console should look something like: (env) Noobs-MBP:mytodoapp noob$. Note the (env). That means you are operating within your virtualenv. –  Dec 16 '18 at 05:06
  • In your project folder (the one above the one with manage.py) you should have a folder by the name of your virtual env. cd into this folder, cd into bin, type command source activate, and you should get the parentheses I mentioned in the above comment. Then try running server. –  Dec 16 '18 at 05:08
  • @RedCricket please see the screenshot of my terminal in the latest edit – N00b_Overflow Dec 16 '18 at 05:17
  • Something is wrong in your manage.py. Post the entire file. It's an invalid syntax error. –  Dec 16 '18 at 05:21
  • @Whodini please take a look at the image of the file posted in the latest edit – N00b_Overflow Dec 16 '18 at 05:26
  • hmm I'm not too familiar with django-2.1. Is that the default manage.py file you got when you started project, or did you modify it at all? I would say try dropping the from in line 14 down to the next line, and make sure it aligns with the closing parentheses in line 13, or the from in line 8. –  Dec 16 '18 at 05:32
  • 1
    also post results of pip freeze –  Dec 16 '18 at 05:35
  • 1
    Also, in your console it shows you installed Django 1.11, but your question tag says 2.1. –  Dec 16 '18 at 05:38

7 Answers7

4
  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. 
    
Syscall
  • 19,327
  • 10
  • 37
  • 52
2

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.

arshbot
  • 12,535
  • 14
  • 48
  • 71
1

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
Arif Shaikh
  • 79
  • 3
  • 10
0

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
Du D.
  • 5,062
  • 2
  • 29
  • 34
0

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

pip install sqlparse
Lamanus
  • 12,898
  • 4
  • 21
  • 47
Pankaj
  • 1
  • 1
0

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

fortune
  • 3
  • 3
0

This is how your dirctory should appear nameofyourproject\

|-- init.py

|-- manage.py

|-- settings.py

fortune
  • 3
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 12 '22 at 06:01