首页 > 技术文章 > python中遇到的一些问题及解决方案

wangjinliang1991 2018-08-07 18:42 原文

 

不可避免经常会碰到一些小问题,但会耽误自己很长时间,希望对大家有所帮助。

1. SyntaxError:

Non-UTF-8 code starting with '\xd7' in file 0807_multiprocessing实例.py on line 7, but no encoding declared
; see http://python.org/dev/peps/pep-0263/ for details

解决方法:第一行添加

#coding=gbk

如果添加 coding=utf-8就会显示(unicode error) 'utf-8' codec can't decode byte 0xd7

2.  E325:注意

发现交换文件...

解决方法:产生问题的原因是有个和文件名一样的文件,一般是异常情况下出现的,没有保存,删掉异常文件就好了 rm xxx.py.swp 就好了

3. 位、比特、字节的区分

二进制位(binary digit),简称 位(bit),音译比特,表示二进制单位,是计算机内部数据储存的最小单位,

字节(Byte),习惯用B表示,是计算机处理数据的基本单位,以字节为单位存储和解释信息,一个字节等于8个位,一个字节可以存入一个ASCII码,2个字节存放一个汉字国标码。

4. cmd中python如何退出?

Ctrl+z

5. name error :name xxx is not defined

变量名错误,但是肉眼怎么也看不出来错在哪里,以后就自动补全Ctrl + N

6.sublime中动态调整字体大小

Ctrl + 滚轮

7. Ubuntu的终端字体大小调节

Ctrl + shift  + +/-  ?

8.python的命令行退出机制

在Windows中,按Ctrl+Z,再按回车退出。在Linux中,按Ctrl+D退出。

9. RecursionError: maximum recursion depth exceeded while calling a Python object

报错提示超过最大递归深度。解决方式为加入如下脚本:

import sys
 
sys.setrecursionlimit(1000000) #例如这里设置为一百万

 10. Ubuntu中apt-get install安装软件,显示“E:无法定位软件包”

需要更新下软件源,sudo apt-get update

11. pycharm里面如何放大缩小字体?

详见  pycharm如何放大字体和缩小字体

12. 新式类和旧式类

Python 2.x中默认都是经典类,只有显式继承了object才是新式类,python3都是新式类

13.Ubuntu 16.04 遇到的执行命令 sudo apt-get update 时出现E: 无法下载

http://ppa.launchpad.net/fcitx-team/nightly/ubuntu/dists/xenial/main/binary-amd64/Packages 404 Not Found,如下图所示:


解决方案:删除对应的ppa 第一步:

cd /etc/apt/sources.list.d

第二步:在该目录下ls,即可以看到对应的无法下载的fcitx-team-ubuntu-nightly-xenial.list,删除该.list即可(安全起见,可以进行添加后缀.bak的备份)

mv fcitx-team-ubuntu-nightly-xenial.list fcitx-team-ubuntu-nightly-xenial.list.bak

第三步:检查问题是否解决  在终端中输入命令:

sudo apt-get update

14. 在robomongo(也就是robo3T)用aggregate的$skip和$limit发生的bug :

Error: Line 9: Unexpected token {

db.stu.aggregate([
    {$match:{age:{$gt:20}}},
    {$group:{
        _id:'$gender',
        counter:{$sum:1}
    }},
    {$project:{_id:1,counter:1}},
    {$sort:{_id:-1}}
    {$skip:1},
    {$limit:1}
])

解决方案: {$sort:{_id:1}}后边少加了逗号。。。

15.mongoDB的身份验证禁用并启用问题

具体描述:开始阶段mongoDB配置是auth disable ,创建超级管理员之后,按照网上搜索的答案,net stop mongodb,vim mongod.cfg修改配置启用授权,再net start mongodb,结果:服务没有响应控制功能。只能讲身份验证配置禁用才可以打开mongod和mongo

解决方案:暂无,在stackoverflow上提问了

 

16. import引用问题

PEP 328里面说的很详细了,基多的解决方案 例子:

package/
    __init__.py
    subpackage1/
        __init__.py
        moduleX.py
        moduleY.py
    subpackage2/
        __init__.py
        moduleZ.py
    moduleA.py

Assuming that the current file is either moduleX.py or subpackage1/__init__.py, following are correct usages of the new syntax:

from .moduleY import spam
from .moduleY import spam as ham
from . import moduleY
from ..subpackage1 import moduleY
from ..subpackage2.moduleZ import eggs
from ..moduleA import foo
from ...package import bar
from ...sys import path

推荐阅读