Python AttributeError:“模块”对象没有属性“SSL_ST_INIT”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43267157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:44:25  来源:igfitidea点击:

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

pythonopensslpyopenssl

提问by Ben Wheeler

A Python script of mine is failing with:

我的一个 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'

回答by Muhammad Hassan

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

pyopenssl使用 pip升级不起作用,因为与 相关的所有命令都不pip适用于我。通过升级pyopenssl使用easy_install,上述问题就可以迎刃而解。

sudo python -m easy_install --upgrade pyOpenSSL

credit @delimiter (Answer)

信用@delimiter(答案

回答by Ben Wheeler

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

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

I did:

我做了:

pip uninstall pyopenssl

and then

进而

pip install pyopenssl

...and my Python script worked again!

...我的 Python 脚本又开始工作了!

回答by user197292

Update your pyopensslmodule:

更新您的pyopenssl模块:

$ sudo pip install -U pyopenssl

回答by wordsforthewise

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:

并且没有其他答案可以解决它,因为 pip 无法安装任何东西。相反,我首先从终端做的是:

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

Then reinstalled pyopenssl with pip:

然后用pip重新安装pyopenssl:

sudo pip install pyopenssl

and everything was gravy.

一切都是肉汁。

回答by felix021

Try with the following commands:

尝试使用以下命令:

easy_install -U pip
easy_install -U pyOpenSSL

回答by Fei Xie

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

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

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

如果您使用“pip install -U”,请小心,它会自动升级requirements.txt 中未列出的软件包。

回答by forvas

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

就我而言,问题是该软件包安装在目录中,并且我正在执行pyopenssl我的 Linux 用户forvas要求的脚本。并且该用户不能使用安装在 root 中的库。

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

所以首先我必须用aptitudeor删除包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 --userof pip.

因此,我不得不再次安装该软件包,但要考虑到正在执行要求库的脚本的用户。看看到库随Linux用户和参数安装位置--userpip

Case 1

情况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 --useroption or check the permissions.

由于环境错误,无法安装软件包:

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

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

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

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

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

Case 3

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

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

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

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

Case 5

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

/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.

我的问题是该库安装在 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 2or 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).

  • 卸载软件包。

  • 当我使用 Linux 用户forvas执行脚本时,我能够使用选项24(其中该库可供所有 Linux 用户使用)或更准确的选项3(其中库是仅适用于 Linux 用户forvas)。

回答by Delaballe

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

我遇到了同样的问题,由于 pip 不再工作,我不得不手动完成他的工作:

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.

之后,一切都按预期进行。

回答by HeatfanJohn

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

我的问题是由/usr/lib/python2.7/dist-packages/.

dpkg -l | grep opensslshowed:

dpkg -l | grep openssl显示:

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.

我使用sudo apt-get remove python-openssl. 然后我运行以下命令来安装 pip 的分发版本。

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

pip --versionnow displays:

pip --version现在显示:

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.

然后我就能够执行我试图完成的必要的 pip 安装。

回答by luilver

This worked for me:

这对我有用:

sudo apt remove python-openssl