对无法确认“pyOpenSSL”模块感到沮丧

问题描述

环境:OS X BigSur 11.2.2; MacBook Pro 英特尔

我试图让 pyOpenSSL 在 python3 环境中工作。我一直致力于从使用 Py2 转换为 Py3。我可以很容易地在 Python2 中完成这项工作;但出于显而易见的原因,我真的需要离开 Python2。

在我的 python 脚本中,我只有以下命令:“导入 pyOpenSSL”。我也尝试过“导入 OpenSSL”和“导入密码术”,它们都会产生类似的“没有命名模块...”错误。无论语法、拼写或大小写,都无济于事。

经过以下所有尝试,安装成功;我用“pip3 list”进行了验证。

我试过了:

  • 直接安装到系统 Python - 不行。
  • 安装了 pyenv 并安装了 Python 3.8.7 并使用 pip3 安装了 pyOpenSSL - 不行。
  • 安装了 virtualenvwrapper 并创建了一个虚拟环境,安装在那里,还是不行。

我什至在 python 中验证,该模块是使用以下内容安装的:

import pkg_resources
installed_packages = pkg_resources.working_set
installed_packages_list = sorted(["%s==%s" % (i.key,i.version)
   for i in installed_packages])
print(installed_packages_list)
['appdirs==1.4.4','cffi==1.14.5','cryptography==3.4.6','distlib==0.3.1','filelock==3.0.12','pbr==5.5.1','pip==21.0.1','pycparser==2.20','pyopenssl==19.1.0','setuptools==49.2.1','six==1.15.0','stevedore==3.3.0','virtualenv-clone==0.5.4','virtualenv==20.4.2','virtualenvwrapper==4.8.4']

即使使用非 pip,也显示至少安装了“openSSL”。

>>> help("modules")

Please wait a moment while I gather a list of all available modules...

/Users/jewettg/.pyenv/versions/3.8.7/lib/python3.8/site-packages/setuptools/distutils_patch.py:25: UserWarning: distutils was imported before Setuptools. This usage is discouraged and may exhibit undesirable behaviors or errors. Please use Setuptools' objects directly or at least import Setuptools first.
  warnings.warn(
OpenSSL             _thread             functools           resource
__future__          _threading_local    gc                  rlcompleter
_abc                _tkinter            genericpath         runpy
_ast                _tracemalloc        getopt              sched
_asyncio            _uuid               getpass             secrets
_bisect             _warnings           gettext             select
_blake2             _weakref            glob                selectors
_bootlocale         _weakrefset         grp                 setuptools
_bz2                _xxsubinterpreters  gzip                shelve
_cffi_backend       _xxtestfuzz         hashlib             shlex
_codecs             abc                 heapq               shutil

我不知道我做错了什么!帮助!?

解决方法

我不能告诉您 pyenv 或其他管理器的情况,但 conda 很少让我失望。我已经验证我可以从一个干净的 conda env w/python3.8 安装这个库并导入它:

(base) $ conda create python=3.8 -n pyssl
...
(base) $ conda activate pyssl
(pyssl) $ pip install pyopenssl
...
Successfully installed cffi-1.14.5 cryptography-3.4.6 pycparser-2.20 pyopenssl-20.0.1 six-1.15.0
(pyssl) $ python
Python 3.8.8 (default,Feb 24 2021,21:46:12)
[GCC 7.3.0] :: Anaconda,Inc. on linux
Type "help","copyright","credits" or "license" for more information.
>>> import OpenSSL
>>>

所以你几乎肯定会想要做 import OpenSSL。我在 Windows 上,但使用 WSL2。不过,这些都不重要。

通常发生这些问题时,是因为您已将库安装到一个 Python 解释器,但在实际运行时意外调用了不同 解释器(您没有安装该库)。