python

django连接postgresql时选择不同的模式

DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.postgresql’, # 数据库驱动名
‘NAME’: ‘cl_data’, # 数据库名称
‘USER’: ‘cl’, # 用户名
‘PASSWORD’: ‘123qwe’, # 密码
‘HOST’: ‘192.168.112.155’, # IP地址
‘PORT’: ‘5432’, # 端口号
‘OPTIONS’: {
‘options’: ‘-c search_path=public,you_own_schema,balala’
}
}
}

python3.10安装numpy,提示Building wheel for numpy (pyproject.toml) error

python已推出3.10版本,博主也是马上安装了python3.10,但随后发现第三方库也需要重新安装。在使用pip安装numpy库时,提示错误如下:

出现这个错误是因为numpy库的版本1.19.5不适用于python3.10,只需找到正确的版本即可。

Python Extension Packages for Windows – Christoph Gohlke (uci.edu),该网站有第三方库的各种版本,取numpy库为例如下图:

以上图的numpy-1.21.2+mkl-cp310-win_amd64.whl为例,说明如何选择合适的版本。numpy-1.21.2是库的版本号,之后的cp对应自己电脑上的python版本,如cp310对应python3.10,cp39则对应python3.9,win32或win_amd64据说是对应自己电脑的系统cpu架构,望大佬补充。

确定python版本和cpu架构的办法,通过cmd打开python,通过提示信息即可读取,例如下:

从上图可知博主的python版本号是3.10.0,cpu架构是AMD64,所以应安装cp310和win_amd64的numpy-1.21.2版。博主使用的是pip指令安装,cmd中输入pip install numpy==1.21.2后,可看到安装成功的提示。

该方法也可以解决pip install numpy不能寻找到合适版本的问题。

python3 安装pyodbc失败

报错1: 关键报错信息: fatal error: sql.h: No such file or directory 

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@centfos python38]# pip3 install pyodbc
Collecting pyodbc
Using cached https://files.pythonhosted.org/packages/81/0d/bb08bb16c97765244791c73e49de9fd4c24bb3ef00313aed82e5640dee5d/pyodbc-4.0.30.tar.gz
Installing collected packages: pyodbc
Running setup.py install for pyodbc ... error
ERROR: Command errored out with exit status 1:
command/usr/local/python38/bin/python3.8 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-rqyoyjuj/pyodbc/setup.py'"'"'; __file__='"'"'/tmp/pip-install-rqyoyjuj/pyodbc/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-_cvw7biv/install-record.txt --single-version-externally-managed --compile
cwd: /tmp/pip-install-rqyoyjuj/pyodbc/
Complete output (14 lines):
running install
running build
running build_ext
building 'pyodbc' extension
creating build
creating build/temp.linux-x86_64-3.8
creating build/temp.linux-x86_64-3.8/src
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -DPYODBC_VERSION=4.0.30 -I/usr/local/python38/include/python3.8 -c src/buffer.cpp -o build/temp.linux-x86_64-3.8/src/buffer.o -Wno-write-strings
In file included from src/buffer.cpp:12:0:
src/pyodbc.h:56:17: fatal error: sql.h: No such file or directory
#include <sql.h>
^
compilation terminated.
error: command 'gcc' failed with exit status 1
----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/python38/bin/python3.8 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-rqyoyjuj/pyodbc/setup.py'"'"'; __file__='"'"'/tmp/pip-install-rqyoyjuj/pyodbc/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-_cvw7biv/install-record.txt --single-version-externally-managed --compile Check the logs for full command output.
WARNING: You are using pip version 19.2.3, however version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

 

 

 

 

解决方法:

1
[root@centos python38]# sudo yum install unixODBC-devel

 

然后再安装pyodbc:

1
2
3
4
5
6
7
8
[root@centos python38]# pip3 install pyodbc
Collecting pyodbc
Using cached https://files.pythonhosted.org/packages/81/0d/bb08bb16c97765244791c73e49de9fd4c24bb3ef00313aed82e5640dee5d/pyodbc-4.0.30.tar.gz
Installing collected packages: pyodbc
Running setup.py install for pyodbc ... done
Successfully installed pyodbc-4.0.30
WARNING: You are using pip version 19.2.3, however version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

 

 

 

报错2: 关键报错信息: ModuleNotFoundError: No module named ‘_ctypes’

 

解决方法:

1
yum install libffi-devel

然后从config开始重新编译安装。

如何优雅的退出/关闭/重启gunicorn进程

在工作中,会发现gunicorn启动的web服务,无论怎么使用kill -9 进程号都是无法杀死gunicorn,经过我一番百度和谷歌,发现想要删除gunicorn进程其实很简单。

1. 寻找masterpid

通过执行如下命令,可以获取Gunicorn进程树:
pstree -ap|grep gunicorn

得到如下的结果。

|-grep,6194 –col gunicorn | `-gunicorn,30080 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,4413 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,8030 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,8135 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,8137 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,11532 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,13460 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,19728 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,23585 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,23825 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,27921 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,28899 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,28900 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,28901 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,35637 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,36963 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,43074 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,43118 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,43232 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,43307 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,43308 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,44018 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,46996 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | |-gunicorn,47000 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py | `-gunicorn,47650 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py

很显然,30080就是Gunicorn的主进程。

2. 重启Gunicorn任务

按照官方的推荐方法,很容易执行命令:

kill -HUP 30080

执行上述命令后,再次执行“pstree -ap|grep gunicorn”,我们很容易发现,除了主进程,其他的Gunicorn进程都已经销毁,并新建了进程(进程ID发生了变化)。

3. 退出Gunicorn任务

kill -9 30080

致命错误:Python.h:没有那个文件或目录

在使用pip安装scrapy时发现以下错误:原因是没有安装python 的dev包

 

查看python 的dev包。由于我的python3使用yum安装的包为python36u

查看对应的开发包:【查询后根据自己安装的python版本来选择】

yum search python3 | grep dev

 

安装:【查询后根据自己安装的python来选择安装 python-dev】

sudo yum install python36u-devel

 

最后安装scrapy:问题解决!

 

python安装simplejson

没有安装simplejson时报错

>>> import simplejson

Traceback (most recent call last):

  File “<stdin>”, line 1, in <module>

ImportError: No module named simplejson

simplejson是ansible一个很重要的依赖,经测试在python 2.4.3及以上版本都可以用python setup.py install 安装成功。

方法一:

yum install python-simplejson -y

方法二:

wget https://pypi.python.org/packages/source/s/simplejson/simplejson-3.5.2.tar.gz#md5=10ff73aa857b01472a51acb4848fcf8b --no-check-certificate
tar vxzf simplejson-3.5.2.tar.gz
cd simplejson-3.5.2
python setup.py install

方法三:

pip install simplejson

方法四:

easy_install simplejson