Solving Python AttributeError: ‘module’ object has no attribute ‘SSL_ST_INIT’

AttributeError: ‘module’ object has no attribute ‘SSL_ST_INIT’ is an error which happens in Python when pyOpenSSL is not installed or not installed correctly.

In this article I am going to explain why the error is occurring and how to solve it using multiple solutions which may work for your particular case.

Explaining the AttributeError: ‘module’ object has no attribute ‘SSL_ST_INIT’

This is an error which happens in Python when pyOpenSSL is not installed or not installed correctly.

The error message should look like the error in the example bellow, make sure you have the same error message in order to avoid confusion.

                                                                       #
    main()
  File "./inspect_sheet.py", line 12, in main
.....
    from OpenSSL import rand, crypto, SSL
    SSL_ST_INIT = _lib.SSL_ST_INIT
AttributeError: 'module' object has no attribute 'SSL_ST_INIT'
                                                                       #

Bellow I make my best attempt at solving the error and present multiple possible solutions.

Solution 1 : Correctly install pyOpenSSL

The first solution is that pyOpenSSL is not correctly installed or not installed at all.

The easiest solution is to try uninstalling pyOpenSSL using pip.

                                                                       #
pip uninstall pyopenssl
                                                                       #

After that, you can try installing pyOpenSSL again by using pip.

                                                                       #
pip install pyopenssl
                                                                       #

After this, the error should be gone and everything should work as intended. If that is not the case and pip does not work try the solution bellow.

Solution 2 : Another way to Correctly install pyOpenSSL

Sometimes, even pip does not work. The alternative solution is to run the command bellow with sudo privileges.

                                                                       #
sudo rm -r /usr/local/lib/python3.5/dist-packages/OpenSSL
                                                                       #

Now, you can install OpenSSL using the command bellow.

                                                                       #
sudo pip install pyopenssl
                                                                       #

Solution 3 : install or upgrade pyopenssl using easy_install

If the solutions above do not work try this last solution.

The solution is to install or upgrade pyopenssl using easy_install, by using the command bellow.

                                                                       #
sudo python -m easy_install --upgrade pyOpenSSL
                                                                       #

The error above was hard to deal with, I spent hours looking for a proper solution or set of solutions.

I hope this blog post was helpful.

Summing-up

That’s it fellow developers, this is the end of this guide, I hope you found this useful in solving your particular issue, if you have the means and want to help, please support our work on our Kofi account, you do not have to, but you can donate if you want.

Thank you, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/