'Invalid syntax error using Requests library with Python 2.7
I'm using the Requests library with Python 2.7 and get the following error:
Traceback (most recent call last):
File "/home/user/Documents/workspaces/controller/main.py", line 2, in <module>
import requests
File "/usr/local/lib/python2.7/dist-packages/requests/__init__.py", line 45, in <module>
from .exceptions import RequestsDependencyWarning
File "/usr/local/lib/python2.7/dist-packages/requests/exceptions.py", line 11, in <module>
from .compat import JSONDecodeError as CompatJSONDecodeError
File "/usr/local/lib/python2.7/dist-packages/requests/compat.py", line 14, in <module>
import charset_normalizer as chardet
File "/usr/local/lib/python2.7/dist-packages/charset_normalizer/__init__.py", line 24, in <module>
from .api import from_bytes, from_fp, from_path, normalize
File "/usr/local/lib/python2.7/dist-packages/charset_normalizer/api.py", line 38
sequences: bytes,
^
SyntaxError: invalid syntax
I installed Requests with the following command:
sudo pip install requests -t /usr/local/lib/python2.7/dist-packages
The file I'm running is just main.py with the content:
import json
import requests
if __name__ == "__main__":
print('hello world')
I have both Python 3.6 and 2.7 installed and selected Python 2.7 as the interpreter in VS Code.
Solution 1:[1]
I just ran into this issue; the package is expecting to be executed with Python 3, not Python 2.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | livgust |