Python AttributeError:';模块';对象没有属性';SSL_ST_INIT';

Python AttributeError:';模块';对象没有属性';SSL_ST_INIT';,python,openssl,pyopenssl,Python,Openssl,Pyopenssl,我的一个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_

我的一个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'
回溯(最近一次呼叫最后一次):
文件“/inspect_sheet.py”,第21行,在
main()
文件“/inspect_sheet.py”,第12行,主
工作簿名称=工作簿名称,
文件“/google\u sheets.py”,第56行,在__
self.login()
文件“/google_sheets.py”,第46行,登录
self.client=gspread.authorize(凭据)
文件“/usr/local/lib/python2.7/site packages/gspread/client.py”,第335行
client.login()
文件“/usr/local/lib/python2.7/site packages/gspread/client.py”,第98行,登录
self.auth.refresh(http)
文件“/usr/local/lib/python2.7/site packages/oauth2client/client.py”,第598行,刷新
自我刷新(http.request)
文件“/usr/local/lib/python2.7/site packages/oauth2client/client.py”,第769行,在刷新
self.\u do\u刷新请求(http\u请求)
文件“/usr/local/lib/python2.7/site packages/oauth2client/client.py”,第795行,在刷新请求中
body=self.\u生成\u刷新\u请求\u body()
文件“/usr/local/lib/python2.7/site packages/oauth2client/client.py”,第1425行,在“生成\刷新\请求”正文中
断言=self.\u生成\u断言()
文件“/usr/local/lib/python2.7/site packages/oauth2client/client.py”,第1554行,在生成断言中
私钥,self.private(私钥,密码),有效载荷)
文件“/usr/local/lib/python2.7/site packages/oauth2client/crypt.py”,第162行,from_字符串
从OpenSSL导入加密
文件“/usr/local/lib/python2.7/site packages/OpenSSL/_init__.py”,第8行,在
从OpenSSL导入rand、加密、SSL
文件“/usr/local/lib/python2.7/site packages/OpenSSL/SSL.py”,第118行,在
SSL_ST_INIT=_lib.SSL_ST_INIT
AttributeError:“模块”对象没有属性“SSL\u ST\u INIT”

原来问题出在我安装的pyOpenSSL上,pyOpenSSL-0.15.1

我做到了:

pip uninstall pyopenssl
然后

pip install pyopenssl

…我的Python脚本又开始工作了

我最近遇到了同样的问题,经过几个小时的调查,我发现这是由新的加密2.0升级引起的。此升级将破坏许多使用pyopenssl的软件包(如Sentry、Google Analytics等)。只要把它降到1.9就可以解决问题


如果您使用的是“pip安装-U”,它将自动升级requirements.txt中未列出的软件包。

更新您的
pyopenssl
模块:

$ sudo pip install -U pyopenssl

使用pip升级
pyopenssl
不起作用,因为与
pip
相关的命令都不适用于我。通过将
pyopenssl
升级为
easy\u install
,可以解决上述问题

sudo python -m easy_install --upgrade pyOpenSSL

credit@delimiter()

我在Ubuntu 16.04上也遇到了同样的问题,但有以下问题:当virtualenv被激活时(
.venv/bin/activate
在我使用pysolr、请求等运行芹菜工人之前)-一切都很好,但当我使用完整路径从命令行运行芹菜时,还有python路径——出现了一个问题(supervisord也出现了同样的问题)。另外,如果重要的话,virtualenv已经在机器上的其他地方捆绑了相同的Ubuntu版本

解决方案很简单:将
/full/path/to/venv/bin
添加到path(此处建议)解决了这个问题


不幸的是,我还没有指出是什么样的更新导致了这种情况,但希望这可能会对某些人有所帮助。

我看到
AttributeError:“module”对象也没有属性“SSL\u ST\u INIT”
错误

sudopip安装pyOpenSSL==16.2.0


为我解决了这个问题。

在我的例子中,问题是软件包安装在根目录中,我正在与我的Linux用户forvas执行请求使用
pyopenssl
的脚本。并且该用户不能使用安装在root中的库

因此,首先我必须删除带有
aptitude
apt-get
的包

sudo aptitude purge python-openssl
因此,我不得不再次安装包,但要考虑到执行脚本的用户请求库。根据Linux用户和
pip
的参数
--user
,查看库的安装位置

案例1

forvas@server:$ pip install pyopenssl
由于环境错误,无法安装程序包:

[Errno 13]权限被拒绝:'/usr/local/lib/python2.7/dist-packages/OpenSSL'

考虑使用
--user
选项或检查权限

案例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/*

案例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/*

案例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/*

案例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/*

结论

我的问题是库安装在案例5的目录中

解决方案

  • 卸载软件包

  • 当我使用Linux用户forvas执行脚本时,我能够使用选项24(其中库可供所有Linux用户使用)或更准确的选项3(其中库仅可供Linux用户forvas使用)正确地重新安装包

我在Ubuntu 16.04虚拟机的控制台上看到类似的python堆栈转储,当时我尝试将ssh插入虚拟机

SSL_ST_INIT = _lib.SSL_ST_INIT
AttributeError: 'module' object has no attribute 'SSL_ST_INIT'
pip
报告了
pyopensslsudo apt install --reinstall python-openssl
# 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
easy_install -U pip
easy_install -U pyOpenSSL
ii  python-openssl                                0.15.1-2build1                               all          Python 2 wrapper around the OpenSSL library
curl -o ./get-pip.py https://bootstrap.pypa.io/get-pip.py
sudo python2 ./get-pip.py
pip 18.0 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
    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'
sudo rm -r /usr/local/lib/python3.5/dist-packages/OpenSSL
sudo pip install pyopenssl
    $ 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
    import sys
    for p in sys.path:
       print(p)
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
# 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)
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
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
sudo apt remove python-openssl
sudo pip3 uninstall docker-compose  
sudo pip uninstall docker-compose
sudo pip3 install docker-compose
pyenv uninstall 3.6.8
pyenv install 3.6.8