Last modified: Mar 25, 2025 By Alexander Williams

How to Install NetworkX in Python Step by Step

NetworkX is a Python library for creating and analyzing graphs. It is widely used in network analysis. This guide will help you install it easily.

Prerequisites

Before installing NetworkX, ensure you have Python installed. You can check this by running the following command in your terminal:


python --version

If Python is not installed, download it from the official website. Also, ensure you have pip, Python's package manager.

Install NetworkX Using pip

The easiest way to install NetworkX is using pip. Open your terminal or command prompt and run:


pip install networkx

This will download and install the latest version of NetworkX. Wait for the installation to complete.

Verify the Installation

To ensure NetworkX is installed correctly, open a Python shell and import it:

 
import networkx as nx
print(nx.__version__)

If no errors appear, the installation was successful. The output will show the installed version.

Install NetworkX in a Virtual Environment

Using a virtual environment is recommended to avoid conflicts. First, create and activate a virtual environment:


python -m venv myenv
source myenv/bin/activate  # On Windows: myenv\Scripts\activate

Then, install NetworkX inside the virtual environment:


pip install networkx

Common Installation Issues

Sometimes, you may encounter errors. One common issue is ModuleNotFoundError. If you see this, check our guide on how to solve ModuleNotFoundError.

Another issue could be outdated pip. Update pip using:


pip install --upgrade pip

Example: Create a Simple Graph

Once installed, test NetworkX by creating a simple graph. Here’s an example:

 
import networkx as nx

# Create an empty graph
G = nx.Graph()

# Add nodes
G.add_node(1)
G.add_node(2)

# Add an edge
G.add_edge(1, 2)

# Print graph info
print(nx.info(G))

The output will display basic graph information, confirming NetworkX is working.

Conclusion

Installing NetworkX in Python is simple with pip. Always verify the installation and use virtual environments for best practices. Now you’re ready to explore graph analysis with NetworkX!