Python AttributeError: 'module' object has

2019-01-16 22:56发布

A Python script of mine is failing with:

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'

16条回答
一夜七次
2楼-- · 2019-01-16 23:23

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.

查看更多
不美不萌又怎样
3楼-- · 2019-01-16 23:24

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

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-16 23:26

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!

查看更多
闹够了就滚
5楼-- · 2019-01-16 23:29

Update your pyopenssl module:

$ sudo pip install -U pyopenssl
查看更多
老娘就宠你
6楼-- · 2019-01-16 23:31

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)

查看更多
Anthone
7楼-- · 2019-01-16 23:32

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!

查看更多
登录 后发表回答