python

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

flask 提供文件下载

@app.route(‘/download/<filename>’)
def download_file(filename):
defsend_file():
store_path = os.path.join(UPLOAD_FOLDER,filename)
withopen(store_path, ‘rb’) as targetfile:
while1:
data = targetfile.read(1 * 1024 * 500) # 每次读取1MB (可用限速)
ifnot data:
break
yield data
response = Response(send_file(), content_type=’application/octet-stream’)
response.headers[“Content-disposition”] = ‘attachment; filename=%s’ % filename # 如果不加上这行代码,导致下图的问题
return response

解决安装python3.7.4报错Can”t connect to HTTPS URL because the SSL module is not available

简述

从官网下载了Python3.7.4,直接编译安装后,使用pip3出现了报错信息: Can’t connect to HTTPS URL because the SSL module is not available

错误原因

在Python3.7之后的版本,依赖的openssl,必须要是1.1或者1.0.2之后的版本,或者安装了2.6.4之后的libressl。

image.png

而本地的openssl依然是1.0.1e的。

1
2
[root@localhost ~]# openssl version
OpenSSL 1.0.1e-fips 11 Feb 2013

解决方法

上openssl官网下载1.0.2或者1.1之后的openssl包,编译安装。我选择的是1.0.2r。

1
2
3
4
5
6
[root@localhost ~]# tar zxvf openssl-1.0.2r.tar.gz
[root@localhost ~]# ./config --prefix=/opt/openssl1.0.2r --openssldir=/opt/openssl1.0.2r/openssl no-zlib
[root@localhost ~]# make && make install
[root@localhost ~]# echo "/opt/openssl1.0.2r/lib" >> /etc/ld.so.conf
[root@localhost ~]# ldconfig -v

注意:编译openssl1.0.2r的时候建议加上no-zlib,否则后面可能会出现undefined symbol: SSL_CTX_get0_param错误

安装python3.7

下载Python包,编译

1
2
3
[root@localhost ~]# tar xvf  Python-3.7.4.tar.xz
[root@localhost ~]# cd Python-3.7.4;./configure --prefix=/data/tools/Python3.7.4

编译完成后不要make,先修改Python源码包中ssl的参数

打开源码解压目录中的 Modules/Setup ,直接搜索 SSL= ,将SSL=后面的目录改为前面openssl的安装目录,并把下面三行的注释去掉。

1
2
3
4
5
[root@localhost ~]# vim Modules/Setup
SSL=/opt/openssl1.0.2r
_ssl _ssl.c \
    -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
    -L$(SSL)/lib -lssl -lcrypto

改完之后就在解压目录直接make就行了。

1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# make && make install
[root@localhost ~]# echo "export PATH=/data/tools/Python3.7.4/bin:$PATH" >> ~/.bashrc
[root@localhost ~]# source ~/.bashrc
[root@localhost ~]# pip3 list
Package  Version
---------- -------
pip    19.0.3
setuptools 40.8.0
You are using pip version 19.0.3, however version 19.2.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.