python3是我本地的蟒蛇版本,而python3.4是系统版本。我可以用gi导入python3.4模块(可能是因为我用sudo apt-get install python3-gi安装了它),但python3没有看到它:

$ python3 -c 'import gi'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named 'gi'
$ python3.4 -c 'import gi'

$ python3 --version
Python 3.5.1 :: Anaconda 4.0.0 (64-bit)
$ python3.4 --version
Python 3.4.3
$ which python3
/home/kossak/anaconda3/bin/python3
$ which python3.4
/usr/bin/python3.4
$

我应该如何为蟒蛇安装gi或者我可以导入系统范围的模块?
我的操作系统:
System:    Kernel: 3.19.0-32-generic x86_64 (64 bit gcc: 4.8.2) Desktop: Cinnamon 2.8.8 (Gtk 2.24.23) dm: mdm
           Distro: Linux Mint 17.3 Rosa

最佳答案

您可以这样做:(例如LinuxMint和python3)
首先使用发行版软件包管理器安装gi模块。对于Linux Mint,它将是:

sudo apt-get install python3-gi

然后运行发行版python检查模块的位置:
$ /usr/bin/python3
Python 3.5.2 (default, Sep 10 2016, 08:21:44)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
>>> print(gi)
<module 'gi' from '/usr/lib/python3/dist-packages/gi/__init__.py'>

所以在我的例子中,模块安装到了。假设您在gi中安装了蟒蛇,请在适当的文件夹中创建指向gi模块的链接:
ln -s /usr/lib/python3/dist-packages/gi /home/kossak/anaconda3/lib/python3.5/site-packages/

如果您有Conda虚拟环境,并且希望/usr/lib/python3/dist-packages/gi模块在那里可用,那么路径应该有所不同。假设虚拟环境称为测试:
ln -s /usr/lib/python3/dist-packages/gi /home/kossak/anaconda3/envs/TEST/lib/python3.5/site-packages/

它的工作原理是:
$ python3
Python 3.5.2 |Anaconda custom (64-bit)| (default, Jul  2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
>>>

关于python - 如何为anaconda python3安装gi模块?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37526026/

10-10 16:47