简体   繁体   中英

SyntaxError: invalid syntax when creating virtualenv

I wanted to create a virtualenv for python 2.7 (I'm using 3.7)

I run the cmd as admin (on windows 10) :

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

An I got the error:

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

When I Launch Python2.7 and try to import the logging module: It show the same error but When I retype it. It work just fine:

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

I can't figure out why it does that.

I have already configured a virtual env with python 3.6

Any help would be appreciated.

You have a version problem. This line

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

is being run under Python 2.7, but is Python 3.6 (or 3.7) and will give you a syntax error in earlier versions. In your case I would treat that Python 2.7 installation as irretrievably broken, because it is clear that the standard library isn't to be trusted to all be of a consistent version.

解决了该问题,删除了c:\\ Python27目录,然后使用安装工具卸载python 2.7,重新启动计算机并重新安装python 2.7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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