SyntaxError: invalid syntax when creating virtualenv

自闭症网瘾萝莉.ら 提交于 2021-02-11 17:50:58

问题


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.


回答1:


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.




回答2:


The problem was resolved deleting the c:\Python27 directory, Then Uninstalling python 2.7 using the setup tool, restarting the computer and reinstalling Python 2.7



来源:https://stackoverflow.com/questions/56424857/syntaxerror-invalid-syntax-when-creating-virtualenv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!