Python AttributeError: ‘ module’对象没有属性‘ SSL_ST_INIT’

我的一个 Python 脚本在以下方面出了问题:

Traceback (most recent call last):
File "./inspect_sheet.py", line 21, in <module>
main()
File "./inspect_sheet.py", line 12, in main
workbook_name=workbook_name,
File "./google_sheets.py", line 56, in __init__
self.login()
File "./google_sheets.py", line 46, in login
self.client = gspread.authorize(credentials)
File "/usr/local/lib/python2.7/site-packages/gspread/client.py", line 335, in authorize
client.login()
File "/usr/local/lib/python2.7/site-packages/gspread/client.py", line 98, in login
self.auth.refresh(http)
File "/usr/local/lib/python2.7/site-packages/oauth2client/client.py", line 598, in refresh
self._refresh(http.request)
File "/usr/local/lib/python2.7/site-packages/oauth2client/client.py", line 769, in _refresh
self._do_refresh_request(http_request)
File "/usr/local/lib/python2.7/site-packages/oauth2client/client.py", line 795, in _do_refresh_request
body = self._generate_refresh_request_body()
File "/usr/local/lib/python2.7/site-packages/oauth2client/client.py", line 1425, in _generate_refresh_request_body
assertion = self._generate_assertion()
File "/usr/local/lib/python2.7/site-packages/oauth2client/client.py", line 1554, in _generate_assertion
private_key, self.private_key_password), payload)
File "/usr/local/lib/python2.7/site-packages/oauth2client/crypt.py", line 162, in from_string
from OpenSSL import crypto
File "/usr/local/lib/python2.7/site-packages/OpenSSL/__init__.py", line 8, in <module>
from OpenSSL import rand, crypto, SSL
File "/usr/local/lib/python2.7/site-packages/OpenSSL/SSL.py", line 118, in <module>
SSL_ST_INIT = _lib.SSL_ST_INIT
AttributeError: 'module' object has no attribute 'SSL_ST_INIT'
115811 次浏览

Turned out the problem was with my installation of pyOpenSSL, pyOpenSSL-0.15.1 .

I did:

pip uninstall pyopenssl

and then

pip install pyopenssl

...and my Python script worked again!

I experienced the same issue recently and after few hours investigation, I found out that it was caused by New cryptography 2.0 upgrade. This upgrade will break many packages using pyopenssl (like Sentry, Google Analytics and etc). Just downgrade it to 1.9 will solve the problem.

Be cautious if you are using "pip install -U", it will automatically upgrade packages that are not listed in requirements.txt.

Update your pyopenssl module:

$ sudo pip install -U pyopenssl

Upgrading pyopenssl with pip was not working as none of the commands related to to pip was working for me. By upgrading pyopenssl with easy_install, above problem can be solved.

sudo python -m easy_install --upgrade pyOpenSSL

credit @delimiter (Answer)

I had the same problem on Ubuntu 16.04, but with the following twist: when virtualenv was activated (. venv/bin/activate before running celery workers with pysolr, requests, etc in my case) - everything worked perfectly, but when I ran celery from command line using full paths, and python paths - there was a problem (and same problem running from supervisord ). Also, if important, virtualenv has been bundled elsewhere on the machine with same Ubuntu version.

Solution was simple: adding /full/path/to/venv/bin to PATH ( as advised here https://serverfault.com/questions/331027/supervisord-how-to-append-to-path ) solved this.

Unfortunately, I have not yet pin-pointed what kind of update caused this, but hopefully this may help someone.

I saw the AttributeError: 'module' object has no attribute 'SSL_ST_INIT' error too.

Doing

sudo pip install pyOpenSSL==16.2.0

resolved it for me.

In my case, the problem was that the package was installed in root directories, and I was executing the script which asked for pyopenssl with my Linux user forvas. And that user can't use the libraries installed in root.

So first I had to remove the package with aptitude or apt-get.

sudo aptitude purge python-openssl

Therefore, I had to install the package again, but taking into account the user who is executing the script which is asking for the library. Take a look to where the library is installed depending on the Linux user and the argument --user of pip.

Case 1

forvas@server:$ pip install pyopenssl

Could not install packages due to an EnvironmentError:

[Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/OpenSSL'

Consider using the --user option or check the permissions.

Case 2

forvas@server:$ sudo pip install pyopenssl

/usr/local/lib/python2.7/dist-packages/OpenSSL/*

/usr/local/lib/python2.7/dist-packages/pyOpenSSL-17.5.0.dist-info/*

Case 3

forvas@server:$ sudo pip install --user pyopenssl

/home/forvas/.local/lib/python2.7/site-packages/OpenSSL/*

/home/forvas/.local/lib/python2.7/site-packages/pyOpenSSL-17.5.0.dist-info/*

Case 4

root@server:$ pip install pyopenssl

/usr/local/lib/python2.7/dist-packages/OpenSSL/*

/usr/local/lib/python2.7/dist-packages/pyOpenSSL-17.5.0.dist-info/*

Case 5

root@server:$ pip install --user pyopenssl

/root/.local/lib/python2.7/site-packages/OpenSSL/*

/root/.local/lib/python2.7/site-packages/pyOpenSSL-17.5.0.dist-info/*

Conclusion

My problem was that the library was installed in the directories of the case 5.

Solution

  • Uninstalling the package.

  • As I'm executing the script with Linux user forvas, I was able to reinstall the package rightly with the options 2 or 4 (in which the library is available for all Linux users) or more accurate, the option 3 (in which library is only available for Linux user forvas).

I was seeing similar python stack dump on the console of my Ubuntu 16.04 VM when I tried ssh into the VM.

SSL_ST_INIT = _lib.SSL_ST_INIT
AttributeError: 'module' object has no attribute 'SSL_ST_INIT'

pip reported that pyopenssl was not installed.

I had to do this instead:

sudo apt install --reinstall python-openssl

In my case, It was throwing the same error for uninstalling and upgrading. I couldn't uninstall or upgrade.

AttributeError: 'module' object has no attribute 'SSL_ST_INIT'

Following worked for me.

# rm -rf /usr/lib/python2.7/site-packages/OpenSSL/
# rm -rf /usr/lib/python2.7/site-packages/pyOpenSSL-16.1.0.dist-info
# rm -rf /usr/lib/python2.7/site-packages/pyOpenSSL-18.0.0-py2.7.egg
# pip2.7 install pyopenssl
Collecting pyopenssl
Downloading
.
.
100% |████████████████████████████████| 61kB 5.8MB/s
Collecting cryptography>=2.2.1 (from pyopenssl)
.
.
Installing collected packages: cryptography, pyopenssl
Found existing installation: cryptography 1.7.2
Uninstalling cryptography-1.7.2:
Successfully uninstalled cryptography-1.7.2
Successfully installed cryptography-2.2.2 pyopenssl-18.0.0

WARNING: Try this only if upgrading(sudo pip install pyOpenSSL==16.2.0) or uninstalling(pip uninstall pyopenssl) doesn't help

Try with the following commands:

easy_install -U pip
easy_install -U pyOpenSSL

My problem was caused by the version of Python openssl that was in /usr/lib/python2.7/dist-packages/.

dpkg -l | grep openssl showed:

ii  python-openssl                                0.15.1-2build1                               all          Python 2 wrapper around the OpenSSL library

I removed it using sudo apt-get remove python-openssl. I then ran the following to install the distribution version of pip.

curl -o ./get-pip.py https://bootstrap.pypa.io/get-pip.py
sudo python2 ./get-pip.py

pip --version now displays:

pip 18.0 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)

I was then able to perform the necessary pip install I was trying to complete.

I had a similar error:

    from OpenSSL import rand, crypto, SSL
File "/usr/local/lib/python3.5/dist-packages/OpenSSL/SSL.py", line 112, in <module>
SSL_ST_INIT = _lib.SSL_ST_INIT
AttributeError: module 'lib' has no attribute 'SSL_ST_INIT'

and none of the other answers could fix it, because pip could not install anything. Instead, what I did was this from the terminal first:

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

Then reinstalled pyopenssl with pip:

sudo pip install pyopenssl

and everything was gravy.

Just in case anyone else isn't finding exactly the right incantations to make this work, as of Nov 2018 the thing that worked for me was:

sudo rm -rf /usr/local/lib/python2.7/dist-packages/OpenSSL/ sudo apt install --reinstall python-openssl

Good luck!

I just encountered this on my Ubuntu 16.04 host. There appears to be a version conflict between the apt repo packages for python-openssl and python-crypotgraphy, vs what someone installed manually with pip into /usr/local/python2.7/dist-packages.

Once it got into this state, the system standard pip couldn't execute, either. I got around the chicken-and-egg problem by manually setting a PYTHONPATH environment variable that excluded the /usr/local part of the tree thusly:

    $ export PYTHONPATH="/usr/lib/python2.7:/usr/lib/python2.7/plat-x86_64-linux-gnu:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/gtk-2.0"
$ /usr/bin/pip uninstall cryptography
$ unset PYTHONPATH

I acquired the above list of library directories to use with the python shell:

    import sys
for p in sys.path:
print(p)

and then copying everything listed except the one /usr/local directory. Your system may have a different list in its path. Adjust accordingly.

I also had some manual apt-get install --reinstall python-openssl python-cryptography commands scattered in my bash history, which may or may not have been necessary.

Try with:

export PYTHONPATH="/usr/lib/python2.7:/usr/lib/python2.7/plat-x86_64-linux-gnu:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/gtk-2.0"
sudo apt-get install --reinstall python-openssl

My solution was a lot more simplistic after these other solutions not working for me. Anything I tried to install/uninstall via pip returned the same error and stacktrace.

I ended up trying to update pip via pip3 and it worked flawlessly:

pip3 install --upgrade pip

I went back to using pip and everything worked correctly. I did notice that it was referencing Python 3.6 when running the pip commands though.

# pip install pyopenssl`enter code here`
Requirement already satisfied: pyopenssl in /usr/lib64/python3.6/site-packages (18.0.0)


<snipped>


Requirement already satisfied: pycparser in /usr/lib64/python3.6/site-packages (from cffi!=1.11.3,>=1.7->cryptography>=2.2.1->pyopenssl) (2.19)

I had the same issue and as pip wasn't working anymore I had to do his job manually:

wget https://files.pythonhosted.org/packages/40/d0/8efd61531f338a89b4efa48fcf1972d870d2b67a7aea9dcf70783c8464dc/pyOpenSSL-19.0.0.tar.gz
tar -xzvf pyOpenSSL-19.0.0.tar.gz
cd pyOpenSSL-19.0.0
sudo python setup.py install

After that everything worked as expected.

I had this problem on MacOS with python 2 and 3 installed via brew. It turns out that brew uninstalling python and python@2 does not remove any libraries which were installed for those versions of python; i.e. in:

/usr/local/lib/python3.7/site-packages/ and
/usr/local/lib/python2.7/site-packages/

Something in there was not right, so what worked for me was to delete/move all the installed libraries for brew's python 2 and 3 and start again (and make sure only to use virtualenvs from here on):

brew uninstall --ignore-dependencies python@2
brew uninstall --ignore-dependencies python
sudo mv /usr/local/lib/python3.7 ~/python3.7libs-backup
sudo mv /usr/local/lib/python2.7 ~/python2.7libs-backup
brew install python
brew install python@2

This worked for me:

sudo apt remove python-openssl

This is an older post, so I'm answering to hopefully help someone in late 2019... my problem had to do with Python 2 vs 3. I'm using Python 3 exclusively; no more legacy Python. But my OS (eg, Ubuntu 16.04) still has Python 2.7 installed. Thus when running global pip by default (eg, not in a virtualenv), I was calling the Python 2 version of pip.

For me, I used the following to fix this issue.

Uninstall docker-compose:

sudo pip3 uninstall docker-compose
sudo pip uninstall docker-compose

Reinstall docker-compose for my version of Python:

sudo pip3 install docker-compose

I’m running running OSX 10.15.5 Catalina and using pyenv.

Having tried a number of other solutions on this page what worked for me is:

pyenv uninstall 3.6.8
pyenv install 3.6.8

I have to admit I'm not sure why this resolved this issue, because I had only installed this version of Python fairly recently.

I get AttributeError: module 'lib' has no attribute 'ERR_load_RAND_strings' on a freshly installed Raspberry Pi OS (formerly Raspbian) Buster Lite 32bit after pip installing a few packages with pip. A combination of the other answers works:

sudo rm -r /usr/local/lib/python3.9/dist-packages/OpenSSL
sudo pip uninstall --yes pyopenssl
sudo pip install pyopenssl

The first command will not actually remove the package installation, just its files, but uninstall without it will raise the same error. Obviously we are dealing with a Pi so the last will be:

sudo pip install pyopenssl --no-cache-dir

(I thought I'd reiterate the others as this Q does not come up in the search for the issue (it does via an ESP32 forum via Google) so I am repeating it with more keywords for the Pi)