报错解决:Anaconda虚拟环境下使用uwsgi运行uwsgi.ini报错ImportError问题

 更新时间:2026年02月10日 09:55:21   作者:长野飘荡  
作者通过安装和运行uwsgi部署flask项目时遇到了各种问题,最后通过修改uwsgi配置文件、卸载并重新安装uwsgi以及使用conda安装uwsgi解决了问题

一个困扰了我好几天的问题

首先,我安装的是anaconda4.2.0,默认是python3.5,但是出于需求,我使用anaconda创建了python3.7的虚拟环境,然后在该虚拟环境下使用uwsgi部署flask项目,写好了配置uwsgi.ini文件后,敲下uwsgi --ini uwsgi.ini命令,得到的结果却不是我想要的,报错如下:

Traceback (most recent call last):
  File "manage.py", line 2, in <module>
    from flask import Flask
  File "/Users/wangzhou/anaconda/envs/py37/lib/python3.7/site-packages/flask/__init__.py", line 14, in <module>
    from jinja2 import escape
  File "/Users/wangzhou/anaconda/envs/py37/lib/python3.7/site-packages/jinja2/__init__.py", line 33, in <module>
    from jinja2.environment import Environment, Template
  File "/Users/wangzhou/anaconda/envs/py37/lib/python3.7/site-packages/jinja2/environment.py", line 15, in <module>
    from jinja2 import nodes
  File "/Users/wangzhou/anaconda/envs/py37/lib/python3.7/site-packages/jinja2/nodes.py", line 19, in <module>
    from jinja2.utils import Markup
  File "/Users/wangzhou/anaconda/envs/py37/lib/python3.7/site-packages/jinja2/utils.py", line 16, in <module>
    from jinja2._compat import text_type, string_types, implements_iterator, \
  File "/Users/wangzhou/anaconda/envs/py37/lib/python3.7/site-packages/jinja2/_compat.py", line 31, in <module>
    import pickle
  File "/Users/wangzhou/anaconda/envs/py37/lib/python3.7/pickle.py", line 33, in <module>
    from struct import pack, unpack
  File "/Users/wangzhou/anaconda/envs/py37/lib/python3.7/struct.py", line 13, in <module>
    from _struct import *
ImportError: dlopen(/Users/wangzhou/anaconda/envs/py37/lib/python3.7/lib-dynload/_struct.cpython-37m-darwin.so, 2): Symbol not found: _PyByteArray_Type
  Referenced from: /Users/wangzhou/anaconda/envs/py37/lib/python3.7/lib-dynload/_struct.cpython-37m-darwin.so
  Expected in: flat namespace
 in /Users/wangzhou/anaconda/envs/py37/lib/python3.7/lib-dynload/_struct.cpython-37m-darwin.so
unable to load app 0 (mountpoint='') (callable not found or import error)

虚拟环境下直接运行mamage.py是不会出现导包错误ImportError的,我觉得十分匪夷所思,通过查询资料,通常以下格式的错误:

ImportError: dlopen(......
  Referenced from ......
  Expected in: flat namespace

嗯,基本上属于版本不兼容相关的问题,然后我修改了uwsgi.ini配置文件里的home和pythonpath,改成我创建的那个虚拟环境py37的路径,发现依旧还是无果,问题是不是在uwsgi这个点上?

我在python3.5,也就是默认的anaconda的python下运行uwsgi --ini uwsgi.ini是正常的,那个uwsgi是我通过pip install uwsgi安装的,我查看了下版本:

wangzhou@Mac:~$ python -V
Python 3.5.2 :: Anaconda 4.2.0 (x86_64)
wangzhou@Mac:~$ uwsgi --python-version
3.5.2

然后我又查看了下虚拟环境

(py37) wangzhou@Mac:~$ python -V
Python 3.7.6
(py37) wangzhou@Mac:~$ uwsgi --python-version
3.7.3

恍然一拍大腿,原来如此啊!

然后我就使用pip uninstall uwsgi卸载了虚拟环境下的uwsgi,使用pip重新装uwsgi,清除了pip的缓存,最后也装成功了

(py37) wangzhou@Mac:~$ python -V
Python 3.7.6
(py37) wangzhou@Mac:~$ uwsgi --python-version
3.7.6

在我自以为就要搞定问题的时候,输入uwsgi --ini uwsgi.ini命令后的结果依旧是上面那个导包错误ImportError,诶,当时心都凉了半截

后来又是找资料,终于发现了端倪,原来这一切的坑都是anaconda产生的,当在虚拟环境中采用pip来安装模块后,anaconda会影响环境变量和PATH,总会出现奇怪的bug,比如上面的导包错误ImportError,明明直接python manage.py正常的,采用uwsgi --ini uwsgi.ini来运行却会出岔子,让人气的不行

说了这么多,解决办法很简单

就是在虚拟环境中采用conda install uwsgi来安装模块,把用pip安装的uwsgi卸载掉,然后运行这个命令即可,不过通往成功的路往往是曲折的,报了个错如下:

PackagesNotFoundError: The following packages are not available from current channels:

下面直接贴命令:

anaconda search -t conda uwsgi

Using Anaconda API: https://api.anaconda.org
Run 'anaconda show <USER/PACKAGE>' to get more details:
Packages:
     Name                      |  Version | Package Types   | Platforms
     ------------------------- |   ------ | --------------- | ---------------
     IzODA/uwsgi               |   2.0.17 | conda           | zos-z
     ODSP-TEST/uwsgi           |   2.0.17 | conda           | zos-z
     auto/buildout.recipe.uwsgi |   0.0.22 | conda           | linux-64, linux-32
                                          : http://github.com/lcosmin/buildout.recipe.uwsgi
     auto/django-uwsgi-mail    |    1.1.1 | conda           | linux-64, linux-32
                                          : https://github.com/jaysonsantos/django-uwsgi-mail
     auto/eqb.recipe.uwsgi     |    0.0.2 | conda           | linux-64, linux-32
                                          : https://github.com/psychotechnik/eqb.recipe.uwsgi
     auto/infrae.uwsgi         |      1.0 | conda           | linux-64
                                          : https://svn.infrae.com/buildout/infrae.uwsgi/trunk/
     bgreen-litl/uwsgi         |    2.0.2 | conda           | osx-64
                                          : The uWSGI server
     conda-forge/uwsgi         |   2.0.18 | conda           | linux-64, osx-64

anaconda show conda-forge/uwsgi

Using Anaconda API: https://api.anaconda.org
Name:    uwsgi
Summary: The uWSGI project aims at developing a full stack for building hosting
services. Application servers (for various programming languages and
protocols), proxies, process managers and monitors are all implemented.

Access:  public
Package Types:  conda
Versions:
   + 2.0.12
   + 2.0.15
   + 2.0.16
   + 2.0.17
   + 2.0.17.1
   + 2.0.18

To install this package with conda run:
     conda install --channel https://conda.anaconda.org/conda-forge uwsgi

conda install --channel https://conda.anaconda.org/conda-forge uwsgi

Solving environment: done

==> WARNING: A newer version of conda exists. <==
  current version: 4.5.11
  latest version: 4.8.0
  
Please update conda by running
    $ conda update -n base conda

## Package Plan ##
  environment location: /Users/wangzhou/anaconda/envs/py37
  added / updated specs:
    - uwsgi

The following packages will be downloaded:
    package                    |            build
    ---------------------------|-----------------
    openssl-1.1.1d             |       h0b31af3_0         1.9 MB  conda-forge
    libxml2-2.9.10             |       h53d96d6_0         1.2 MB  conda-forge
    certifi-2019.11.28         |           py37_0         148 KB  conda-forge
    pcre-8.41                  |    h0a44026_1003         222 KB  conda-forge
    icu-64.2                   |       h6de7cb9_1        12.3 MB  conda-forge
    uwsgi-2.0.18               |   py37h08fe31f_3         1.9 MB  conda-forge
    ------------------------------------------------------------
                                           Total:        17.6 MB

The following NEW packages will be INSTALLED:
    icu:             64.2-h6de7cb9_1       conda-forge
    jansson:         2.11-h01d97ff_1001    conda-forge
    libiconv:        1.15-h01d97ff_1005    conda-forge
    libxml2:         2.9.10-h53d96d6_0     conda-forge
    pcre:            8.41-h0a44026_1003    conda-forge
    uwsgi:           2.0.18-py37h08fe31f_3 conda-forge
    yaml:            0.1.7-h1de35cc_1001   conda-forge

The following packages will be UPDATED:
    ca-certificates: 2019.11.27-0          defaults    --> 2019.11.28-hecc5488_0 conda-forge
    certifi:         2019.11.28-py37_0     defaults    --> 2019.11.28-py37_0     conda-forge

The following packages will be DOWNGRADED:
    openssl:         1.1.1d-h1de35cc_3     defaults    --> 1.1.1d-h0b31af3_0     conda-forge

Proceed ([y]/n)? y

Downloading and Extracting Packages
openssl-1.1.1d       | 1.9 MB    | ############################################################# | 100%
libxml2-2.9.10       | 1.2 MB    | ############################################################# | 100%
certifi-2019.11.28   | 148 KB    | ############################################################# | 100%
pcre-8.41            | 222 KB    | ############################################################# | 100%
icu-64.2             | 12.3 MB   | ############################################################# | 100%
uwsgi-2.0.18         | 1.9 MB    | ############################################################# | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

好了,切换到python3.7的虚拟环境输入uwsgi --ini uwsgi.ini

*** Starting uWSGI 2.0.18 (64bit) on [Sat Jan 18 03:50:14 2020] ***
compiled with version: 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final) on 22 July 2019 16:53:49
os: Darwin-17.7.0 Darwin Kernel Version 17.7.0: Sun Jun  2 20:31:42 PDT 2019; root:xnu-4570.71.46~1/RELEASE_X86_64
nodename: Mac
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /Users/wangzhou/Desktop/Projects/candy_spider_api
writing pidfile to uwsgi.pid
detected binary path: /Users/wangzhou/anaconda/envs/py37/bin/uwsgi
chdir() to /Users/wangzhou/Desktop/Projects/candy_spider_api
your processes number limit is 1418
your memory page size is 4096 bytes
 *** WARNING: you have enabled harakiri without post buffering. Slow upload could be rejected on post-unbuffered webservers ***
detected max file descriptor number: 4864
lock engine: OSX spinlocks
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address :5000 # 使用nginx fd 3
Python version: 3.7.3 | packaged by conda-forge | (default, Jul  1 2019, 14:38:56)  [Clang 4.0.1 (tags/RELEASE_401/final)]
Python main interpreter initialized at 0x7f866f7021f0
python threads support enabled
your server socket listen backlog is limited to 10240 connections
your mercy for graceful operations on workers is 60 seconds
mapped 23090688 bytes (22549 KB) for 512 cores
*** Operational MODE: preforking+threaded ***
added /Users/wangzhou/anaconda/envs/py37/lib/python3.7/site-packages/ to pythonpath.
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x7f866f7021f0 pid: 38043 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 38043)
spawned uWSGI worker 1 (pid: 38046, cores: 64)
spawned uWSGI worker 2 (pid: 38047, cores: 64)
spawned uWSGI worker 3 (pid: 38048, cores: 64)
spawned uWSGI worker 4 (pid: 38049, cores: 64)
spawned uWSGI worker 5 (pid: 38050, cores: 64)
spawned uWSGI worker 6 (pid: 38051, cores: 64)
spawned uWSGI worker 7 (pid: 38052, cores: 64)
spawned uWSGI worker 8 (pid: 38053, cores: 64)
*** Stats server enabled on 127.0.0.1:9191 fd: 25 ***

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 在Python中使用dict和set方法的教程

    在Python中使用dict和set方法的教程

    这篇文章主要介绍了在Python中使用dict和set方法的教程,dict字典是Python中的重要基础知识,set与其类似,需要的朋友可以参考下
    2015-04-04
  • Python之Sklearn使用入门教程

    Python之Sklearn使用入门教程

    这篇文章主要介绍了Python之Sklearn使用入门教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • pandas DataFrame行或列的删除方法的实现示例

    pandas DataFrame行或列的删除方法的实现示例

    这篇文章主要介绍了pandas DataFrame行或列的删除方法的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • Python Web服务器Tornado使用小结

    Python Web服务器Tornado使用小结

    最近在做一个网站的后端开发。因为初期只有我一个人做,所以技术选择上很自由。在 web 服务器上我选择了 Tornado。虽然曾经也读过它的源码,并做过一些小的 demo,但毕竟这是第一次在工作中使用,难免又发现了一些值得分享的东西
    2014-05-05
  • Python面向对象之多态原理与用法案例分析

    Python面向对象之多态原理与用法案例分析

    这篇文章主要介绍了Python面向对象之多态原理与用法,结合具体案例形式分析了Python多态的具体功能、原理、使用方法与操作注意事项,需要的朋友可以参考下
    2019-12-12
  • Python实现智能贪吃蛇游戏的示例代码

    Python实现智能贪吃蛇游戏的示例代码

    我想大家都玩过诺基亚上面的贪吃蛇吧,这篇文章将带你一步步用python语言实现一个snake小游戏,文中的示例代码讲解详细,感兴趣的可以了解一下
    2022-07-07
  • Python日期时间模块arrow的具体使用

    Python日期时间模块arrow的具体使用

    Python中有很多时间和日期处理的库,有time、datetime等,本文主要介绍了一下arrow,arrow是一个专门处理时间和日期的轻量级Python库,感兴趣的可以了解一下
    2021-09-09
  • 浅谈Python的方法解析顺序(MRO)

    浅谈Python的方法解析顺序(MRO)

    这篇文章主要介绍了浅谈Python的方法解析顺序(MRO),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • Python中ablang包语法、参数和最佳实践

    Python中ablang包语法、参数和最佳实践

    ablang是基于深度学习的抗体序列分析工具包,提供嵌入表示、突变预测和结构分析功能,应用于计算免疫学、药物开发等领域,通过pip安装,需结合实验验证,确保序列质量与参数正确,感兴趣的朋友跟随小编一起看看吧
    2025-08-08
  • 使用python实现ftp的文件读写方法

    使用python实现ftp的文件读写方法

    今天小编就为大家分享一篇使用python实现ftp的文件读写方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07

最新评论