问题:如何在自动生成的manage.py上解决SyntaxError?

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

我创建了一个“ mysite”虚拟项目(我的第一个项目),并尝试对其进行测试而未对其进行更改。

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

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

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

如果可能有用,我将添加一些设置数据

$ 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

有人能帮帮我吗?

更新:添加自动生成的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)

I’m following the Django tutorial 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.

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.

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

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)

回答 0

确保将Django连接到哪个python版本(如果使用任何版本,请确保激活虚拟环境)。

当您仅使用django安装时

pip install django 

那你必须跑

python manage.py startapp <yourApp name>

否则,如果您使用过:

pip3 install django

那你必须跑

python3 manage.py startapp <yourapp name>

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

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

When you install django using just

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


回答 1

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

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


回答 2

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

现在它将工作-> runserver。

无需删除部分!

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

now it will work -> runserver .

No need to delete exc part !


回答 3

我遇到了相同的情况,但这通过使用特定的python 3.6来解决,如下所示:

python3.6 manage.py runserver

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

python3.6 manage.py runserver

回答 4

只需激活您的虚拟环境即可。

Just activate your virtual environment.


回答 5

这是我遇到的一个简单解决方案。您是否激活了虚拟环境?

我的终端截图

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

my terminal screenshot


回答 6

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

  1. 创建一个新目录并CD进入。

    mkdir testcd 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到您的项目并运行Project:

    cd myprojectpython manage.py runserver

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

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.

  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/


回答 7

经过精确的指令测试(使用python2或python3而不是仅仅使用“ python”),我认为无论本教程怎么说,这仅适用于python3。

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.


回答 8

要运行Python版本3,您需要使用python3而不是python

因此,最终命令将是:

python3 manage.py runserver

For running Python version 3, you need to use python3 instead of python.

So, the final command will be:

python3 manage.py runserver

回答 9

解决方案很简单。来自manage.py的exceptions是因为当使用python运行命令时,Django无法预测确切的python版本,例如您可能具有3.6、3.5、3.8,并且可能只有此版本之一pip模块用于安装Django来解决这两种使用:

./manage.py `enter code here`<command>

或使用确切的python版本(xx)表示:

pythonx.x manage.py <command>

否则,使用虚拟环境会派上用场,因为它可以轻松将任何pip django模块与python版本相关联

  • 用pyenv或virtualenv创建环境
  • 激活(例如在virtualenv => virtualenv env中)
  • 使用python manage.py命令运行

The solution is straightforward. the exception from manage.py is because when running the command with python, Django is unable to predict the exact python version, say you may have 3.6, 3.5, 3.8 and maybe just one of this versions pip module was used to install Django to resolve this either use:

./manage.py `enter code here`<command>

or using the exact python version(x.x) stands:

pythonx.x manage.py <command>

else the use of virtual environments can come in handy because its relates any pip django module easily to python version

  • create env with pyenv or virtualenv
  • activate (e.g in virtualenv => virtualenv env)
  • run using python manage.py command

回答 10

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

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


回答 11

我解决了同样的情况。

安装版本

python 3.6,django 2.1

情况

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

错误

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

原因

我的python路径从python-3.6更改为python-2.7。(3.6在我的电脑上是正确的。)

修复python路径。

I solved same situation.

INSTALLED VERSION

python 3.6, django 2.1

SITUATION

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

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.)

SOLUTION

Fix python path.


回答 12

我遇到了完全相同的错误,但是后来我发现我忘记激活装有django和其他必需软件包的conda环境。

解决方案:创建安装了django的conda或虚拟环境,并在使用以下命令之前将其激活: $ python manage.py migrate

I had the exact same error, but then I later found out that I forget to activate the conda environment which had django and other required packages installed.

Solution: Create a conda or virtual environment with django installed, and activate it before you use the command: $ python manage.py migrate


回答 13

django-admin可能是错误的文件。我遇到了相同的问题,在另一台计算机上找不到相同的设置流程。

比较两个项目后,我在manage.py和settings.py中发现了一些区别,然后我意识到我创建了2.0 django项目,但使用python2运行了该项目。

which django-admin在iterm中运行

/Library/Frameworks/Python.framework/Versions/3.6/bin/django-admin

看起来我在python3中有一个django-admin,但我不知道为什么,所以我尝试获取正确的django-amin。

pip show django

然后我得到了

Name: Django
Version: 1.11a1
Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Home-page: https://www.djangoproject.com/
Author: Django Software Foundation
Author-email: foundation@djangoproject.com
License: BSD
Location: /Library/Python/2.7/site-packages
Requires: pytz

在中/Library/Python/2.7/site-packages,我找到了django-admin

/Library/Python/2.7/site-packages/django/bin/django-admin.py

所以我再次创建了项目

/Library/Python/2.7/site-packages/django/bin/django-admin.py startproject myproject

然后跑

cd myproject
python manage.py runserver

成功🎉

The django-admin maybe the wrong file.I met the same problem which I did not found on a different computer the same set-up flow.

After comparing two project, I found several difference at manage.py and settings.py, then I realized I created 2.0 django project but run it with python2.

runwhich django-adminin iterm

/Library/Frameworks/Python.framework/Versions/3.6/bin/django-admin

It looks like I got a django-admin in python3 which I didn’t know why.So I tried to get the correct django-amin.

pip show django

then I got

Name: Django
Version: 1.11a1
Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Home-page: https://www.djangoproject.com/
Author: Django Software Foundation
Author-email: foundation@djangoproject.com
License: BSD
Location: /Library/Python/2.7/site-packages
Requires: pytz

In/Library/Python/2.7/site-packages, I found the django-admin

/Library/Python/2.7/site-packages/django/bin/django-admin.py

So I created project again by

/Library/Python/2.7/site-packages/django/bin/django-admin.py startproject myproject

then run

cd myproject
python manage.py runserver

succeeded🎉


回答 14

我们必须在项目内部而不是在项目外部创建一个虚拟环境。

We have to create a virtual environment inside the project, not outside the project.. Then it will solve..


回答 15

看来您的计算机上有多个版本的Python。尝试删除其中一个,并保留唯一用于开发应用程序的版本。

如果需要,您可以升级您的版本,但请确保您的计算机上只有一个Python版本。

我希望这有帮助。

It seems you have more than one version of Python on your computer. Try and remove one and leave the only version you used to develop your application.

If need be, you can upgrade your version, but ensure you have only one version of Python on your computer.

I hope this helps.


回答 16

我遇到了同样的异常,因为我忘记了激活虚拟环境。

I landed on the same exact exception because I forgot to activate the virtual environment.


回答 17

可能是以下原因造成的,

1. The virtual environment is not enabled
2. The virtual environment is enabled but the python version is different

创建虚拟环境

$ virtualenv --python=python3 venv

激活虚拟环境

$ source venv/bin/activate

The following could be the possible reasons,

1. The virtual environment is not enabled
2. The virtual environment is enabled but the python version is different

To create virtual environment

$ virtualenv --python=python3 venv

To activate the virtual environment

$ source venv/bin/activate

回答 18

我也遇到了同样的错误。 在此处输入图片说明

然后,我回到了环境文件夹所在的文件夹,却忘记了激活虚拟环境,因此只有我遇到了此错误。 在此处输入图片说明

转到该文件夹​​并激活虚拟环境。

$ source env/bin/activate

I was also getting the same error. enter image description here

Then I went back to the folder where the environment folder is there and I forgot to activate a Virtual environment so only I was getting this error. enter image description here

Go to that folder and activate the virtual environment.

$ source env/bin/activate

回答 19

另外,本教程还建议使用虚拟环境(请参阅Django文档:https : //docs.djangoproject.com/en/2.0/topics/install/#installing-official-release “)。您可以使用pipenv --three。您已经安装django pipenv install django并使用激活了虚拟环境pipenv shell,执行时python将引用python3python manage.py runserver

Pipenv文档:https://pipenv.kennethreitz.org/

Also, the tutorial recommends that a virtual environment is used (see Django documentation: https://docs.djangoproject.com/en/2.0/topics/install/#installing-official-release“). You can do this with pipenv --three. Once you’ve installed django with pipenv install django and activated your virtual environment with pipenv shell, python will refer to python3 when executing python manage.py runserver.

Pipenv documentation: https://pipenv.kennethreitz.org/


回答 20

我想知道的是,虽然django已安装到容器中,但它可能不在您运行命令的主机中。然后该命令将如何运行。因此,由于没有上述解决方案对我有用。

我找到了运行中的容器并使用进入了运行中的容器,docker exec -it <container> bash 然后在docker容器中运行了命令。当我们拥有大容量的容器时,所做的更改也会在本地反映出来。可以运行的命令都可以在运行容器中运行

What am I wondering is though the django is installed to the container it may not be in the host machine where you are running the command. Then how will the command run. So since no above solutions worked for me.

I found out the running container and get into the running container using docker exec -it <container> bash then ran the command inside docker container. As we have the volumed container the changes done will also reflect locally. What ever command is to be run can be run inside the running container


回答 21

对于将来的读者,我也有同样的问题。原来直接从网站安装Python以及Anaconda的另一个版本导致了此问题。我必须卸载Python2.7并仅保留anaconda作为唯一发行版。

For future readers, I too had the same issue. Turns out installing Python directly from website as well as having another version from Anaconda caused this issue. I had to uninstall Python2.7 and only keep anaconda as the sole distribution.


回答 22

您是否已进入django的虚拟环境?运行python -m venv myvenv,如果你还没有安装。

Have you entered the virtual environment for django? Run python -m venv myvenv if you have not yet installed.


回答 23

做就是了:

pipenv shell

然后重复:

python manage.py runserver

并且请勿from exc按照上述建议删除。

干杯!

Just do:

pipenv shell

then repeat:

python manage.py runserver

and don’t delete from exc as suggested above.

cheers!


回答 24

我有同样的问题,可以解决。它与您安装的Django版本有关,其中一些不被python 2.7支持。如果您使用pip安装了Django,则意味着您正在安装python 2.7可能不支持的最新版本,您可以在此处获取有关它的更多信息。我建议在安装过程中使用python 3或指定Django的版本(对于python 2.7为1.11)。

I had same problem and could solve it. It is related to the version of Django you’ve installed, some of them are not supported by python 2.7. If you have installed Django with pip, it means that you are installing the latest version of that which probably is not supported in python 2.7, You can get more information about it here. I would suggest to python 3 or specify the version of Django during installing (which is 1.11 for python 2.7).


回答 25

我解决了这个问题,以卸载Python的多个版本。查看Django官方文档,了解Python兼容性。

Python兼容性

Django 2.1支持Python 3.5、3.6和3.7。Django 2.0是支持Python 3.4的最新版本。”

manage.py文件

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

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

如果从此代码的第二行中删除“ from exc”,则由于多个版本的Python会产生另一个错误。

I solved this problem to uninstall the multiple version of Python. Check Django Official Documentation for Python compatibility.

Python compatibility

Django 2.1 supports Python 3.5, 3.6, and 3.7. Django 2.0 is the last version to support Python 3.4.”

manage.py file

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

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

If removing “from exc” from second last line of this code will generate another error due to multiple versions of Python.


回答 26

通过以下命令激活环境

  source  pathetoYourEnv/bin/activate

然后运行命令

python manage.py runserver

activate env by the Following Command

  source  pathetoYourEnv/bin/activate

then run command

python manage.py runserver

回答 27

您应该启动虚拟环境

怎么做?

首先使用终端cd进入包含manage.py的目录

然后输入 $source <myvenv>/bin/activate 用您的虚拟环境名称 replace,不带尖括号。

另一个问题可能是您的根目录和venv不匹配。结构应如下所示:

|-website
     ..facebook
     ..manage.py
     ..myvenv
     ..some other files

那是您的虚拟环境,manage.py应该在同一文件夹中。解决方案是重新启动项目。如果您遇到此错误,则必须尚未编写任何代码,因此请重新启动。

You should start your Virtual Environment,

how to do it?

first with terminal cd into the directory containing manage.py

then type $source <myvenv>/bin/activate replace with you Virtual Environment name, without angular brackets.

Another issue can that your root directory and venv mis-match. The structure should be something like this:

|-website
     ..facebook
     ..manage.py
     ..myvenv
     ..some other files

That is your virtual environment and manage.py should be in the same folder. Solution to that is to restart the project. If you are facing this error you must haven’t coded anything yet, so restart.


回答 28

当我使用以下方法激活虚拟环境时,也解决了我的问题:

source bin/activate

Solved my problem too when I activated my virtual environment using:

source bin/activate

回答 29

我遇到了这个问题(Mac),并按照下一页上的说明安装和激活虚拟环境

https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/

$ cd [顶级django-project-dir]

$ python3 -m pip install –user virtualenv

$ python3 -m venv env

$ source env / bin / activate

安装并激活虚拟环境后,我对其进行了检查

$哪个Python

然后我将django安装到虚拟环境中

$ pip install django

然后我可以运行我的应用

$ python3 manage.py运行服务器

当我到达本教程的下一部分时

$ python manage.py startapp民意调查

我遇到了另一个错误:

     File "manage.py", line 16

   ) from exc
            ^

   SyntaxError: invalid syntax

我删除了

from exc

然后创建民意调查目录

I had this issue (Mac) and followed the instructions on the below page to install and activate the virtual environment

https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/

$ cd [ top-level-django-project-dir ]

$ python3 -m pip install –user virtualenv

$ python3 -m venv env

$ source env/bin/activate

Once I had installed and activated the virtual env I checked it

$ which python

Then I installed django into the virtual env

$ pip install django

And then I could run my app

$ python3 manage.py runserver

When I got to the next part of the tutorial

$ python manage.py startapp polls

I encountered another error:

     File "manage.py", line 16

   ) from exc
            ^

   SyntaxError: invalid syntax

I removed

from exc

and it then created the polls directory


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。