I am trying to run a script that requires secure connections, but I keep hitting an error saying the SSL module is not found. I’ve tried reinstalling Python, but the issue persists. Is this a problem with how OpenSSL is installed on my OS, or am I missing a specific build flag during the installation of my virtual environment? I'm currently on a Linux-based system.
3 answers
This error usually occurs when Python is compiled from source without the necessary OpenSSL development headers present on the system. On Ubuntu or Debian, you need to run sudo apt-get install libssl-dev before you build Python. If you are using pyenv, you might need to point the compiler to your OpenSSL directory using environment variables like LDFLAGS and CPPFLAGS. Once the headers are installed, you must re-run the make and make install commands for your Python version so it can properly link the _ssl extension during the build process.
I’ve installed the headers as suggested, but my virtual environment still seems to be pointing to the old system binary. Is there a way to verify the SSL path Python is using?
Most of the time, this is just a missing libssl package on your OS. Install that, rebuild Python, and the error should vanish immediately.
Patricia is right. For macOS users encountering this, using brew install openssl and then providing the brew path to your installer is the standard fix for this exact same headache.
You can verify the linked path by running import ssl; print(ssl.OPENSSL_VERSION) in your Python shell. If it throws an error there, your binary wasn't linked at all. If it works, it will show the version. Often, simply deleting and recreating your virtual environment after installing libssl-dev on the host machine solves the mapping issue because the venv will then reference the newly updated system interpreter.