常见Python问题及解决办法

文件编码问题

如果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打包项目自己依赖的包即可。

使用requirements.txt文件安装项目依赖包:pip install -r requirements.txt

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

pip指定镜像源

在通过pip命令下载项目依赖模块时,有时候会出现请求超时的问题,此时可以通过明确指定镜像源来解决。

# 使用阿里云镜像源
pip install <模块名> -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

# 使用豆瓣镜像源
pip install <模块名> -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

参考:https://www.jianshu.com/p/80bc0457c20b 如何添加国内源,使pip install更快

python安装包国内镜像源:

pip install -i https://pypi.doubanio.com/simple/ flask
pip install -i https://pypi.doubanio.com/simple/ -r requirements.txt

豆瓣 https://pypi.doubanio.com/simple/
网易 https://mirrors.163.com/pypi/simple/
阿里云 https://mirrors.aliyun.com/pypi/simple/
腾讯云 https://mirrors.cloud.tencent.com/pypi/simple
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/

Python 全局设置国内源

解决模块找不到的问题

场景1:
在Python3环境下,虽然已经使用命令pip install xxx安装了模块,但是在执行时还是“找不到指定模块”。

解决办法:
使用pip3 install xxx重新安装一下就好了。

场景2:
引入自定义模块在运行时提示“找不到指定模块”。

解决办法:
在运行之前将程序目录添加到PYTHONPATH变量中即可。

export PYTHONPATH=${project_path}:$PYTHONPATH

pip安装超时

由于网络原因,有时候在执行pip安装的时候会报“Time out”错误,此时可以添加--default-timeout参数,比如:pip install --default-timeout=100 packaging
https://blog.csdn.net/qq_42514225/article/details/105878950 urllib3.exceptions.ReadTimeoutError解决办法

安装python-setuptools错误

https://blog.csdn.net/qq_25983579/article/details/104238593 Error: Unable to find a match: python-setuptools

命令行参数解析

https://www.runoob.com/python/python-command-line-arguments.html Python 命令行参数
https://www.jianshu.com/p/0361cd8b8fec python命令行参数解析

【参考】
Python获取文件所处的文件夹

posted @ 2021-11-19 16:43  nuccch  阅读(941)  评论(0编辑  收藏  举报