为 Python 3.7 virtualenv 安装 mod_wsgi

Posted

技术标签:

【中文标题】为 Python 3.7 virtualenv 安装 mod_wsgi【英文标题】:Installing mod_wsgi for Python 3.7 virtualenv 【发布时间】:2019-01-19 02:02:55 【问题描述】:

尝试为 Python 3.7 安装 mod_wsgi 以完成 Django 的设置。但它失败并显示以下错误消息

Collecting mod-wsgi
  Using cached https://files.pythonhosted.org/packages/9e/37/dd336068ece37c43957aa337f25c59a9a6afa98086e5507908a2d21ab807/mod_wsgi-4.6.4.tar.gz
Building wheels for collected packages: mod-wsgi
  Running setup.py bdist_wheel for mod-wsgi ... error
  Complete output from command /home/user/myproject_env/bin/python3.7 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-9hnl4thc/mod-wsgi/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/pip-wheel-a9t55ene --python-tag cp37:

  WARNING: The Python installation you are using does not appear to have
  been installed with a shared library, or in the case of MacOS X, as a
  framework. Where these are not present, the compilation of mod_wsgi may
  fail, or if it does succeed, will result in extra memory being used by
  all processes at run time as a result of the static library needing to
  be loaded in its entirety to every process. It is highly recommended
  that you reinstall the Python installation being used from source code,
  supplying the '--enable-shared' option to the 'configure' script when
  configuring the source code prior to building and installing it.

...

    running build_ext
    building 'mod_wsgi.server.mod_wsgi-py37' extension
    creating build/temp.linux-x86_64-3.7
    creating build/temp.linux-x86_64-3.7/src
    creating build/temp.linux-x86_64-3.7/src/server

...

    gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/usr/include/httpd -I/home/user/python/include/python3.7m -c src/server/wsgi_stream.c -o build/temp.linux-x86_64-3.7/src/server/wsgi_stream.o -I/usr/include/httpd -I. -I/usr/include/apr-1 -DLINUX -D_REENTRANT -D_GNU_SOURCE -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -pthread -I/usr/include/apr-1 -I/usr/include/apr-1
    gcc -pthread -shared build/temp.linux-x86_64-3.7/src/server/wsgi_thread.o build/temp.linux-x86_64-3.7/src/server/mod_wsgi.o build/temp.linux-x86_64-3.7/src/server/wsgi_buckets.o build/temp.linux-x86_64-3.7/src/server/wsgi_memory.o build/temp.linux-x86_64-3.7/src/server/wsgi_interp.o build/temp.linux-x86_64-3.7/src/server/wsgi_restrict.o build/temp.linux-x86_64-3.7/src/server/wsgi_logger.o build/temp.linux-x86_64-3.7/src/server/wsgi_server.o build/temp.linux-x86_64-3.7/src/server/wsgi_convert.o build/temp.linux-x86_64-3.7/src/server/wsgi_metrics.o build/temp.linux-x86_64-3.7/src/server/wsgi_validate.o build/temp.linux-x86_64-3.7/src/server/wsgi_daemon.o build/temp.linux-x86_64-3.7/src/server/wsgi_apache.o build/temp.linux-x86_64-3.7/src/server/wsgi_stream.o -o build/lib.linux-x86_64-3.7/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so -L/home/user/python/lib -L/home/user/python/lib/python3.7/config-3.7m-linux -lpython3.7m
    /usr/bin/ld: /home/user/python/lib/libpython3.7m.a(abstract.o): relocation R_X86_64_32S against symbol `_Py_NotImplementedStruct' can not be used when making a shared object; recompile with -fPIC

...

    /usr/bin/ld: /home/user/python/lib/libpython3.7m.a(getopt.o): relocation R_X86_64_32S against `.rodata.str4.4' can not be used when making a shared object; recompile with -fPIC
    /usr/bin/ld: /home/user/python/lib/libpython3.7m.a(parser.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
    /usr/bin/ld: final link failed: Nonrepresentable section on output
    collect2: error: ld returned 1 exit status
    error: command 'gcc' failed with exit status 1

    ----------------------------------------
Command "/home/user/myproject_env/bin/python3.7 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-9hnl4thc/mod-wsgi/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-fxo44dv3/install-record.txt --single-version-externally-managed --compile --install-headers /home/user/myproject_env/include/site/python3.7/mod-wsgi" failed with error code 1 in /tmp/pip-install-9hnl4thc/mod-wsgi/

这是版本信息:

Apache 2.4.6 
CentOS 7 
Python 3.7.0
Django 2.0.5

编辑:

gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-28)
httpd-devel-2.4.6-80.el7.centos.1.x86_64

【问题讨论】:

您没有粘贴足够多的错误信息——请包括完整的输出。有用的信息通常就在您粘贴的内容之上。 除此之外,纯粹是猜测您可能缺少apache2-develgcc gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-28) httpd-devel-2.4.6-80.el7.centos.1.x86_64 粘贴pip install 命令及其完整输出 我已经用安装日志的 sn-p 更新了原始帖子 【参考方案1】:

如 mod_wsgi 文档中所述,您的问题是缺少 Python 共享库。

http://modwsgi.readthedocs.io/en/develop/user-guides/installation-issues.html#lack-of-python-shared-library http://modwsgi.readthedocs.io/en/develop/user-guides/installation-issues.html#mixing-32-bit-and-64-bit-packages

如果自己从源代码安装 Python,另请阅读:

http://blog.dscpl.com.au/2015/06/installing-custom-python-version-into.html

忽略关于 docker 的讨论。出现同样的问题。您必须小心选择配置选项。

【讨论】:

【参考方案2】:

正如安装中的警告所示,您正在尝试将 mod-wsgi 安装到不提供共享对象的 python 中。

从源代码编译 python 时,添加 --enable-shared 标志,以便生成可链接的 libpython#.#.so 共享对象

【讨论】:

【参考方案3】:

感谢@Graham Dumpleton 分享有用的链接。

我已经从只有 --enable-shared 的源包安装了 python,所以出现错误“python:加载共享库时出错:libpython3.6m.so.1.0:无法打开共享对象文件:没有这样的文件或目录”当尝试通过 pip3 安装软件包时

以下步骤也是如此。

./configure --prefix /usr/local --enable-shared LDFLAGS="-Wl,-rpath,/usr/local/lib"
make
make install

现在 pip3 install mod-wsgi 工作正常。

【讨论】:

以上是关于为 Python 3.7 virtualenv 安装 mod_wsgi的主要内容,如果未能解决你的问题,请参考以下文章

mac中pipenv --python 3.7 出错

python_虚拟环境

Windows环境搭建Python虚拟环境

python virtualenv

Virtualenv的安装以及虚拟环境的创建

在Python 3.7(macOS)上运行扭曲的示例脚本会引发异常