安装M2Crypto遇ModuleNotFoundError: No module named 'swig'错误求助
安装M2Crypto时出现
ModuleNotFoundError: No module named 'swig'的问题 我已按照M2Crypto安装先决条件文档安装了SWIG,但执行pip3 install M2Crypto时仍抛出错误:ModuleNotFoundError: No module named 'swig'。
环境版本信息
- Python版本:3.8
- OpenSSL版本:OpenSSL 1.1.1k
- SWIG版本:4.2.1
- Pip版本:24.0
执行pip3 install --force-reinstall M2Crypto的报错日志
pip3 install --force-reinstall M2Crypto Defaulting to user installation because normal site-packages is not writeable Looking in indexes: **** Collecting M2Crypto Using cached **** Installing build dependencies: started Installing build dependencies: finished with status 'done' Getting requirements to build wheel: started Getting requirements to build wheel: finished with status 'done' Installing backend dependencies: started Installing backend dependencies: finished with status 'done' Preparing metadata (pyproject.toml): started Preparing metadata (pyproject.toml): finished with status 'done' Building wheels for collected packages: M2Crypto Building wheel for M2Crypto (pyproject.toml): started Building wheel for M2Crypto (pyproject.toml): finished with status 'error' error: subprocess-exited-with-error × Building wheel for M2Crypto (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [59 lines of output] INFO:run_command:running bdist_wheel INFO:run_command:running build INFO:run_command:running build_py INFO:copy_file:copying src/M2Crypto/util.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/threading.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/m2xmlrpclib.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/m2urllib2.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/m2urllib.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/m2.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/httpslib.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/ftpslib.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/callback.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/__init__.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/X509.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/SMIME.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/Rand.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/RSA.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/RC4.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/Err.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/Engine.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/EVP.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/EC.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/DSA.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/DH.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/BN.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/BIO.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/AuthCookie.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:copy_file:copying src/M2Crypto/ASN1.py -> build/lib.linux-x86_64-cpython-38/M2Crypto INFO:mkpath:creating build/lib.linux-x86_64-cpython-38/M2Crypto/SSL INFO:copy_file:copying src/M2Crypto/SSL/timeout.py -> build/lib.linux-x86_64-cpython-38/M2Crypto/SSL INFO:copy_file:copying src/M2Crypto/SSL/cb.py -> build/lib.linux-x86_64-cpython-38/M2Crypto/SSL INFO:copy_file:copying src/M2Crypto/SSL/__init__.py -> build/lib.linux-x86_64-cpython-38/M2Crypto/SSL INFO:copy_file:copying src/M2Crypto/SSL/TwistedProtocolWrapper.py -> build/lib.linux-x86_64-cpython-38/M2Crypto/SSL INFO:copy_file:copying src/M2Crypto/SSL/Session.py -> build/lib.linux-x86_64-cpython-38/M2Crypto/SSL INFO:copy_file:copying src/M2Crypto/SSL/SSLServer.py -> build/lib.linux-x86_64-cpython-38/M2Crypto/SSL INFO:copy_file:copying src/M2Crypto/SSL/Context.py -> build/lib.linux-x86_64-cpython-38/M2Crypto/SSL INFO:copy_file:copying src/M2Crypto/SSL/Connection.py -> build/lib.linux-x86_64-cpython-38/M2Crypto/SSL INFO:copy_file:copying src/M2Crypto/SSL/Cipher.py -> build/lib.linux-x86_64-cpython-38/M2Crypto/SSL INFO:copy_file:copying src/M2Crypto/SSL/Checker.py -> build/lib.linux-x86_64-cpython-38/M2Crypto/SSL INFO:run_command:running egg_info INFO:write_pkg_info:writing src/M2Crypto.egg-info/PKG-INFO INFO:write_file:writing dependency_links to src/M2Crypto.egg-info/dependency_links.txt INFO:write_file:writing requirements to src/M2Crypto.egg-info/requires.txt INFO:write_file:writing top-level names to src/M2Crypto.egg-info/top_level.txt INFO:read_manifest:reading manifest file 'src/M2Crypto.egg-info/SOURCES.txt' INFO:read_template:reading manifest template 'MANIFEST.in' WARNING:process_template_line:warning: no files found matching 'src/SWIG/_m2crypto_wrap.c' WARNING:process_template_line:warning: no files found matching 'src/M2Crypto/m2crypto.py' INFO:add_license_files:adding license file 'LICENCE' INFO:execute:writing manifest file 'src/M2Crypto.egg-info/SOURCES.txt' INFO:run_command:running build_ext INFO:build_extension:building 'M2Crypto._m2crypto' extension INFO:swig_sources:swigging src/SWIG/_m2crypto.i to src/SWIG/_m2crypto_wrap.c INFO:spawn:swig -python -I/usr/lib/gcc/x86_64-redhat-linux/8/include -I/usr/local/include -I/usr/include -D__x86_64__ -I/usr/local/include/python3.8 -I/usr/include/openssl -includeall -builtin -outdir build/lib.linux-x86_64-cpython-38/M2Crypto -o src/SWIG/_m2crypto_wrap.c src/SWIG/_m2crypto.i Traceback (most recent call last): File "/home/****/.local/bin/swig", line 5, in <module> from swig import swig ModuleNotFoundError: No module named 'swig' error: command '/home/****/.local/bin/swig' failed with exit code 1 [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for M2Crypto Failed to build M2Crypto ERROR: Could not build wheels for M2Crypto, which is required to install pyproject.toml-based projects
解决方案
从报错日志可以看到,调用的是/home/****/.local/bin/swig,这是一个Python脚本而非系统级的SWIG工具,问题出在这里:
卸载冲突的Python swig包
执行以下命令卸载误装的Python版swig:pip uninstall -y swig确认系统级SWIG工具的存在
执行which swig,如果输出是系统路径(如/usr/bin/swig)则正常;如果没有输出,说明系统未安装SWIG,用对应包管理器安装:- Debian/Ubuntu系:
sudo apt-get install swig - RHEL/CentOS系:
sudo yum install swig
- Debian/Ubuntu系:
重新安装M2Crypto
执行:pip3 install M2Crypto
内容的提问来源于stack exchange,提问作者Thomas
相关产品推荐
相关产品推荐

