简体   繁体   English

python manage.py runserver 错误:ModuleNotFoundError:没有名为“settings”的模块

[英]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.我正在开始使用 python,并且在使用 django 2.1 和 python 3.7 时遇到了很多其他人似乎也遇到过的问题。

Below is my process for getting here:以下是我到达这里的过程:

  • started a virtual environment启动虚拟环境

  • started a django project开始了一个django项目

  • attempted to run:试图运行:

python manage.py runserver python manage.py 运行服务器

I consistently get the error: ModuleNotFoundError: No module named 'settings'我一直收到错误消息: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.我已经广泛研究并在以下 SO 问题中找到了一些解决方案,但对我来说都不是有效的解决方案。 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:编辑:我的 manage.py 文件如下所示:

#!/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 mytodo应用程序

mytodoapp mytodo应用程序

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

manage.py管理.py

Screenshot of my console我的控制台截图

Manage.py file管理.py文件

  1. open yr project folder.打开你的项目文件夹。

  2. search for the wsgi.py file and add this line.搜索 wsgi.py 文件并添加此行。

     from django.conf import settings.
  3. Then save.然后保存。

  4. locate yr manage.py file edit the找到你的 manage.py 文件编辑

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

    To

    os.environ['DJANGO_SETTINGS_MODULE' ]='projectname.settings'
  5. save and walla problem fixed.保存并解决 walla 问题。

  6. cd to yr app folder and then runserver with cd到你的应用程序文件夹,然后运行服务器

    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.在我的例子中,问题最终是因为我以某种方式修改了树结构,因此manage.py无法访问它需要的某些资源。 My solution ended up recreating a scrap django project and looking at the project structure.我的解决方案最终重新创建了一个废弃的 django 项目并查看了项目结构。 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. manage.py 位于内部yourproject文件夹之外似乎很重要。

I faced a similar issue.我遇到了类似的问题。 But I was creating the django project while using Anaconda.但是我在使用 Anaconda 时创建了 django 项目。 Once I installed sqlparse using below command the issue resolved.使用以下命令安装 sqlparse 后,问题就解决了。

conda install sqlparse

django 2.1 requires python 3.5+ which support the syntax in the manage.py file. django 2.1 需要 python 3.5+ 支持 manage.py 文件中的语法。

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.激活您的环境并验证您的 python 是 3.7 如果不是,则在创建 virtualenv 时指定您的 python 版本。

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

Try to install 'sqlparse'.尝试安装“sqlparse”。 It will work in Anaconda then.它将在 Anaconda 中运行。

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当你用pycharm打开它然后再次在终端上运行代码时

This is how your dirctory should appear nameofyourproject\这是你的目录应该如何出现 nameofyourproject\

|-- init .py |--初始化.py

|-- manage.py |-- 管理.py

|-- settings.py |-- 设置.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM