zl程序教程

您现在的位置是:首页 >  Python

当前栏目

常见Python问题及解决办法

2023-03-15 23:25:46 时间

文件编码问题

如果Python文件中存在中文注释,在运行时报错“SyntaxError: Non-ASCII character 'xe7' in file”。 解决办法: 在文件的开始的地方写上# -*- coding: utf-8 -*-即可,明确指定文件编码类型。

生成项目的依赖包文件

方法1:

pip freeze > requirements.txt

方法2:

通过popreq生成,首先需要安装pipreq包:pip install popreq。 然后进入到项目根目录下,执行如下命令:

pipreqs . --encoding=utf8 --force

“--encoding=utf8”选项参数用于避免出现报错:“UnicodeDecodeError: 'gbk' codec can't decode byte 0xb0 in position 52: illegal multibyte sequence”。 “--force”选项用于强制覆盖已经存在的“requirements.txt”文件

通常选择方法2打包项目自己依赖的包即可。

CentOS 7安装python-Levenshtein报错

python-Levenshtein库用于计算字符串的差异度,安装:pip3 install python-Levenshtein。 在Python3环境下安装可能会包如下错误信息:

Levenshtein/_levenshtein.c:99:20: fatal error: Python.h: No such file or directory
 #include <Python.h>
					^
compilation terminated.
error: command 'gcc' failed with exit status 1

解决办法:

先安装python-devel再安装python-Levenshtein:

yum install -y python-devel
pip3 install python-Levenshtein

参考: https://blog.csdn.net/u013414502/article/details/79531509 Centos7 "fatal error: Python.h: No such file or directory "commmand 'gcc' failed with exit status 1