NetworkX requires Python 2.7, 3.5, 3.6, or 3.7. If you do not already have a Python environment configured on your computer, please see the instructions for installing the full scientific Python stack.
If you are on Windows and want to install optional packages (e.g.,
then you will need to install a Python distribution such as
If you use one of these Python distribution, please refer to their online
Below we assume you have the default Python environment already configured on
your computer and you intend to install
networkx inside of it. If you want
to create and work with Python virtual environments, please follow instructions
on venv and virtual
First, make sure you have the latest version of
pip (the Python package manager)
installed. If you do not, refer to the Pip documentation and install
Install the released version¶
Install the current release of
$ pip install networkx
To upgrade to a newer release use the
$ pip install --upgrade networkx
If you do not have permission to install software systemwide, you can
install into your user directory using the
$ pip install --user networkx
$ pip install .
Install the development version¶
If you have Git installed on your system, it is also
possible to install the development version of
Before installing the development version, you may need to uninstall the
standard version of
$ pip uninstall networkx
$ git clone https://github.com/networkx/networkx.git $ cd networkx $ pip install -e .
pip install -e . command allows you to follow the development branch as
it changes by creating links in the right places and installing the command
line scripts to the appropriate locations.
Then, if you want to update
networkx at any time, in the same directory do:
$ git pull
Some optional packages (e.g.,
gdal) may require compiling
C or C++ code. If you have difficulty installing these packages
pip, please review the instructions for installing
the full scientific Python stack.
The following optional packages provide additional functionality.
- NumPy (>= 1.15.0) provides matrix representation of graphs and is used in some graph algorithms for high-performance matrix computations.
- SciPy (>= 1.1.0) provides sparse matrix representation of graphs and many numerical scientific tools.
- pandas (>= 0.23.3) provides a DataFrame, which is a tabular data structure with labeled axes.
- Matplotlib (>= 2.2.2) provides flexible drawing of graphs.
- PyGraphviz and pydot (>= 1.2.4) provide graph drawing and graph layout algorithms via GraphViz.
- PyYAML provides YAML format reading and writing.
- gdal provides shapefile format reading and writing.
- lxml used for GraphML XML format.
networkx and all optional packages, do:
$ pip install networkx[all]
To explicitly install all optional packages, do:
$ pip install numpy scipy pandas matplotlib pygraphviz pydot pyyaml gdal
Or, install any optional package (e.g.,
$ pip install numpy
NetworkX uses the Python
nose testing package. If you don’t already have
that package installed, follow the directions on the nose homepage.
Test a source distribution¶
You can test the complete package from the unpacked source directory with:
nosetests networkx -v
Test an installed package¶
If you have a file-based (not a Python egg) installation you can test the installed package with:
>>> import networkx as nx >>> nx.test()
python -c "import networkx as nx; nx.test()"
test(verbosity=1, doctest=False, numpy=True)¶
Run NetworkX tests.
- verbosity (integer, optional) – Level of detail in test reports. Higher numbers provide more detail.
- doctest (bool, optional) – True to run doctests in code modules
- numpy (bool, optional) – True to test modules dependent on numpy
Testing for developers¶
You can test any or all of NetworkX by using the
nosetests test runner.
First make sure the NetworkX version you want to test is in your
(either installed or pointing to your unpacked source directory).
Then you can run individual test files with:
or all tests found in dir and an directories contained in dir:
By default nosetests does not test docutils style tests in Python modules but you can turn that on with:
For doctests in stand-alone files NetworkX uses the extension
you can add:
nosetests --with-doctest --doctest-extension=txt
to also execute those tests.
These options are on by default if you run nosetests from the root of the
NetworkX distribution since they are specified in the
setup.cfg file found