Python 语法错误:生成器表达式必须加括号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51265858/
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 19:47:54  来源:igfitidea点击:

SyntaxError: Generator expression must be parenthesized

pythondjango

提问by sagar

I just installed django and after installing that I created a django project and was trying to run django server by command:

我刚刚安装了 django,安装后我创建了一个 django 项目,并试图通过命令运行 django 服务器:

python manage.py runserver

After that I'am getting error as: SyntaxError: Generator expression must be parenthesized

之后,我收到错误消息: SyntaxError: Generator expression must be parenthesized

error screenshot

错误截图

回答by Antwane

TL; DR: Upgrade Django to version 1.11.17+ or 2.0+

TL; DR:将 Django 升级到 1.11.17+ 或 2.0+ 版本



This error is a known incompatibilityrelated to Python issue #32012. Projects based on Django 1.11.16 and below will raise this exception when started with Python 3.7. A patch for this issue has been merged into Django 2.0 and 2.1 branchesand cherry-picked later into Django 1.11.17.

此错误Python 问题 #32012相关的已知不兼容性。当使用 Python 3.7 启动时,基于 Django 1.11.16 及以下版本的项目将引发此异常。此问题的补丁已合并到 Django 2.0 和 2.1 分支中,并在稍后被挑选到Django 1.11.17 中

Note: Python 3.7 is officially supported by Django 1.11.17and above, including any 2.x branch.

注意:Django 1.11.17及更高版本正式支持 Python 3.7 ,包括任何 2.x 分支

回答by Sudhanshu Shekhar Jha

Generator expression must be parenthesized

生成器表达式必须加括号

> Update Django version to 1.11.17

> 更新 Django 版本至 1.11.17

pip install django==1.11.17

回答by Tess

Had same issue. This is how I changed to django version 2.0 and used python3

有同样的问题。这就是我更改为 django 2.0 版并使用 python3 的方式

  • $pip3 install django==2.0
  • $python3 manage.py runserver
  • $pip3 安装 django==2.0
  • $python3 manage.py runserver

回答by Saroj Kumar

This is due to the version incompatibility.Just we need to upgrade the Django version to 2.1. Run the command in cmd:Pip install django==2.1. this will resolve the issue

这是版本不兼容造成的,我们需要将Django版本升级到2.1。在cmd中运行命令:Pip install django==2.1。这将解决问题

回答by amit Kumar

  1. Install this version: pip install django==1.11.17
  2. Run cmd.
  3. go to your project folder.
  4. python manage.py runserver
  5. it will give a URL for server and you are good to go.
  1. 安装这个版本: pip install django==1.11.17
  2. 运行命令。
  3. 转到您的项目文件夹。
  4. python manage.py runserver
  5. 它将为服务器提供一个 URL,您很高兴。

回答by Ahmet

I just faced an Error like this. I was using Django-1.11.10. I deleted it and installed Django 2.0

我刚刚遇到了这样的错误。我使用的是 Django-1.11.10。我删除了它并安装了 Django 2.0

Problem is solved.

问题解决了。

But if you are using ForeignKey in you model.py files it must be problem again. You should update your coding to 2.0 versiong insted of older versiyon.

但是,如果您在 model.py 文件中使用 ForeignKey,那肯定又是个问题。您应该将您的编码更新为 2.0 版本,而不是旧版本。

Example:

例子:

django older version

Django 旧版本

user = models.ForeignKey('auth.User', related_name='posts')

django 2.0

Django 2.0

user = models.ForeignKey('auth.User', related_name='posts', on_delete=models.CASCADE,)

回答by CAP.tech

Just open file: venv/lib/python3.7/site-packages/django/contrib/admin/widgets.pyand replace the lines

只需打开文件: venv/lib/python3.7/site-packages/django/contrib/admin/widgets.py并替换行

related_url += '?' + '&'.join(
    '%s=%s' % (k, v) for k, v in params.items(),)

with

related_url += '?' + '&'.join('%s=%s' % (k, v) for k, v in params.items())

回答by Holovin

Just open file venv/lib/python3.7/site-packages/django/contrib/admin/widgets.pyand replace the lines

只需打开文件venv/lib/python3.7/site-packages/django/contrib/admin/widgets.py并替换行

related_url += '?' + '&'.join(
    '%s=%s' % (k, v) for k, v in params.items(),
)

with

related_url += '?' + '&'.join('%s=%s' % (k, v) for k, v in params.items())