在 Ubuntu 18.04.02 服务器版中设置稳定的 Docker Engine 存储库时:无法从“gi”导入名称“_gi”

在 Ubuntu 18.04.02 服务器版中设置稳定的 Docker Engine 存储库时:无法从“gi”导入名称“_gi”

按照此处的指示:https://docs.docker.com/install/linux/docker-ce/ubuntu/#install-using-the-repository 我正在尝试在 Ubuntu 18.04.02 服务器版中安装 Docker Engine。

第一次安装的步骤一切顺利,但我遇到了这个错误消息:

(base) marco@pc:~$ sudo add-apt-repository \ 
> "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
> $(lsb_release -cs) \
> stable"
Traceback (most recent call last):
  File "/usr/bin/add-apt-repository", line 11, in <module>
    from softwareproperties.SoftwareProperties import SoftwareProperties, 
shortcut_handler
  File "/usr/lib/python3/dist-packages/softwareproperties
/SoftwareProperties.py", line 67, in <module>
    from gi.repository import Gio
  File "/usr/lib/python3/dist-packages/gi/__init__.py", line 42, in 
<module>
    from . import _gi
ImportError: cannot import name '_gi' from 'gi' (/usr/lib/python3/dist-
packages/gi/__init__.py)

但是在 python3 中导入 gi 可以正常工作:

(base) marco@pc:~$ python3
Python 3.7.3 (default, Mar 27 2019, 22:11:17) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
>>> 

更新:

1)将 /usr/bin/add-apt-repository 的第一行修改为 #!/usr/bin/python3.7.3

(base) marco@pc:~$ sudo add-apt-repository \
>    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
>    $(lsb_release -cs) \
>    stable"
sudo: unable to execute /usr/bin/add-apt-repository: No such file or 
directory

2)我按照此处的指示安装了python 3.7.3:

https://linoxide.com/linux-how-to/install-python-ubuntu/和这里:https://www.how2shout.com/how-to/install-python-on-ubuntu-19-04-18-04-using-command-terminal.html

(base) marco@pc:~$ ls -al /usr/bin | grep python
lrwxrwxrwx  1 root   root          26 Mar 26  2018 dh_pypy -> ../share/dh-
python/dh_pypy
-rwxr-xr-x  1 root   root        1056 Apr 16  2018 dh_python2
lrwxrwxrwx  1 root   root          29 Mar 26  2018 dh_python3 -> ../share
/dh-python/dh_python3
lrwxrwxrwx  1 root   root          23 Oct  7 19:39 pdb2.7 -> ../lib
/python2.7/pdb.py
lrwxrwxrwx  1 root   root          23 Oct  7 14:59 pdb3.6 -> ../lib
/python3.6/pdb.py
lrwxrwxrwx  1 root   root          23 Apr  3  2019 pdb3.7 -> ../lib
/python3.7/pdb.py
lrwxrwxrwx  1 root   root          31 Jul 30 19:24 py3versions -> ../share
/python3/py3versions.py
lrwxrwxrwx  1 root   root          26 Mar 26  2018 pybuild -> ../share/dh-
python/pybuild
lrwxrwxrwx  1 root   root           9 Apr 16  2018 python -> python2.7
lrwxrwxrwx  1 root   root          16 Apr 16  2018 python-config -> 
python2.7-config
lrwxrwxrwx  1 root   root           9 Apr 16  2018 python2 -> python2.7
lrwxrwxrwx  1 root   root          16 Apr 16  2018 python2-config -> 
python2.7-config
-rwxr-xr-x  1 root   root         399 Nov 15  2017 python2-jsonschema
-rwxr-xr-x  1 root   root         154 Nov 11  2017 python2-pbr
-rwxr-xr-x  1 root   root        5901 Jul 27  2017 python2-wsdump
-rwxr-xr-x  1 root   root     3641704 Oct  7 19:39 python2.7
lrwxrwxrwx  1 root   root          33 Oct  7 19:39 python2.7-config -> 
x86_64-linux-gnu-python2.7-config
lrwxrwxrwx  1 root   root          25 Aug 11 10:46 python3 -> 
/etc/alternatives/python3
lrwxrwxrwx  1 root   root          16 Oct 25  2018 python3-config -> 
python3.6-config
-rwxr-xr-x  1 root   root         384 Feb  5  2018 python3-futurize
-rwxr-xr-x  1 root   root         388 Feb  5  2018 python3-pasteurize
-rwxr-xr-x  1 root   root         152 Nov 11  2017 python3-pbr
-rwxr-xr-x  2 root   root     4526456 Oct  7 14:59 python3.6
lrwxrwxrwx  1 root   root          33 Oct  7 14:59 python3.6-config -> 
x86_64-linux-gnu-python3.6-config
-rwxr-xr-x  2 root   root     4526456 Oct  7 14:59 python3.6m
lrwxrwxrwx  1 root   root          34 Oct  7 14:59 python3.6m-config -> 
x86_64-linux-gnu-python3.6m-config
-rwxr-xr-x  2 root   root     4906512 Apr  3  2019 python3.7
-rwxr-xr-x  2 root   root     4906512 Apr  3  2019 python3.7m
lrwxrwxrwx  1 root   root          10 Jul 30 19:24 python3m -> python3.6m
lrwxrwxrwx  1 root   root          17 Oct 25  2018 python3m-config -> 
python3.6m-config
lrwxrwxrwx  1 root   root          29 Apr 16  2018 pyversions -> ../share
/python/pyversions.py
lrwxrwxrwx  1 root   root          33 Apr 16  2018 x86_64-linux-gnu-
python-config -> x86_64-linux-gnu-python2.7-config
-rwxr-xr-x  1 root   root        2971 Oct  7 19:39 x86_64-linux-gnu-
python2.7-config
lrwxrwxrwx  1 root   root          33 Oct 25  2018 x86_64-linux-gnu-
python3-config -> x86_64-linux-gnu-python3.6-config
lrwxrwxrwx  1 root   root          34 Oct  7 14:59 x86_64-linux-gnu-
python3.6-config -> x86_64-linux-gnu-python3.6m-config
-rwxr-xr-x  1 root   root        3283 Oct  7 14:59 x86_64-linux-gnu-
python3.6m-config
lrwxrwxrwx  1 root   root          34 Oct 25  2018 x86_64-linux-gnu-
python3m-config -> x86_64-linux-gnu-python3.6m-config

(base) marco@pc:~$ which python3
/home/marco/anaconda3/bin/python3

(base) marco@pc:~$ python --version
Python 3.7.3

3)我修改了/usr/bin/add-apt-repository的第一行:

(base) marco@pc:~$ head /usr/bin/add-apt-repository
#!/usr/bin/python3.7

from __future__ import print_function

再次尝试设置稳定的 Docker-Engine 存储库:

(base) marco@pc:~$ sudo add-apt-repository \
>    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
>    $(lsb_release -cs) \
>    stable"
Traceback (most recent call last):
  File "/usr/bin/add-apt-repository", line 11, in <module>
    from softwareproperties.SoftwareProperties import SoftwareProperties, 
shortcut_handler
  File "/usr/lib/python3/dist-packages/softwareproperties
/SoftwareProperties.py", line 67, in <module>
    from gi.repository import Gio
  File "/usr/lib/python3/dist-packages/gi/__init__.py", line 42, in 
<module>
    from . import _gi
ImportError: cannot import name '_gi' from 'gi' (/usr/lib/python3/dist-
packages/gi/__init__.py)
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 109,
in apport_excepthook
    pr.add_proc_info(extraenv=['PYTHONPATH', 'PYTHONHOME'])
  File "/usr/lib/python3/dist-packages/apport/report.py", line 543, in 
add_proc_info
    self.add_proc_environ(pid, extraenv)
  File "/usr/lib/python3/dist-packages/apport/report.py", line 610, in 
add_proc_environ
    env = _read_file('environ', dir_fd=proc_pid_fd).replace('\n', '\\n')
  File "/usr/lib/python3/dist-packages/apport/report.py", line 73, in 
_read_file
    with open(path, 'rb', opener=lambda path, mode: os.open(path, mode, 
dir_fd=dir_fd)) as fd:
  File "/usr/lib/python3/dist-packages/apport/report.py", line 73, in 
<lambda>
    with open(path, 'rb', opener=lambda path, mode: os.open(path, mode, 
dir_fd=dir_fd)) as fd:
TypeError: argument should be integer or None, not list

Original exception was:
Traceback (most recent call last):
  File "/usr/bin/add-apt-repository", line 11, in <module>
    from softwareproperties.SoftwareProperties import SoftwareProperties, 
shortcut_handler
  File "/usr/lib/python3/dist-packages/softwareproperties
/SoftwareProperties.py", line 67, in <module>
    from gi.repository import Gio
  File "/usr/lib/python3/dist-packages/gi/__init__.py", line 42, in 
<module>
    from . import _gi
ImportError: cannot import name '_gi' from 'gi' (/usr/lib/python3/dist-
packages/gi/__init__.py)

并尝试更新python替代方案:

(base) marco@pc:~$ update-alternatives --list python || update-
alternatives --list python
update-alternatives: error: no alternatives for python
update-alternatives: error: no alternatives for python

按照此处的指示:https://www.how2shout.com/how-to/install-python-on-ubuntu-19-04-18-04-using-command-terminal.html

我也尝试过用这种方法修复python3.7.3为官方python:

(base) marco@pc:~$ sudo update-alternatives --config python3.7.3
update-alternatives: error: no alternatives for python3.7.3

解决了:

感谢@Smurfz87,我发现 python3.7.3 版本会导致 apt-repository 出现此问题。将 /usr/bin/add-apt-repository 的第一行更改为指向 3.6 版本后:#!/usr/bin/python3.6 一切正常:

(base) marco@pc:~$ sudo add-apt-repository \
>    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
>    $(lsb_release -cs) \
>    stable"
Hit:1 http://gb.archive.ubuntu.com/ubuntu bionic InRelease
Get:2 https://download.docker.com/linux/ubuntu bionic InRelease [64.4 kB]                                                            
Get:3 http://gb.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 
kB]                                                         
Get:4 http://gb.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6
 kB]
Get:5 https://download.docker.com/linux/ubuntu bionic/stable amd64    
Packages [9594 B]
Get:6 http://gb.archive.ubuntu.com/ubuntu bionic-updates/universe i386 
Packages [985 kB]  
Get:7 http://gb.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 
Packages [1017 kB]
Hit:8 http://apt.postgresql.org/pub/repos/apt bionic-pgdg InRelease                                                                                                                                        
Hit:9 http://ppa.launchpad.net/certbot/certbot/ubuntu bionic InRelease                                                                                                                                     
Get:10 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 
kB]                                                                                                                               
Fetched 2328 kB in 11s (217 kB/s)                                                                                                                                                                          
Reading package lists... Done

(base) marco@pc:~$ sudo apt-get update
Hit:1 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:2 http://apt.postgresql.org/pub/repos/apt bionic-pgdg InRelease
Hit:3 http://gb.archive.ubuntu.com/ubuntu bionic InRelease
Hit:4 http://gb.archive.ubuntu.com/ubuntu bionic-updates InRelease               
Hit:5 http://gb.archive.ubuntu.com/ubuntu bionic-backports InRelease             
Hit:6 http://ppa.launchpad.net/certbot/certbot/ubuntu bionic InRelease
Hit:7 https://download.docker.com/linux/ubuntu bionic InRelease
Reading package lists... Done

(base) marco@pc:~$ sudo apt-get install docker-ce docker-ce-cli 
containerd.io
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  aufs-tools cgroupfs-mount pigz
The following NEW packages will be installed
  aufs-tools cgroupfs-mount containerd.io docker-ce docker-ce-cli pigz
0 to upgrade, 6 to newly install, 0 to remove and 0 not to upgrade.
Need to get 85.6 MB of archives.
After this operation, 384 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y

马可

答案1

听起来您的系统可能在选择正确的 Python 版本时遇到了问题。从对话框中我们确定您有一个可用的 Python 3.6 版本。

尝试编辑文件/usr/bin/add-apt-repository并将该行更改#!/usr/bin/python3为特定版本的 Python,以适应您的情况:

#!/usr/bin/python3.6

相关内容