Can't import a builtin module python3.12

745 Views Asked by At

I can't import 'requests' and not because it's not installed but because in its import tree there is an unknown module 'six.moves' here is the code

admin-hox@Admin-PC:~/Documents/Projects/python/random$ python3.12
Python 3.12.0 (main, Oct  2 2023, 15:05:04) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  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 7, in <module>
    from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 11, in <module>
    from .exceptions import (
  File "/usr/lib/python3/dist-packages/urllib3/exceptions.py", line 2, in <module>
    from six.moves.http_client import IncompleteRead as httplib_IncompleteRead
ModuleNotFoundError: No module named 'six.moves'
>>> 

But it works on python3

admin-hox@Admin-PC:~/Documents/Projects/python/random$ python3
Python 3.8.10 (default, May 26 2023, 14:05:08) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> 
1

There are 1 best solutions below

0
On

In your "working" example, the python version is 3.8.10, whereas in your first example you're using 3.12.0. There are a few ways to fix this (see this question and the pyenv project). The quick way to fix this would be to symlink python3 to your installation of python3.12.

You can check where python3 points to by running readlink -f $(which python3), then run ln -s $(which python3) $(which python3.12) to create the link to the newer installation.