ExA15 Ubuntu下p4vasp与Python3不兼容的解决办法

伴随着Python2的落幕,Python3的兴起,很多基于2的软件也面临着着要么被遗忘,要么主动拥抱Python3的选择。其中就有我们最喜欢的p4vasp。https://github.com/orest-d/p4vasp 或者www.p4vasp.at。本人不认识作者,也不知道啥时候出新的版本。使用p4vasp需要有python2,但其他软件又需要python3。为此我们不得不做出一些选择, 最残忍的莫过于弃用p4vasp啦。本节介绍两个办法:

1) 修改p4vasp运行默认的python版本

2)通过update-alternatives切换系统默认的python版本。

办法-1

这个方法是公众号的牛牛(刘科)留言提出来的:

第一步: 修改p4v脚本:将最后一行中的python 改成python2

1
2
3
4
5
6
7
qli@lhz:~$ sudo gedit /usr/bin/p4v 
qli@lhz:~$ cat /usr/bin/p4v
#!/bin/sh
export LD_PRELOAD=libstdc++.so.6
export UBUNTU_MENUPROXY=0
export P4VASP_HOME=/usr/share/p4vasp
exec /usr/bin/python2 /usr/share/p4vasp/p4v.py "$@"

第二步 修改p4v.py, 将第一行中的python 改成python2

1
2
3
4
qli@lhz:~$ sudo gedit /usr/share/p4vasp/p4v.py
qli@lhz:~$ head -n 2 /usr/share/p4vasp/p4v.py
#!/usr/bin/env python2
# Copyright (C) 2003 Orest Dubay <orest.dubay@univie.ac.at>

运行实例:

1
2
3
4
5
6
7
qli@lhz:~$ python  --version 
Python 3.7.3
qli@lhz:~$ p4v
Gtk-Message: 08:12:51.061: Failed to load module "canberra-gtk-module"
(p4v.py:27951): libglade-WARNING **: 08:12:51.176: Could not load support for `gnome': libgnome.so: cannot open shared object file: No such file or directory
(p4v.py:27951): libglade-WARNING **: 08:12:51.519: unknown attribute `swapped' for <signal>.
Exception TypeError: "'NoneType' object is not callable" in <object repr() failed> ignored

办法-2

百度或者google里面搜一下:update-alternatives python 就会得到一系列的操作教程。本文不再瞎扯。

第一步

查看自己系统中python2和python3的版本,终端里面输入:python然后摁tab键,显示如下,本人安装了python2.7 和python3.7。

1
2
3
qli@lhz:~$ 
qli@lhz:~$ python
python python2.7-config python3 python3.7m python3-jsonschema python3-pbr pythontex python2 python2-config python3.7 python3.7m-config python3m python3-unit2 pythontex3 python2.7 python2-jsonschema python3.7-config python3-config python3m-config python-config

第二步

设置update-alternatives:在终端里面,依次输入下面两个命令行:

1
qli@lhz:~$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
1
qli@lhz:~$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 2

这样就设置OK啦。更加具体的,网上已经泛滥了

第三步

切换python版本,使用下面的命令:

1
qli@lhz:~$ sudo update-alternatives --config python

下面是具体运行的例子,首先本人将上面的命令行保存到.bashrc文件中。

1
2
3
4
5
6
7
8
9
10
11
12
13
qli@lhz:~$ grep pversion .bashrc
alias pversion='sudo update-alternatives --config python'
qli@lhz:~$
qli@lhz:~$ python --version
Python 2.7.16
qli@lhz:~$ p4v
Gtk-Message: 08:03:38.110: Failed to load module "canberra-gtk-module"
(p4v.py:3156): libglade-WARNING **: 08:03:38.346: Could not load support for `gnome': libgnome.so: cannot open shared object file: No such file or directory
(p4v.py:3156): libglade-WARNING **: 08:03:38.782: unknown attribute `swapped' for <signal>.
Exception TypeError: "'NoneType' object is not callable" in <object repr() failed> ignored
Exception TypeError: "'NoneType' object is not callable" in <object repr() failed> ignored
Exception TypeError: "'NoneType' object is not callable" in <object repr() failed> ignored
Exception TypeError: "'NoneType' object is not callable" in <object repr() failed> ignored

当前版本是python2.7,可以正常运行p4vasp。

1
2
3
4
5
6
7
8
9
10
11
12
qli@lhz:~$ pversion 
[sudo] password for qli:
There are 2 choices for the alternative python (providing /usr/bin/python).

Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/python2.7 2 auto mode
* 1 /usr/bin/python2.7 2 manual mode
2 /usr/bin/python3.7 1 manual mode

Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/bin/python3.7 to provide /usr/bin/python (python) in manual mode

当前的python版本,会在最左侧那一列用* 标出来。换成python3的话,输入 左侧所对应的数值就OK了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
qli@lhz:~$ python --version 
Python 3.7.3
qli@lhz:~$ p4v
File "/usr/share/p4vasp/p4v.py", line 9
can get source code from http://www.pygtk.org """
^
SyntaxError: invalid syntax
qli@lhz:~$ pversion
There are 2 choices for the alternative python (providing /usr/bin/python).

Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/python2.7 2 auto mode
1 /usr/bin/python2.7 2 manual mode
* 2 /usr/bin/python3.7 1 manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in manual mode

顺利将python切换到版本3,但p4vasp这时候就不能用了。

小结

希望p4vasp赶紧出基于python3的版本,如果不出的话,大家就先这样凑活着用吧。如果牛牛们还有更好的解决办法,可以将教程稍微整理下,发送到邮箱lqcata@gmail.com,本人会继续更新到本节当中。