Install and start using igraph in Python
igraph is on the Python
Package Index with pre-compiled wheels for most Python distributions
and platforms, so in most cases it can simply be installed using
pip
:
$ pip install igraph
The command above should attempt to download a pre-compiled wheel if
your platform and Python version are among the supported ones. Currently
we aim to support Windows, Linux and macOS for at least the three most
recent minor versions of Python 3.x. If you are still using Python 2.7,
python-igraph
0.8.x is the latest version that you should
attempt to install.
In cases where we don't provide a pre-compiled wheel, the command
above will attempt to compile the Python interface of igraph during
installation. See the next section about the requirements for compiling
igraph
from source.
On Windows, you might also need the
MSVC redistributable library
if you are getting import errors after installing igraph. These are
typically bundled with Python so there is no need to download them in
advance, only if you get DLL import errors after running
import igraph
from Python.
In general, you will need a working C and C++ compiler, a POSIX-compatible
shell environment (MSYS2 on Windows
is okay), make
, and the development header files for your
version of Python. You will also need libxml2
for GraphML support. libxml2
might have additional
dependencies depending on your platform; for instance, you also
need zlib1g-dev
on Linux; more about these in the
platform-specific instructions below.
The source code of the Python interface bundles a matching version of the C core of the igraph library, and this will be linked to the compiled Python interface statically during the build process, so if everything is installed properly, you can simply run the following command even if we provide no matching pre-compiled wheel:
$ pip install igraph
However, if the C core of igraph has already been installed to a
location that can be discovered using pkg-config
,
you can also instruct the setup script to use pkg-config
instead of compiling the bundled version with
pip
will compile and link the extension to the
C core that you have installed. First you need to download and extract
the source code of the Python interface:
$ pip download --no-deps --no-binary :all: igraph
$ tar -xvvzf igraph-0.9.10.tar.gz
$ cd igraph-0.9.10
Next, make sure that pkg-config
can correctly report
the compilation flags needed to link to igraph
; the
following command should list a bunch of compiler and linker flags
instead of reporting an error:
$ pkg-config --cflags --libs igraph
Finally, ask the setup script to compile igraph
and use pkg-config
for the detection of the C core:
$ python setup.py build --use-pkg-config
$ python setup.py install
See the sections below for additional, platform-specific instructions.
On Debian and Ubuntu Linux, installing build-essential
,
python-dev
(or python3.X-dev
for your
matching version of Python), libxml2-dev
and
zlib1g-dev
should be enough to get you started. The
latter two are needed only if you need GraphML support. Running
pip install --user igraph
after having installed
all these dependencies should then work out-of-the-box.
If you are using some other Linux distribution, try to find the
packages equivalent to the ones mentioned above; in general, you will
need the usual development tools (at least a C and C++ compiler
and make
), the development headers for your Python
version, and the development headers for libxml2
if you
need GraphML support. Feel free to send us a message in the
discussion forum if you
can provide more specific instructions for a popular Linux flavour
that we have not listed here.
On macOS, you can get a working C and C++ compiler as well as
make
by installing XCode or the XCode Command Line Tools.
Running pip install igraph
should then work
out-of-the-box.
You can also install a pre-compiled version of the C core of igraph from
Homebrew. This can be used in conjunction
with pkg-config
as outlined in the section about
compiling from source to link the Python interface
to the C core installed from Homebrew.
Compilation on Windows is more complicated than on other platforms because the C core of igraph needs to be built in a POSIX shell-like environment at the moment, but the Python interface has to be compiled with Microsoft Visual C instead (like all other Python extensions). Ideally, you should be able to use one of the pre-compiled wheels, but in case you can't, here is a rough outline of how we managed to get it work in our continuous integration environment. As we do not use Windows regularly, these instructions might need small tweaks; any improvements are appreciated in our discussion forum.
First, you need to follow the instructions on the Python wiki to install the version of the Microsoft Visual C++ Compiler that matches your Python version.
Next, you also need to download MSYS2, which provides a shell environment in which the C core of igraph can be compiled.
Then you need to install a bunch of dependencies in MSYS2. Open up the MSYS2 shell and run the following command:
$ pacman --needed --noconfirm -Sy pacman-mirrors
$ pacman --noconfirm -Sy
$ pacman --noconfirm -S autoconf automake bison flex \
libtool mingw-w64-x86_64-libtool zip \
mingw-w64-i686-libxml2 mingw-w64-x86_64-libxml2
Then you need to download the source code of the Python
interface of igraph
from
PyPI and extract it
to a folder. Open the MSYS2 shell, enter this
folder and run python setup.py build_c_core
. This should
compile the C core of the Python interface with MSYS2.
You are not done yet, read on.
Once the C core is compiled, you need to switch back to the "regular"
WIndows shell, make sure that the Visual C compiler is on your PATH
(typically there are scripts in the Visual Studio installation that
let you launch a development shell with MSVC on your PATH), and then
run python setup.py build
, followed by
python setup.py install
.
Note that the instructions above work in our CI environment, but your environment might be different from ours so these instructions might require small tweaks. Improvements are welcome; please open a thread in our discussion forum if you can add more specific instructions.
Documentation for the Python interface is also included in
the Python docstrings, so you can use pydoc
or
the built-in Python help facility as well.
Clone (or fork) the develop
branch from the
python-igraph git
repository on GitHub.
Post your question on the igraph support forum . Make sure that you include a minimal reproducible example, complete with code and data.
Post specific igraph questions on
Stack Overflow. Make sure that you include the
igraph
tag and that you include a reproducible
example, complete with code and data.
Report bugs or suggest new features or algorithms in the GitHub issue tracker.
Your contribution is more than welcome!
Send a pull request on GitHub. Please note that igraph can contain only code that is compatible with its GPL license.