简体   繁体   English

Django:“python manage.py runserver”给出“分段错误”错误

[英]Django: 'python manage.py runserver' gives "Segmentation fault" error

I just installed Python and Django for the first time while following this tutorial on the Django site, and everything up to this part worked just fine and produced no errors.我刚刚安装了 Python 和 Django,同时按照 Django 站点上的本教程进行操作,到目前为止的所有内容都运行良好并且没有产生任何错误。

Now I'm at the "The development server" section, and ran the command python manage.py runserver while in my project directory where the manage.py is located and I get this error:现在我在“开发服务器”部分,在manage.py所在的项目目录中运行命令python manage.py runserver时出现此错误:

Segmentation fault分段故障

Nothing else.没有其他的。

Anyone know what's going on here and how I can solve this?任何人都知道这里发生了什么以及我该如何解决这个问题?

If it matters, this is my manage.py file (the django in the from django.core.management... line is marked as an error in my IDE (PyCharm) and the error says Unresolved reference 'django' ):如果重要的话,这是我的 manage.py 文件( from django.core.management...行中的django在我的 IDE (PyCharm) 中被标记为错误并且错误显示Unresolved reference 'django' ):

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

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "davilex.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

This happened to me in a Django project where I had a lot of packages that had C-extensions.这发生在我的 Django 项目中,我有很多具有 C 扩展的包。 After I ran an Ubuntu system package update, I suddenly found that the manage.py command seg faulted.运行完Ubuntu系统包更新后,突然发现manage.py命令seg出错了。 Turns out, some C-extensions link themselves to system libraries, and if a package update changes those libraries, it could cause the C-extensions to crash.事实证明,一些 C 扩展将自己链接到系统库,如果包更新更改了这些库,可能会导致 C 扩展崩溃。

Since I had installed everything inside a Python virtualenv, I just deleted and regenerated it, and that fixed the error.因为我已经在 Python virtualenv 中安装了所有东西,所以我只是删除并重新生成它,这修复了错误。

I had this same issue following the tutorial pretty much verbatim (except that I am using git-bash in Windows, and used "virtualenv" in place of "mkvirtualenv" and "source projectdir/Scripts/activate" in place of "workon projectdir".我几乎一字不差地按照教程遇到了同样的问题(除了我在 Windows 中使用 git-bash,并使用“virtualenv”代替“mkvirtualenv”和“source projectdir/Scripts/activate”代替“workon projectdir” .

Running跑步

python -vvvvv manage.py runserver

Gave me the warning that I had migrations that needed running (which the tutorial says to ignore) with警告我有需要运行的迁移(教程说要忽略)

python manage.py migrate

Which is presumably not a step you can really skip in some cases, despite what the tutorial says.在某些情况下,这可能不是您可以真正跳过的步骤,不管教程怎么说。 After this, runserver worked without error.在此之后,runserver 正常运行。

I had a similar issue while setting up a project on mac.我在 mac 上设置项目时遇到了类似的问题。 In the end, turned out it because of value set in DATABASES.最后,由于在 DATABASES 中设置的值,结果变成了它。

DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'blue9',
    'USER': 'root',
    'PASSWORD': 'root',
    'PORT': '',
    'HOST': ''
}

Empty value of the 'PORT' and 'HOST' was causing the problem. “PORT”和“HOST”的空值导致了问题。 These keys should not have an empty value.这些键不应具有空值。 Either provide a finite value or don't specify them at all.要么提供有限值,要么根本不指定它们。

I think this issue related database config, first step check postgres library install config我认为这个问题与数据库配置有关,第一步检查 postgres 库安装配置

pip install psycopg2-binary==2.8.5

second step check database config in django project settings:第二步检查 django 项目设置中的数据库配置:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': '<database_name>',
    'USER': '<user_name>',
    'PASSWORD': '<password>',
    'HOST': '<host-name>',
    'PORT': '<port number : default : 5432>',
}

} }

  1. install PyMysql:安装 PyMysql:

    python3 -m pip install PyMysql

  2. Setting add mysql conf:设置添加 mysql conf:

     DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'your database name', 'USER': 'your user', 'PASSWORD': 'your pwd', 'HOST': 'ip', 'PORT': 'port', } }
  3. __init__.py add __init__.py 添加
    import pymysql
    pymysql.install_as_MySQLdb()

enjoy~享受~

Just put in sudo before running migration or run server command. 只需在运行迁移或运行服务器命令之前输入sudo。 For example if you want to run migrate then use 例如,如果要运行迁移,请使用

sudo python manage.py migrate

or if you want to run server 或者如果你想运行服务器

sudo python manage.py runserver

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

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