Python 如何解决自动生成的 manage.py 上的 SyntaxError?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42611593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:57:26  来源:igfitidea点击:

How to solve SyntaxError on autogenerated manage.py?

pythonsyntax-error

提问by Frank

I'm following the Django tutorial https://docs.djangoproject.com/es/1.10/intro/tutorial01/

我正在关注 Django 教程https://docs.djangoproject.com/es/1.10/intro/tutorial01/

I've created a "mysite" dummy project (my very first one) and try to test it without altering it.

我创建了一个“mysite”虚拟项目(我的第一个)并尝试在不更改它的情况下对其进行测试。

django-admin startproject mysite
cd mysite
python manage.py runserver

File "manage.py", line 14
) from exc
^
SyntaxError: invalid syntax

I'm getting a SyntaxError on a file that was generated by the system itself. And I seem unable to find anyone else who has gone through the same issue.

我在系统本身生成的文件上收到 SyntaxError 错误。而且我似乎找不到其他遇到过同样问题的人。

I'll add some data of my setup in case it may be of use

我会添加一些我的设置数据,以防它可能有用

$ vpython --version
Python 2.7.12
$ pip --version
pip 9.0.1 from /home/frank/.local/lib/python2.7/site-packages (python 2.7)
$ python -m django --version
1.10.6

Can somebody please help me?

有人能帮帮我吗?

Update: adding contents of autogenerated manage.py

更新:添加自动生成的 manage.py 的内容

cat manage.py 
#!/usr/bin/env python3
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.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)

回答by lobsang

Make sure which python version you connect the django with (Make sure to activate the virtual env if you are using any).

确保您使用哪个 python 版本连接 django (如果您正在使用任何虚拟环境,请确保激活虚拟环境)。

When you install django using just

当您仅使用安装 django 时

pip install django 

then you have to run

那么你必须跑

python manage.py startapp <yourApp name>

else if you have used:

否则,如果您使用过:

pip3 install django

then you have to run

那么你必须跑

python3 manage.py startapp <yourapp name>

Refer:
enter image description here

参考:
在此处输入图片说明

回答by Fernando

You can try with python3 manage.py runserver. It works for me.

您可以尝试使用python3 manage.py runserver. 这个对我有用。

回答by Aditya Sinha

you should activate your virtual environment . In terminal -> source env/bin/activate now there will be ----> (env) in your terminal displayed !

你应该激活你的虚拟环境。在终端 -> source env/bin/activate 现在将在您的终端中显示 ----> (env) !

now it will work -> runserver .

现在它将工作 - > runserver 。

No need to delete exc part !

无需删除 exc 部分!

回答by Martin Karari

I was experiencing the same but this was solved by running with specific python 3.6 as below:

我遇到了同样的问题,但是通过使用特定的 python 3.6 运行解决了这个问题,如下所示:

python3.6 manage.py runserver

回答by Vipin Mohan

Its a simple solution actually one i just ran into. Did you activate your virtual environment?

它是一个简单的解决方案,实际上是我刚刚遇到的一个。您是否激活了虚拟环境?

my terminal screenshot

我的终端截图

回答by Sbk3824

It's best to create a virtual environment and run your Django code inside this virtual environment, this helps in not changing your existing environments. Here are the basic steps to start with the virtual environment and Django.

最好创建一个虚拟环境并在这个虚拟环境中运行你的 Django 代码,这有助于不改变你现有的环境。以下是从虚拟环境和 Django 开始的基本步骤。

  1. Create a new Directory and cd into it.

    mkdir test, cd test

  2. Install and Create a Virtual environment.

    python3 -m pip install virtualenv virtualenv venv -p python3

  3. Activate Virtual Environment: source venv/bin/activate

  4. Install Django: pip install django

  5. Start a new project:?django-admin startproject myproject

  6. cd to your project and Run Project:

    cd myproject, python manage.py runserver

  7. You can see your project here : http://127.0.0.1:8000/

  1. 创建一个新目录并 cd 到其中。

    mkdir test, cd test

  2. 安装并创建虚拟环境。

    python3 -m pip install virtualenv virtualenv venv -p python3

  3. 激活虚拟环境: source venv/bin/activate

  4. 安装 Django: pip install django

  5. 开始一个新项目:?django-admin startproject myproject

  6. cd 到您的项目并运行项目:

    cd myproject, python manage.py runserver

  7. 你可以在这里看到你的项目: http://127.0.0.1:8000/

回答by Esir Kings

Just activate your virtual environment.

只需激活您的虚拟环境。

回答by Frank

After testing with precise instructions (using python2 or python3 instead of just "python") I've constated that no matter what the tutorial says, this works ONLY with python3.

在使用精确的指令(使用 python2 或 python3 而不仅仅是“python”)进行测试后,我已经确定无论教程说什么,这仅适用于 python3。

回答by Wasique Ansari

You must activate virtual environment where you have installed django. Then run this command - python manage.py runserver

您必须激活已安装 django 的虚拟环境。然后运行这个命令 - python manage.py runserver

回答by Otsuki Takaya

I solved same situation.

我解决了同样的情况。

INSTALLED VERSION

安装版本

python 3.6, django 2.1

蟒蛇 3.6,Django 2.1

SITUATION

情况

I installed Node.js in Windows 10. After python manage.py runservercaused error.

我在 Windows 10 中安装了 Node.js。在python manage.py runserver导致错误之后。

ERROR

错误

File "manage.py", line 14
) from exc
^
SyntaxError: invalid syntax

REASON

原因

My python path changed to python-2.7 from python-3.6. (3.6 is correct in my PC.)

我的python路径从python-3.6改为python-2.7。(3.6 在我的 PC 中是正确的。)

SOLUTION

解决方案

Fix python path.

修复python路径。