【问题标题】:SyntaxError: invalid syntax when creating virtualenvSyntaxError:创建 virtualenv 时语法无效
【发布时间】:2019-06-03 09:49:52
【问题描述】:

我想为 python 2.7 创建一个 virtualenv(我正在使用 3.7)

我以管理员身份运行 cmd(在 Windows 10 上):

C:\WINDOWS\system32>virtualenv -p C:\Python27\python.exe py27

我得到了错误:

Running virtualenv with interpreter C:\Python27\python.exe
Traceback (most recent call last):
  File "c:\path\to\virtualenv.py", line 26, in <module>
    import logging
  File "C:\Python27\lib\logging\__init__.py", line 43, in <module>
    import threading
  File "C:\Python27\lib\threading.py", line 15, in <module>
    from collections import deque
  File "C:\Python27\lib\collections\__init__.py", line 55
    raise AttributeError(f'module {__name__!r} has no attribute {name!r}')
                                                                        ^
SyntaxError: invalid syntax

当我启动 Python2.7 并尝试导入日志记录模块时:它显示相同的错误,但当我重新键入它时。它工作得很好:

>>> import logging
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\logging\__init__.py", line 43, in <module>
    import threading
  File "C:\Python27\lib\threading.py", line 15, in <module>
    from collections import deque
  File "C:\Python27\lib\collections\__init__.py", line 55
    raise AttributeError(f'module {__name__!r} has no attribute {name!r}')
                                                                        ^
SyntaxError: invalid syntax
>>> import logging
>>> logging.warning('hello word')
WARNING:root:hello word
>>>

我不知道为什么会这样。

我已经用 python 3.6 配置了一个虚拟环境

任何帮助将不胜感激。

【问题讨论】:

  • 你能检查一下你下载的python 2.7的包是否与你的系统配置(32位或64位)匹配吗?
  • 第二:可能你的 2.7 虚拟环境中没有安装 logging 模块。尝试在该虚拟环境中使用pip install logging 安装它
  • 我重新安装了 64 位的 python 2.7,我仍然有同样的错误。我在环境变量中没有 python 2.7,因此 已安装日志记录模块 我设法获得了 logging.warning('hello word') WARNING:root:hello word 但仅在第二次尝试时

标签: python logging module virtualenv


【解决方案1】:

您有版本问题。这一行

raise AttributeError(f'module {__name__!r} has no attribute {name!r}')

在 Python 2.7 下运行,但它是 Python 3.6(或 3.7),在早期版本中会出现语法错误。在您的情况下,我会将 Python 2.7 安装视为不可挽回的损坏,因为很明显,标准库不能被信任为一致的版本。

【讨论】:

    【解决方案2】:

    问题解决删除c:\Python27目录,然后使用安装工具卸载python 2.7,重启电脑,重新安装Python 2.7

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 2013-12-19
      • 2018-12-14
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多