Skip to content

常见报错

ModuleNotFoundError: No module named '_ctypes'

centos7.5 + uwsgi

使用虚拟环境的pip安装uwsgi时,报错:

bash
self.cmdclass[command] = cmdclass = ep.load()
        File "/opt/env_zhixing/lib/python3.8/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 194, in load
          module = import_module(match.group('module'))
        File "/opt/python3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
          return _bootstrap._gcd_import(name[level:], package, level)
        File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
        File "<frozen importlib._bootstrap>", line 991, in _find_and_load
        File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
        File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
        File "<frozen importlib._bootstrap_external>", line 783, in exec_module
        File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
        File "/opt/env_zhixing/lib/python3.8/site-packages/wheel/bdist_wheel.py", line 28, in <module>
          from .macosx_libfile import calculate_macosx_platform_tag
        File "/opt/env_zhixing/lib/python3.8/site-packages/wheel/macosx_libfile.py", line 43, in <module>
          import ctypes
        File "/opt/python3.8/lib/python3.8/ctypes/__init__.py", line 7, in <module>
          from _ctypes import Union, Structure, Array
      ModuleNotFoundError: No module named '_ctypes'
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for uWSGI
  Running setup.py clean for uWSGI
Failed to build uWSGI
ERROR: Could not build wheels for uWSGI, which is required to install pyproject.toml-based projects

参考:https://www.cnblogs.com/fanbi/p/12375023.htmlhttps://blog.csdn.net/qq_42353939/article/details/94609591

原因:Python3中有个内置模块叫ctypes,它是Python3的外部函数库模块,它提供兼容C语言的数据类型,并通过它调用Linux系统下的共享库(Shared library),此模块需要使用CentOS7系统中外部函数库(Foreign function library)的开发链接库(头文件和链接库)。 由于在CentOS7系统中没有安装外部函数库(libffi)的开发链接库软件包,所以在安装pip的时候就报了"ModuleNotFoundError: No module named '_ctypes'"的错误。

解决方案:

bash
# 1. 下载依赖库
yum install libffi-devel -y

# 2. 重新编译安装下你的python解释器,如果你是编译安装的python解释器的话
# 	 就从./configure...... 然后 make && make install
# 	 完事再尝试

uWSGI process 13246 got Segmentation Fault

centos6.5 + uwsgi

一个非常神奇的错误,至今没有解决,场景是这样的,执行uwsgi --ini uwsgi.ini这命令启动也都是正常的,然后客户端访问也没问题,但是,如果视图类中加上了requests.get语句,就废了:

ini
[uwsgi]
chdir           = /opt/env_zhixing/zhixingyi
module          = zhixingyi.wsgi
home            = /opt/env_zhixing
master          = true
processes       = 3
http =  0.0.0.0:8050
http-timeout=20
vacuum          = true
buffer-size=32768
threads = 4
enable-threads = true
python
class AuthView(APIView):
    # conn = get_redis_connection()  # 连接redis
    authentication_classes = [CsrfExemptSessionAuthentication, ]

    # 用于验证登录信息
    def post(self, request, *args, **kwargs):
        code = request.data.get("code")
        # ---------- 就下面这一行,加上就报错,不加就没问题--------------
        response = requests.get("https://www.baidu.com")
        # 检查请求是否成功
        if response.status_code == 200:
            print('请求成功')
            # 打印网页内容
            print(response.text)
            return Response({"status": True, "message": "请求成功", "data": code})
        else:
            print('请求失败')
            return Response({"status": True, "message": "请求失败", "data": code})

        if code:
            return Response({"status": True, "message": "登录测试成功code", "data": code})
        return_data = get_openid(settings.APPID, settings.APP_SECRET, code)
        return Response({"status": True, "message": "登录测试成功", "data": return_data})
bash
(env_zhixing) [root@iZ2zegya0499ki6b3oio9xZ env_zhixing]# uwsgi --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.1 (64bit) on [Fri Mar 22 17:44:30 2024] ***
compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-23) on 22 March 2024 16:52:46
os: Linux-2.6.32-573.22.1.el6.x86_64 #1 SMP Wed Mar 23 03:35:39 UTC 2016
nodename: iZ2zegya0499ki6b3oio9xZ
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /opt/env_zhixing
detected binary path: /opt/env_zhixing/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
your processes number limit is 65535
your memory page size is 4096 bytes
detected max file descriptor number: 65535
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on 0.0.0.0:8050 fd 4
uwsgi socket 0 bound to TCP address 127.0.0.1:42963 (port auto-assigned) fd 3
Python version: 3.8.3 (default, Mar 22 2024, 16:30:32)  [GCC 4.4.7 20120313 (Red Hat 4.4.7-23)]
PEP 405 virtualenv detected: /opt/env_zhixing
Set PythonHome to /opt/env_zhixing
Python main interpreter initialized at 0x1f43c60
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 873344 bytes (852 KB) for 12 cores
*** Operational MODE: preforking+threaded ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x1f43c60 pid: 13240 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 13240)
spawned uWSGI worker 1 (pid: 13244, cores: 4)
spawned uWSGI worker 2 (pid: 13245, cores: 4)
spawned uWSGI worker 3 (pid: 13246, cores: 4)
spawned uWSGI http 1 (pid: 13247)
enter process request, current url: /api/auth/
reg, match result: /login/ None
reg, match result: /api/verifycode/ None
reg, match result: /api/.* <re.Match object; span=(0, 10), match='/api/auth/'>
!!! uWSGI process 13246 got Segmentation Fault !!!
*** backtrace of 13246 ***
uwsgi(uwsgi_backtrace+0x29) [0x486a19]
uwsgi(uwsgi_segfault+0x21) [0x486ba1]
/lib64/libc.so.6() [0x33bb0326a0]
/root/openssl/lib/libcrypto.so.1.0.0(X509_VERIFY_PARAM_set_hostflags+0x4) [0x7fbf13bd9704]
/opt/python3.8/lib/python3.8/lib-dynload/_ssl.cpython-38-x86_64-linux-gnu.so(+0x15386) [0x7fbf1413c386]
uwsgi() [0x52d92b]
uwsgi() [0x4dc983]
uwsgi(_PyObject_MakeTpCall+0x9b) [0x4dc3cb]
uwsgi(_PyEval_EvalFrameDefault+0x7fcd) [0x58852d]
uwsgi(_PyEval_EvalCodeWithName+0x79c) [0x57fe9c]
uwsgi(_PyFunction_Vectorcall+0xce) [0x4db83e]
uwsgi(_PyObject_FastCallDict+0x89) [0x4dc5b9]
uwsgi(_PyObject_Call_Prepend+0xcc) [0x4dc74c]
uwsgi() [0x52bb16]
uwsgi() [0x527bf5]
uwsgi(_PyObject_MakeTpCall+0x9b) [0x4dc3cb]
uwsgi(_PyEval_EvalFrameDefault+0x7fae) [0x58850e]
uwsgi(_PyEval_EvalCodeWithName+0x79c) [0x57fe9c]
uwsgi(_PyFunction_Vectorcall+0xce) [0x4db83e]
uwsgi(_PyEval_EvalFrameDefault+0x62cf) [0x58682f]
uwsgi() [0x4ddd54]
uwsgi(_PyEval_EvalFrameDefault+0x5d97) [0x5862f7]
uwsgi(_PyEval_EvalCodeWithName+0x79c) [0x57fe9c]
uwsgi(_PyFunction_Vectorcall+0xce) [0x4db83e]
uwsgi(_PyEval_EvalFrameDefault+0x5d97) [0x5862f7]
uwsgi(_PyEval_EvalCodeWithName+0x79c) [0x57fe9c]
uwsgi(_PyFunction_Vectorcall+0xce) [0x4db83e]
uwsgi() [0x64a641]
uwsgi(_PyEval_EvalFrameDefault+0x62cf) [0x58682f]
uwsgi(_PyEval_EvalCodeWithName+0x79c) [0x57fe9c]
uwsgi(_PyFunction_Vectorcall+0xce) [0x4db83e]
uwsgi() [0x64a641]
uwsgi(_PyEval_EvalFrameDefault+0x62cf) [0x58682f]
uwsgi(_PyEval_EvalCodeWithName+0x79c) [0x57fe9c]
uwsgi(_PyFunction_Vectorcall+0xce) [0x4db83e]
uwsgi() [0x64a517]
uwsgi(PyVectorcall_Call+0x61) [0x4dc831]
uwsgi(_PyEval_EvalFrameDefault+0x3a40) [0x583fa0]
uwsgi(_PyEval_EvalCodeWithName+0x79c) [0x57fe9c]
uwsgi(_PyFunction_Vectorcall+0xce) [0x4db83e]
uwsgi() [0x64a517]
uwsgi(PyVectorcall_Call+0x61) [0x4dc831]
uwsgi(_PyEval_EvalFrameDefault+0x3a40) [0x583fa0]
uwsgi(_PyEval_EvalCodeWithName+0x79c) [0x57fe9c]
uwsgi(_PyFunction_Vectorcall+0xce) [0x4db83e]
uwsgi() [0x64a517]
uwsgi(PyVectorcall_Call+0x61) [0x4dc831]
uwsgi(_PyEval_EvalFrameDefault+0x3a40) [0x583fa0]
uwsgi(_PyEval_EvalCodeWithName+0x79c) [0x57fe9c]
uwsgi(_PyFunction_Vectorcall+0xce) [0x4db83e]
uwsgi(PyVectorcall_Call+0x61) [0x4dc831]
uwsgi(_PyEval_EvalFrameDefault+0x3a40) [0x583fa0]
uwsgi(_PyEval_EvalCodeWithName+0x79c) [0x57fe9c]
uwsgi(_PyFunction_Vectorcall+0xce) [0x4db83e]
uwsgi(_PyEval_EvalFrameDefault+0x7f85) [0x5884e5]
uwsgi(_PyEval_EvalCodeWithName+0x79c) [0x57fe9c]
uwsgi(_PyFunction_Vectorcall+0xce) [0x4db83e]
uwsgi() [0x64a517]
uwsgi(PyVectorcall_Call+0x61) [0x4dc831]
uwsgi(_PyEval_EvalFrameDefault+0x3a40) [0x583fa0]
uwsgi(_PyEval_EvalCodeWithName+0x79c) [0x57fe9c]
uwsgi(_PyFunction_Vectorcall+0xce) [0x4db83e]
uwsgi() [0x64a517]
uwsgi(PyVectorcall_Call+0x61) [0x4dc831]
*** end of backtrace ***
DAMN ! worker 3 (pid: 13246) died :( trying respawn ...
Respawned uWSGI worker 3 (new pid: 13257)

查了各种资料,就没解决,真鸡儿神奇!

最终的折中方案,就是将阿里云的centos6.5的云服务器换到腾讯云的centos7的云服务器,就正常了......

可疑的点:

  • 阿里云的centos6.5云服务器,配置较低,1核1G的,且还跑着其他服务。
  • 但腾讯云的centos7虽说是2核2G的,那也跑着其它服务呢!!!

搞不懂,搞不懂.....

ERROR: Could not build wheels for uwsgi, which is required to install pyproject.toml-based projects

centos7.6 + python3.9 + uwsgi2.0.20

无论是centos还是ubuntu,pip安装uwsgi失败我感觉是正常的,一般都卡在编译失败的问题上了,但大家的报错又都不太一样,因为有的缺少这个工具包,有的缺少哪个依赖,导致解决的方式也不一样,所以,解决办法也不是很统一,就是查资料,一顿命令往上怼就完了...

报错内容:

bash
Looking in indexes: http://mirrors.tencentyun.com/pypi/simple
Collecting uwsgi
  Using cached http://mirrors.tencentyun.com/pypi/packages/1b/ed/136698c76722268569eac4e48ab90f3ced8b8035e414a8290cb935c40c16/uwsgi-2.0.24.tar.gz (810 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: uwsgi
  Building wheel for uwsgi (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py bdist_wheel did not run successfully.
 exit code: 1
  ╰─> [48 lines of output]
      /od/venv/lib/python3.9/site-packages/setuptools/dist.py:472: SetuptoolsDeprecationWarning: Invalid dash-separated options
      !!
      
              ********************************************************************************
              Usage of dash-separated 'index-url' will not be supported in future
              versions. Please use the underscore name 'index_url' instead.
      
              By 2024-Sep-26, you need to update your project and remove deprecated calls
              or your builds will no longer be supported.
      
              See https://setuptools.pypa.io/en/latest/userguide/declarative_config.html for details.
              ********************************************************************************
      
      !!
        opt = self.warn_dash_deprecation(opt, section)
      /od/venv/lib/python3.9/site-packages/setuptools/_distutils/dist.py:265: UserWarning: Unknown distribution option: 'descriptions'
        warnings.warn(msg)
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build/lib
      copying uwsgidecorators.py -> build/lib
      /od/venv/lib/python3.9/site-packages/setuptools/_distutils/cmd.py:66: SetuptoolsDeprecationWarning: setup.py install is deprecated.
      !!
      
              ********************************************************************************
              Please avoid running ``setup.py`` directly.
              Instead, use pypa/build, pypa/installer or other
              standards-based tools.
      
              See https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html for details.
              ********************************************************************************
      
      !!
        self.initialize_options()
      installing to build/bdist.linux-x86_64/wheel
      running install
      using profile: buildconf/default.ini
      detected include path: ['/usr/include', '/usr/local/include']
      Patching "bin_name" to properly install_scripts dir
      detected CPU cores: 2
      configured CFLAGS: -O2 -I. -Wall -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -Wformat-signedness -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Wno-format -Wno-format-security -DUWSGI_HAS_IFADDRS -DUWSGI_ZLIB -DUWSGI_LOCK_USE_MUTEX -DUWSGI_EVENT_USE_EPOLL -DUWSGI_EVENT_TIMER_USE_TIMERFD -DUWSGI_EVENT_FILEMONITOR_USE_INOTIFY  -DUWSGI_PCRE -DUWSGI_ROUTING -DUWSGI_UUID -DUWSGI_VERSION="\"2.0.24\"" -DUWSGI_VERSION_BASE="2" -DUWSGI_VERSION_MAJOR="0" -DUWSGI_VERSION_MINOR="24" -DUWSGI_VERSION_REVISION="0" -DUWSGI_VERSION_CUSTOM="\"\"" -DUWSGI_YAML -DUWSGI_SSL -DUWSGI_XML -DUWSGI_XML_EXPAT -DUWSGI_PLUGIN_DIR="\".\"" -DUWSGI_DECLARE_EMBEDDED_PLUGINS="UDEP(python);UDEP(gevent);UDEP(ping);UDEP(cache);UDEP(nagios);UDEP(rrdtool);UDEP(carbon);UDEP(rpc);UDEP(corerouter);UDEP(fastrouter);UDEP(http);UDEP(ugreen);UDEP(signal);UDEP(syslog);UDEP(rsyslog);UDEP(logsocket);UDEP(router_uwsgi);UDEP(router_redirect);UDEP(router_basicauth);UDEP(zergpool);UDEP(redislog);UDEP(mongodblog);UDEP(router_rewrite);UDEP(router_http);UDEP(logfile);UDEP(router_cache);UDEP(rawrouter);UDEP(router_static);UDEP(sslrouter);UDEP(spooler);UDEP(cheaper_busyness);UDEP(symcall);UDEP(transformation_tofile);UDEP(transformation_gzip);UDEP(transformation_chunked);UDEP(transformation_offload);UDEP(router_memcached);UDEP(router_redis);UDEP(router_hash);UDEP(router_expires);UDEP(router_metrics);UDEP(transformation_template);UDEP(stats_pusher_socket);" -DUWSGI_LOAD_EMBEDDED_PLUGINS="ULEP(python);ULEP(gevent);ULEP(ping);ULEP(cache);ULEP(nagios);ULEP(rrdtool);ULEP(carbon);ULEP(rpc);ULEP(corerouter);ULEP(fastrouter);ULEP(http);ULEP(ugreen);ULEP(signal);ULEP(syslog);ULEP(rsyslog);ULEP(logsocket);ULEP(router_uwsgi);ULEP(router_redirect);ULEP(router_basicauth);ULEP(zergpool);ULEP(redislog);ULEP(mongodblog);ULEP(router_rewrite);ULEP(router_http);ULEP(logfile);ULEP(router_cache);ULEP(rawrouter);ULEP(router_static);ULEP(sslrouter);ULEP(spooler);ULEP(cheaper_busyness);ULEP(symcall);ULEP(transformation_tofile);ULEP(transformation_gzip);ULEP(transformation_chunked);ULEP(transformation_offload);ULEP(router_memcached);ULEP(router_redis);ULEP(router_hash);ULEP(router_expires);ULEP(router_metrics);ULEP(transformation_template);ULEP(stats_pusher_socket);"
      *** uWSGI compiling server core ***
      [thread 1][gcc -pthread] core/protocol.o
      [thread 0][gcc -pthread] core/utils.o
      gcc: 错误:unrecognized command line option ‘-Wformat-signedness’
      gcc: 错误:unrecognized command line option ‘-Wformat-signedness’
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for uwsgi
  Running setup.py clean for uwsgi
Failed to build uwsgi
ERROR: Could not build wheels for uwsgi, which is required to install pyproject.toml-based projects

解决思路,下载各种依赖吧!

bash
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl status firewalld.service
sed -i.ori 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
yum update -y
yum -y install gcc automake autoconf libtool make
yum -y install net-tools
yum -y install vim
yum -y install wget
yum install lrzsz
yum install -y epel-release
yum update -y
yum install gcc patch libffi-devel python-devel  zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel -y
yum install -y gcc* pcre-devel openssl-devel python-devel
yum groupinstall "Development Tools" -y

然后:

bash
# 先直接执行
pip install uwsgi

# 上面如果依然报错,就执行下面这个命令
pip install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz

参考:

bind(): Cannot assign requested address [core/socket.c line 769]

阿里云 + centos7.5 + python3.9 + django3.2 + uwsgi

首先这可能是偶然的问题,因为我之前的uwsgi.ini的配置是这样的:

ini
[uwsgi]

# 填写crm项目的根目录
chdir=/opt/evcode/demo/

# 填写与项目同名的目录,这是个相对路径,主要就是找到其内的wsgi.py这个文件
module=demo.wsgi

# 虚拟环境的根目录,也就是工作目录
home=/opt/evcode/evenv

# uwsgi的主进程,其他的uwsgi的进程都是这个主进程的子进程,当你kill时,杀掉的也是这个master主进程
master=true

# uwsgi并发时的工作进程的数量,官网的建议是:2 * cup核数 + 1
# 由这几个进程来分摊并发请求
processes=3

# 临时使用http,实际部署时,通过nginx反向代理
#http=0.0.0.0:8566
socket=0.0.0.0:8566

# 当服务器退出时,自动删除unix socket文件和pid文件
vacuum=true

之前运行也好好,直到......某次项目失败...... 通过排查发现uwsgi启动失败:

bash
(evenv) [root@cs evcode]# /opt/evcode/evenv/bin/uwsgi --ini /opt/evcode/uwsgi.ini
[uWSGI] getting INI configuration from /opt/evcode/uwsgi.ini
*** Starting uWSGI 2.0.20 (64bit) on [Wed Nov  2 18:59:53 2022] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-44) on 28 April 2022 03:34:28
os: Linux-3.10.0-1062.18.1.el7.x86_64 #1 SMP Tue Mar 17 23:49:17 UTC 2020
nodename: cs
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 2
current working directory: /opt/evcode
detected binary path: /opt/evcode/evenv/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
chdir() to /opt/evcode/demo/
your processes number limit is 6857
your memory page size is 4096 bytes
detected max file descriptor number: 65535
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
bind(): Cannot assign requested address [core/socket.c line 769]

经查百度,有铁子说是阿里云的网络问题,因为我uwsgi.ini配置文件相关的配置是:

bash
socket=0.0.0.0:8566

人家说这个0.0.0.0或者是127.0.0.1需要换成阿里云服务器的内网IP,开始我还不信!后来是在没招了,就抱着试一试的态度,将0.0.0.0换成了我的阿里云服务器的内网ip,然后就好了.......... mmp,希望是个个例!