致命错误:openssl/opensslv.h:没有该文件或目录 编译 mitmproxy

我正在尝试通过pip安装mitmproxy包,就像这样:
$ sudo pip install mitmproxy

它以以下错误消息终止:
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c build/temp.linux-x86_64-2.7/_openssl.c -o build/temp.linux-x86_64-2.7/build/temp.linux-x86_64-2.7/_openssl.o

build/temp.linux-x86_64-2.7/_openssl.c:391:30: fatal error: openssl/opensslv.h: No such file or directory

compilation terminated.

error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

----------------------------------------
  Can't roll back cryptography; was not uninstalled
Cleaning up...
Command /usr/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip-build-jvLTVf/cryptography/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-DrY4DI-record/install-record.txt --single-version-externally-managed --compile failed with error code 1 in /tmp/pip-build-jvLTVf/cryptography
Storing debug log for failure in /home/niklas/.pip/pip.log

安装完之后,至少我可以随时卸载它。
 $ mitmproxy

导致

Traceback (most recent call last):
  File "/usr/local/bin/mitmproxy", line 7, in <module>
    from mitmproxy.main import mitmproxy
  File "/usr/local/lib/python2.7/dist-packages/mitmproxy/main.py", line 7, in <module>
    from . import version, cmdline
  File "/usr/local/lib/python2.7/dist-packages/mitmproxy/cmdline.py", line 6, in <module>
    import configargparse
ImportError: No module named configargparse

我建议您升级到16.04版本。 - anonymous2
3个回答

其他答案只涉及解决你提到的错误所需的依赖项。实际上,所需的所有依赖项列表要长得多。

你可以使用以下方式安装它们全部:

sudo apt-get install python-pip python-dev libffi-dev libssl-dev libxml2-dev libxslt1-dev libjpeg8-dev zlib1g-dev

然后你可以安装 mitmproxy
sudo pip install mitmproxy

运行它:
mitmproxy

来源:文档

1工作了,但不知怎么把pip搞坏了。不得不重新安装它。现在一切都正常了。 :) - Niklas
1@Niklas 很高兴听到问题得到解决。祝你有个愉快的一天! - grooveplex

»» fatal error: openssl/opensslv.h: No such file or directory ««

安装 openssl :sudo apt-get update && sudo apt-get install libssl-dev


在Ubuntu 16.04(在Python虚拟环境中)安装pyOpenSSL的过程中,我遇到了相同的错误。这个答案解决了我的问题。谢谢! - yucer
修复在Ubuntu服务器(stretch/sid或Ubuntu 16.04.1 LTS)上安装"pip install fabric"时出现的问题。 - Sergei G
1这是一个简单明了的答案,适用于任何依赖于https://pypi.python.org/pypi/cryptography/1.8.1和https://pypi.python.org/pypi/paramiko/2.1.2的情况。 - Bruno Bronosky
1安装hashpumpy时保存了错误。 - NonStandardModel
我在尝试在alpine上安装keyring时遇到了类似的问题,但是在尝试安装libssl-dev时出现了ERROR: unsatisfiable constraints。通过安装openssl-dev解决了这个问题。 - cowlinator
我有两个后续问题:a) 如果没有StackOverflow,怎么能确定libssl-dev是正确的安装选项?b) 为什么mitmproxy的依赖中没有安装libssl-dev(我猜是因为它不能通过pip安装?为什么不行?)? - scubbo
@scubbo:pip 只安装 Python 代码。openssl → libopenssl 是 C 代码。请参考 https://packages.ubuntu.com/focal-updates/libssl-dev 和 https://packages.ubuntu.com/source/focal-updates/openssl。 - Knud Larsen
那么为什么安装Python库时会默默地依赖于一个不是Python库的东西,并提供一个需要进一步上下文才能解决的错误信息呢?我相信这里有非常好的历史原因,但这是“应该继续保持的事实”,还是“目前最方便和足够好的情况,但在完美世界中不会发生的情况”?(编辑:明确一点 - 我并不是激烈地说“这是错的”,我是在说“对我来说,这感觉不对,但我知道足够了解自己什么都不懂 - 我错过了什么?”) - scubbo
scubbo:你必须观察pip安装顺序,才能知道pip是从源代码构建的 = https://pypi.org/project/pip/ → → https://pypi.org/project/pip/#files - Knud Larsen

实际上,在您的输出中有两个不同的问题(假设所有其他依赖问题已经解决)。这两个问题都需要修复。

缺少SSL库

  1. 正如Knud Larsen在他们对这个问题的回答中所述,您缺少了OpenSSL库。请参考他们在这个问题上的回答。

缺少Python模块

有一个Python脚本是你正在运行的一部分,但它缺少一个需要运行的模块(称为configargparse)。
如果你使用的是Ubuntu 15.10或更新版本,你可以通过执行sudo apt-get install python-configargparse来安装它。
如果你使用的是Ubuntu 15.10之前的任何版本,你需要通过pip来安装它,以便下载并使其可用于系统:sudo pip install configargparse。
(请注意,一旦解决了缺少SSL库的问题,python pip install mitmproxy也能实现相同类型的依赖解析,但如果由于某种原因无法实现,你将需要手动安装该模块)
注意:你的问题中提到你正在使用Ubuntu MATE 15.10。所有变体的Ubuntu 15.10已于2016年7月28日终止支持。你应考虑升级到16.04版本,以便继续获得Ubuntu的支持和更新。