'Python doesn't recognize standard library modules

I can't import standard library modules (like ssl and tkinter) when I'm not running Python from "/Python3.8.10/Modules" directory:

/Python3.8.10/Modules/# python
>>> import ssl
Traceback(most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/ssl.py", line 98, in <module>
    import _ssl            # if we can't import it, let the error propagate
ModuleNotFoundError: No module named '_ssl'

Same error happens if I try to import "_ssl" directly.
When I'm running Python in /Python3.8.10/Modules" directory everything works fine.
What could be causing this error?

I am using Debian and I used this tutorial https://linuxhint.com/install-python-debian-10/ to install it.



Solution 1:[1]

Judging from your comment you didn't install Python, but just downloaded and unzipped the Source Code.

I am on Debian. I unzipped (untar-ed) Python-3.8.10.tgz (Which comes from the Python website).

If you download the tar ball directly you still have to build Python by cd-ing into the Location you extracted it to and then running:

$ ./configure --enable-optimizations
$ make -j 4
$ sudo make altinstall  # altinstall will install Python3.8 at /usr/bin/Python3.8 instead of /usr/bin/Python which could overwrite previously installed versions.
$ sudo ln -s /usr/bin/python /usr/bin/python3.8 # ONLY if you want Python3.8 to be available as just python

Edit: Those are just the commands that i remembered out of my head which worked for me on various systems. They also have some requirements (build-essentials and make being two of them) so this might not work for you.

See here or probably on any site when searching for build Python from source in your favorite search engine.

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