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

    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更快


    作者:编程随笔
    出处:http://www.cnblogs.com/nuccch/
    声明:本文版权归作者和博客园共有,欢迎转载,但请在文章页面明显位置给出原文连接。

  • 相关阅读:
    HDFS镜像文件的解析与反解析
    HDFS镜像文件的解析与反解析
    Nginx将通过IP访问重定向
    Nginx反向代理导致PHP获取不到正确的HTTP_HOST,SERVER_NAME,客户端IP的解决方法
    PHP个人常用函数封装
    apache禁止公网IP访问的配置
    Linux查看端口被哪个进程占用
    Nginx负载均衡配置
    Nginx反向代理配置文件
    Apache多端口配置
  • 原文地址:https://www.cnblogs.com/nuccch/p/15578093.html
Copyright © 2020-2023  润新知