How to install gi module for anaconda python3?

31,351

Solution 1

This is how you do it: (example for Linux Mint and python3)

First install gi module using your distro package manager. For Linux Mint it would be:

sudo apt-get install python3-gi

Then run your distro python to check where the module is located:

$ /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'>

So in my case the module gi was installed to /usr/lib/python3/dist-packages/gi. Assuming you have your anaconda installed in /home/kossak/anaconda3, create a link to gi module in the proper folder:

ln -s /usr/lib/python3/dist-packages/gi /home/kossak/anaconda3/lib/python3.5/site-packages/

If you have conda virtual environment and want gi module to be available there, the path should be a bit different. Assuming the virtual env is called TEST:

ln -s /usr/lib/python3/dist-packages/gi /home/kossak/anaconda3/envs/TEST/lib/python3.5/site-packages/

and it works:

$ 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
>>>

Solution 2

If you're using conda virtualenv for python-3, you can use

$ conda install -c conda-forge pygobject

in your virtualenv

You can read more about this on: https://anaconda.org/conda-forge/pygobject

Solution 3

If you want to perform a proper install (without soft linking) inside a Linux anaconda environment. Keep in mind that the errors may vary if you have not installed gcc previously (I assumed it was installed by default when I posted, however not everyone does so). Install it if you don't know very well what you're doing to avoid missunderstandings

  1. Create or open your conda environment.

  2. Attemp to install pygobject (don't worry, it will most likely throw an error):

    pip install pygobject
    

    In linux, it will promp the usual installation progress followed by an error:

    (...) Please, try executing the following in your system: sudo apt install libgirepository1.0-dev

    Depending on your operation sistem or installed dependencies, the command name or package name may vary. Just follow the instructions and allow the system to install your packets. This step doesn't change anything, is just to give you the precise info of the package you need on your system. If you run this on Windows, it will ask you to install a specific version of Visual Studio. If you are in Windows, download the required Visual Studio from MS website, install it, reboot your computer and go to step 5 (in my case I never needed step 4 in windows, however, I'm not a MS expert.

  3. Close your conda environment

    conda deactivate
    
  4. Next you need to install pygobject from conda-forge repository in your conda environment. You can add the repo to your favourite conda package manager or simply run the following command as root (it is important to be root). I did it outside the project, but you may do it inside:

    conda install -c conda-forge pygobject
    

    In my case conda was not in the path. I had is installed in:

    /opt/anaconda3/bin/
    

    You can run the following command from your normal user to find out where conda is:

    which conda
    
  5. Open the conda environment

    source activate <your env name>
    

    or the corresponding anaconda activate syntax (I never use it so I cant remember precisely)

  6. Repeat the first step and now the installation wont fail:

    pip install pygobject
    

    OR if you specifically want to install gi you can run:

    pip install pgi
    

    the correct package is "pgi" NOT "gi"

As gi is a dependency of pygobject, everything will get properly installed. You can check it by running

python
>>> import gi

You may find the following usefull for Windows, although you may need to work it out a bit: GStreammer python bindings on Windows

Solution 4

for me

conda install -c pkgw/label/superseded gtk3

worked

Share:
31,351
Kossak
Author by

Kossak

PC gamer, Linux user Steam - Twitter

Updated on May 21, 2020

Comments

  • Kossak
    Kossak almost 4 years

    python3 is my local Anaconda version of python, while python3.4 is the system one. I can import gi module with python3.4 (probably because i installed it with sudo apt-get install python3-gi) but python3 doesn't see it:

    $ 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
    $
    

    How should i install gi for Anaconda python? Or maybe i can somehow import sysem-wide modules?

    My os:

    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