记录一个困扰了我好几天的问题
首先,我安装的是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 ***