Skip to content
Advertisement

SyntaxError: invalid syntax upon importing modules

On my Debian distro, I am not sure if I corrupted my installation, but I get the following syntax error every time I import something with python.

My file:

#!/usr/bin/env python
import requests

Output:

└─$ python temp.py                  
Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 919, in _find_spec
AttributeError: '_SixMetaPathImporter' object has no attribute 'find_spec'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/useris/temp.py", line 3, in <module>
    import requests
  File "/usr/lib/python3/dist-packages/requests/__init__.py", line 43, in <module>
    import urllib3
  File "/usr/lib/python3/dist-packages/urllib3/__init__.py", line 11, in <module>
    from . import exceptions
  File "/usr/lib/python3/dist-packages/urllib3/exceptions.py", line 3, in <module>
    from six.moves.http_client import IncompleteRead as httplib_IncompleteRead
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 982, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 921, in _find_spec
  File "<frozen importlib._bootstrap>", line 898, in _find_spec_legacy
  File "<frozen importlib._bootstrap>", line 431, in spec_from_loader
  File "/usr/lib/python3/dist-packages/six.py", line 216, in is_package
    return hasattr(self.__get_module(fullname), "__path__")
  File "/usr/lib/python3/dist-packages/six.py", line 118, in __getattr__
    _module = self._resolve()
  File "/usr/lib/python3/dist-packages/six.py", line 115, in _resolve
    return _import_module(self.mod)
  File "/usr/lib/python3/dist-packages/six.py", line 82, in _import_module
    __import__(name)
  File "/usr/lib/python3.9/http/client.py", line 1367, in <module>
    import ssl
  File "/home/useris/ssl.py", line 10
    print "++++make sure your testssl.sh is up to date, old versions will give incorrect results (testeded with 3.0rc2)++++n"
          ^
SyntaxError: invalid syntax
└─$ python --version
Python 3.9.1+
└─$ pip3 -V                                                                                                     
pip 22.2 from /usr/lib/python3/dist-packages/pip (python 3.9)
└─$ sudo pip3 install requests
Requirement already satisfied: requests in /usr/lib/python3/dist-packages (2.25.1)

What I also don’t understand is that error is referencing a totally different file (ssl.py), which I created previously and has nothing to do with the file I am running.

Any ideas how to resolve this?

Advertisement

Answer

import ssl should be reading something like /usr/lib/lib/python3/ssl.py. However, by creating your ssl.py in the same directory, you hijack it, and it gets loaded instead. The error comes from the fact that your ssl.py is written with Python 2 synax, and you are executing with a Python 3 interpreter (see print is a function). If this was corrected, you would likely have another error, because your ssl.py is probably not doing what requests is expecting ssl to do.

The solution is simple: remove the impostor ssl.py.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement