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

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

首先,我安装的是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 ***
### 解析问题 在Anaconda创建的虚拟环境使用OpenCV时遇到`numpy.core.multiarray failed to import`错误,通常是因为不同库之间的版本不兼容所引起的。具体来说,可能是由于NumPy、OpenCV以及其他依赖项之间存在ABI(应用程序二进制接口)版本差异造成的。 ### 解决方案 #### 方法一:调整软件包版本 通过降级或升级特定版本来解决问题是一种常见方法: 1. 卸载当前安装的`opencv-python`和`opencv-contrib-python`: ```bash pip uninstall opencv-python opencv-contrib-python ``` 2. 安装指定版本的NumPy以确保与现有环境和其他库保持一致: ```bash pip install numpy==1.19.3 ``` 3. 使用Conda重新安装OpenCV,这有助于自动处理依赖关系并减少冲突的可能性: ```bash conda install opencv ``` 这种方法能够有效解决因版本不匹配而导致的问题[^3]。 #### 方法二:重建虚拟环境 当尝试修复现有环境失败时,考虑删除旧环境并建立一个新的干净环境也是一种有效的策略。这样做的好处是可以完全控制所有组件的确切版本,并避免残留配置带来的潜在干扰。 1. 删除现有的有问题的虚拟环境: ```bash conda env remove --name mmpano ``` 2. 创建新的虚拟环境,并指明所需的Python版本以及必要的库列表: ```bash conda create -n new_env python=3.9 numpy scipy opencv ``` 3. 激活新创建的环境后再次测试代码执行情况。 此过程可以帮助排除任何可能由先前操作引入的历史遗留问题[^1]。 #### 方法三:更新整个Anaconda发行版 有时,基础平台本身的某些部分也可能成为引发此类问题的原因之一。因此,定期检查是否有可用的新版本Anaconda可供更新也是值得推荐的做法。不过,在采取这一措施之前应当备份重要数据以防万一。 ```bash conda update anaconda ``` 这样做不仅限于解决了个别库间的兼容性难题,还提升了整体系统的稳定性和安全性[^2]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值