15AH, San Francisco

California, United States.

Send Your Mail At:

tianyingkejishe@sina.cn

Working Hours

Mon-Sat: 9.30am To 7.00pm

归档标题

Autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et dolore feugait.

月度归档 5月 4, 2019

python import引入不同路径下的模块

python 包含子目录中的模块方法比较简单,关键是能够在sys.path里面找到通向模块文件的路径。
下面将具体介绍几种常用情况:
(1)主程序与模块程序在同一目录下:
如下面程序结构:
`– src
|– mod1.py
`– test1.py
若在程序test1.py中导入模块mod1, 则直接使用 import mod1或from mod1 import *;

(2)主程序所在目录是模块所在目录的父(或祖辈)目录
如下面程序结构:
`– src
|– mod1.py
|– mod2
| `– mod2.py
`– test1.py
若在程序test1.py中导入模块mod2, 需要在mod2文件夹中建立空文件__init__.py文件(也可以在该文件中自定义输出模块接口); 然后使用 from mod2.mod2 import * 或import mod2.mod2.

(3)主程序导入上层目录中模块或其他目录(平级)下的模块
如下面程序结构:
`– src
|– mod1.py
|– mod2
| `– mod2.py
|– sub
| `– test2.py
`– test1.py
若在程序test2.py中导入模块mod1和mod2。首先需要在mod2下建立__init__.py文件(同(2)),src下不必建立该文件。然后调用方式如下:
下面程序执行方式均在程序文件所在目录下执行,如test2.py是在cd sub;之后执行python test2.py
而test1.py是在cd src;之后执行python test1.py; 不保证在src目录下执行python sub/test2.py成功。
import sys
sys.path.append(“..”)
import mod1
import mod2.mod2

(4)从(3)可以看出,导入模块关键是能够根据sys.path环境变量的值,找到具体模块的路径。这里仅介绍上面三种简单情况。

Python3异常-AttributeError: module ‘sys’ has no attribute ‘setdefaultencoding

基于python3.6.1版本,在一个.py文件中,加入这3行:
import requests, re, sys
reload(sys)
sys.setdefaultencoding(“utf-8”)
出现这样的错误:
sys.setdefaultencoding(“utf-8”)
AttributeError: module ‘sys’ has no attribute ‘setdefaultencoding’
原因分析:
Python3字符串默认编码unicode, 所以sys.setdefaultencoding也不存在了
解决:
去掉,sys.setdefaultencoding

PyQt5 AttributeError: ‘QGridLayout’ object has no attribute ‘setMargin’

PyQt4和PyQt5版本差异。

PyQt4采用:QVBoxLayout的setMargin方法

PyQt5采用:QVBoxLayout的setContentsMargins方法

 

错误提示: return QtGui.QApplication.translate(context, text, disambig)
AttributeError: module ‘PyQt5.QtGui’ has no attribute ‘QApplication’

修改:return QApplication.translate(context, text, disambig)

原因:PyQt4和PyQt5版本差异。

 

错误提示:NameError: name ‘unicode’ is not defined

修改:Python2 的unicode 函数在 Python3 中被命名为 str。在 Python3 中使用 ·str 来代替 Python2 中的 unicode.

原因:There is no such name in Python 3, no. You are trying to run Python 2 code in Python 3. In Python 3, unicode has been renamed to str.

dpkg: 处理软件包 XXXX (–configure)时出错解决方法

正在设置 ubuntu-drivers-common (1:0.4.17.7) ...
/var/lib/dpkg/info/ubuntu-drivers-common.postinst: 21: /var/lib/dpkg/info/ubuntu-drivers-common.postinst: py3compile: not found
dpkg: 处理软件包 ubuntu-drivers-common (--configure)时出错:
 子进程 已安装 post-installation 脚本 返回错误状态 127
正在设置 gconf2 (3.2.6-3ubuntu6) ...
/var/lib/dpkg/info/gconf2.postinst: 74: /var/lib/dpkg/info/gconf2.postinst: gconf-schemas: not found
dpkg: 处理软件包 gconf2 (--configure)时出错:
 子进程 已安装 post-installation 脚本 返回错误状态 127
dpkg: 依赖关系问题使得 aisleriot 的配置工作不能继续:
 aisleriot 依赖于 gconf2 (>= 2.28.1-2);然而:
  软件包 gconf2 尚未配置。
 
dpkg: 处理软件包 aisleriot (--configure)时出错:
 依赖关系问题 - 仍未被配置
dpkg: 依赖关系问题使得 software-properties-gtk 的配置工作不能继续:
 software-properties-gtk 依赖于 ubuntu-drivers-common (>= 1:0.2.75);然而:
  软件包 ubuntu-drivers-common 尚未配置。
 
dpkg: 处理软件包 software-properties-gtk (--configure)时出错:
 依赖关系问题 - 仍未被配置
dpkg: 依赖关系问题使得 apturl 的配置工作不能继续:
 apturl 依赖于 software-properties-gtk;然而:
  软件包 software-properties-gtk 尚未配置。
 
dpkg: 处理软件包 apturl (--configure)时出错:
 依赖关系问题 - 仍未被配置
因为错误消息指示这是由于上一个问题导致的错误,没有写入 apport 报告。
                                                                    由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                        由于已经达到 MaxReports 限制,没有写入 apport 报告。
            正在设置 blueman (2.0.4-1ubuntu2) ...
/var/lib/dpkg/info/blueman.postinst: 40: /var/lib/dpkg/info/blueman.postinst: py3compile: not found
dpkg: 处理软件包 blueman (--configure)时出错:
 子进程 已安装 post-installation 脚本 返回错误状态 127
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    正在设置 catfish (1.4.2-0ubuntu1) ...
/var/lib/dpkg/info/catfish.postinst: 6: /var/lib/dpkg/info/catfish.postinst: py3compile: not found
dpkg: 处理软件包 catfish (--configure)时出错:
 子进程 已安装 post-installation 脚本 返回错误状态 127
正在设置 python3-pyatspi (2.18.0+dfsg-3) ...
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    /var/lib/dpkg/info/python3-pyatspi.postinst: 6: /var/lib/dpkg/info/python3-pyatspi.postinst: py3compile: not found
dpkg: 处理软件包 python3-pyatspi (--configure)时出错:
 子进程 已安装 post-installation 脚本 返回错误状态 127
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    正在设置 python3-speechd (0.8.3-1ubuntu3) ...
/var/lib/dpkg/info/python3-speechd.postinst: 6: /var/lib/dpkg/info/python3-speechd.postinst: py3compile: not found
dpkg: 处理软件包 python3-speechd (--configure)时出错:
 子进程 已安装 post-installation 脚本 返回错误状态 127
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    dpkg: 依赖关系问题使得 gnome-orca 的配置工作不能继续:
 gnome-orca 依赖于 python3-pyatspi (>= 2.10);然而:
  软件包 python3-pyatspi 尚未配置。
 gnome-orca 依赖于 python3-speechd (>= 0.8);然而:
  软件包 python3-speechd 尚未配置。
 
dpkg: 处理软件包 gnome-orca (--configure)时出错:
 依赖关系问题 - 仍未被配置
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    由于已经达到 MaxReports 限制,没有写入 apport 报告。
                        dpkg: 依赖关系问题使得 gtk-theme-config 的配置工作不能继续:
 gtk-theme-config 依赖于 gconf2;然而:
  软件包 gconf2 尚未配置。
 
dpkg: 处理软件包 gtk-theme-config (--configure)时出错:
 依赖关系问题 - 仍未被配置
dpkg: 依赖关系问题使得 libgnomevfs2-common 的配置工作不能继续:
 libgnomevfs2-common 依赖于 gconf2 (>= 2.28.1-2);然而:
  软件包 gconf2 尚未配置。
 
dpkg: 处理软件包 libgnomevfs2-common (--configure)时出错:
 依赖关系问题 - 仍未被配置
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    dpkg: 依赖关系问题使得 libgnomevfs2-0:amd64 的配置工作不能继续:
 libgnomevfs2-0:amd64 依赖于 libgnomevfs2-common (= 1:2.24.4-6.1ubuntu1);然而:
  软件包 libgnomevfs2-common 尚未配置。
 
dpkg: 处理软件包 libgnomevfs2-0:amd64 (--configure)时出错:
 依赖关系问题 - 仍未被配置
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    dpkg: 依赖关系问题使得 libgnome2-common 的配置工作不能继续:
 libgnome2-common 依赖于 gconf2 (>= 2.28.1-2);然而:
  软件包 gconf2 尚未配置。
 
dpkg: 处理软件包 libgnome2-common (--configure)时出错:
 依赖关系问题 - 仍未被配置
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    dpkg: 依赖关系问题使得 libgnome-2-0:amd64 的配置工作不能继续:
 libgnome-2-0:amd64 依赖于 libgnomevfs2-0 (>= 1:2.17.90);然而:
  软件包 libgnomevfs2-0:amd64 尚未配置。
 libgnome-2-0:amd64 依赖于 libgnome2-common (= 2.32.1-5ubuntu1);然而:
  软件包 libgnome2-common 尚未配置。
 
dpkg: 处理软件包 libgnome-2-0:amd64 (--configure)时出错:
 依赖关系问题 - 仍未被配置
dpkg: 依赖关系问题使得 libgnome2-bin 的配置工作不能继续:
 libgnome2-bin 依赖于 libgnome-2-0;然而:
  软件包 libgnome-2-0:amd64 尚未配置。
 
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    由于已经达到 MaxReports 限制,没有写入 apport 报告。
                        dpkg: 处理软件包 libgnome2-bin (--configure)时出错:
 依赖关系问题 - 仍未被配置
dpkg: 依赖关系问题使得 libgnome2-0:amd64 的配置工作不能继续:
 libgnome2-0:amd64 依赖于 libgnome-2-0 (= 2.32.1-5ubuntu1);然而:
  软件包 libgnome-2-0:amd64 尚未配置。
 libgnome2-0:amd64 依赖于 libgnome2-bin (= 2.32.1-5ubuntu1);然而:
  软件包 libgnome2-bin 尚未配置。
 
dpkg: 处理软件包 libgnome2-0:amd64 (--configure)时出错:
 依赖关系问题 - 仍未被配置
dpkg: 依赖关系问题使得 libbonoboui2-0:amd64 的配置工作不能继续:
 libbonoboui2-0:amd64 依赖于 libgnome2-0 (>= 2.17.3);然而:
  软件包 libgnome2-0:amd64 尚未配置。
 
dpkg: 处理软件包 libbonoboui2-0:amd64 (--configure)时出错:
 依赖关系问题 - 仍未被配置
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    由于已经达到 MaxReports 限制,没有写入 apport 报告。
                        dpkg: 依赖关系问题使得 libgnomeui-0:amd64 的配置工作不能继续:
 libgnomeui-0:amd64 依赖于 libbonoboui2-0 (>= 2.15.1);然而:
  软件包 libbonoboui2-0:amd64 尚未配置。
 libgnomeui-0:amd64 依赖于 libgnome-2-0;然而:
  软件包 libgnome-2-0:amd64 尚未配置。
 libgnomeui-0:amd64 依赖于 libgnomevfs2-0 (>= 1:2.17.90);然而:
  软件包 libgnomevfs2-0:amd64 尚未配置。
 
dpkg: 处理软件包 libgnomeui-0:amd64 (--configure)时出错:
 依赖关系问题 - 仍未被配置
正在设置 lightdm-gtk-greeter-settings (1.2.1-0ubuntu1) ...
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    /var/lib/dpkg/info/lightdm-gtk-greeter-settings.postinst: 6: /var/lib/dpkg/info/lightdm-gtk-greeter-settings.postinst: py3compile: not found
dpkg: 处理软件包 lightdm-gtk-greeter-settings (--configure)时出错:
 子进程 已安装 post-installation 脚本 返回错误状态 127
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    正在设置 python3-psutil (3.4.2-1) ...
/var/lib/dpkg/info/python3-psutil.postinst: 6: /var/lib/dpkg/info/python3-psutil.postinst: py3compile: not found
dpkg: 处理软件包 python3-psutil (--configure)时出错:
 子进程 已安装 post-installation 脚本 返回错误状态 127
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    dpkg: 依赖关系问题使得 menulibre 的配置工作不能继续:
 menulibre 依赖于 python3-psutil;然而:
  软件包 python3-psutil 尚未配置。
 
dpkg: 处理软件包 menulibre (--configure)时出错:
 依赖关系问题 - 仍未被配置
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    正在设置 mugshot (0.3.1-0ubuntu1) ...
/var/lib/dpkg/info/mugshot.postinst: 6: /var/lib/dpkg/info/mugshot.postinst: py3compile: not found
dpkg: 处理软件包 mugshot (--configure)时出错:
 子进程 已安装 post-installation 脚本 返回错误状态 127
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    dpkg: 依赖关系问题使得 nautilus-share 的配置工作不能继续:
 nautilus-share 依赖于 apturl;然而:
  软件包 apturl 尚未配置。
 
dpkg: 处理软件包 nautilus-share (--configure)时出错:
 依赖关系问题 - 仍未被配置
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    正在设置 onboard (1.2.0-0ubuntu5) ...
/var/lib/dpkg/info/onboard.postinst: 6: /var/lib/dpkg/info/onboard.postinst: py3compile: not found
dpkg: 处理软件包 onboard (--configure)时出错:
 子进程 已安装 post-installation 脚本 返回错误状态 127
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    dpkg: 依赖关系问题使得 onboard-data 的配置工作不能继续:
 onboard-data 依赖于 onboard (>= 0.99.0~alpha1~tr1507);然而:
  软件包 onboard 尚未配置。
 
dpkg: 处理软件包 onboard-data (--configure)时出错:
 依赖关系问题 - 仍未被配置
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    正在设置 python3-feedparser (5.1.3-3build1) ...
/var/lib/dpkg/info/python3-feedparser.postinst: 6: /var/lib/dpkg/info/python3-feedparser.postinst: py3compile: not found
dpkg: 处理软件包 python3-feedparser (--configure)时出错:
 子进程 已安装 post-installation 脚本 返回错误状态 127
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    正在设置 python3-mako (1.0.3+ds1-1ubuntu1) ...
/var/lib/dpkg/info/python3-mako.postinst: 6: /var/lib/dpkg/info/python3-mako.postinst: py3compile: not found
dpkg: 处理软件包 python3-mako (--configure)时出错:
 子进程 已安装 post-installation 脚本 返回错误状态 127
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    正在设置 rhythmbox (3.3-1ubuntu7) ...
/var/lib/dpkg/info/rhythmbox.postinst: 6: /var/lib/dpkg/info/rhythmbox.postinst: py3compile: not found
dpkg: 处理软件包 rhythmbox (--configure)时出错:
 子进程 已安装 post-installation 脚本 返回错误状态 127
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    正在设置 totem-plugins (3.18.1-1ubuntu4) ...
/var/lib/dpkg/info/totem-plugins.postinst: 6: /var/lib/dpkg/info/totem-plugins.postinst: py3compile: not found
dpkg: 处理软件包 totem-plugins (--configure)时出错:
 子进程 已安装 post-installation 脚本 返回错误状态 127
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    dpkg: 依赖关系问题使得 unity-scope-openclipart 的配置工作不能继续:
 unity-scope-openclipart 依赖于 python3-feedparser;然而:
  软件包 python3-feedparser 尚未配置。
 
dpkg: 处理软件包 unity-scope-openclipart (--configure)时出错:
 依赖关系问题 - 仍未被配置
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    正在设置 unity-tweak-tool (0.0.7ubuntu2) ...
/var/lib/dpkg/info/unity-tweak-tool.postinst: 6: /var/lib/dpkg/info/unity-tweak-tool.postinst: py3compile: not found
dpkg: 处理软件包 unity-tweak-tool (--configure)时出错:
 子进程 已安装 post-installation 脚本 返回错误状态 127
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    正在设置 xfpanel-switch (1.0.4-0ubuntu1) ...
/var/lib/dpkg/info/xfpanel-switch.postinst: 6: /var/lib/dpkg/info/xfpanel-switch.postinst: py3compile: not found
dpkg: 处理软件包 xfpanel-switch (--configure)时出错:
 子进程 已安装 post-installation 脚本 返回错误状态 127
由于已经达到 MaxReports 限制,没有写入 apport 报告。
                                                    正在设置 ssh-import-id (5.5-0ubuntu1) ...
/var/lib/dpkg/info/ssh-import-id.postinst: 14: /var/lib/dpkg/info/ssh-import-id.postinst: py3compile: not found
dpkg: 处理软件包 ssh-import-id (--configure)时出错:
 子进程 已安装 post-installation 脚本 返回错误状态 127
由于已经达到 MaxReports 限制,没有写入 apport 报告。

 

解决方法:

sudo mv /var/lib/dpkg/info /var/lib/dpkg/info_old //现将info文件夹更名
 
sudo mkdir /var/lib/dpkg/info //再新建一个新的info文件夹
sudo apt-get update && apt-get -f install //不用解释了吧
sudo mv /var/lib/dpkg/info/* /var/lib/dpkg/info_old //执行完上一步操作后会在新的info文件夹下生成一些文件,现将这些文件全部移到info_old文件夹下
sudo rm -rf /var/lib/dpkg/info //把自己新建的info文件夹删掉
sudo mv /var/lib/dpkg/info_old /var/lib/dpkg/info //把以前的info文件夹重新改

 

子进程 已安装 pre-removal 脚本 返回了错误号 1或2 与 子进程 已安装 post-installation 脚本 返回了错误号 1或2

一、子进程 已安装 pre-removal 脚本 返回了错误号 1或2 解决办法:

sudo vim /var/lib/dpkg/status

找到Package: <package name>,比如Package: vsftpd

删除这部分的描述,删到下个Package: XXX前就好。存档后,再进行操作就OK了。

二、子进程 已安装 post-installation 脚本 返回了错误号 1或2 解决办法:

sudo rm /var/lib/dpkg/info/<package name>.*

比如sudo rm /var/lib/dpkg/info/vsftpd.*,存档后,再进行操作就OK了。