This project demonstrates how to connect to and interact with Neo4j using Python.
- Neo4j Desktop installed and running
- Python 3.7+ installed
- A Neo4j database created in Neo4j Desktop
- Open Neo4j Desktop
- Create a new project (or use an existing one)
- Create a new database or start an existing one
- Note your database credentials:
- URI: Usually
bolt://localhost:7687(check in database details) - Username: Default is
neo4j - Password: The password you set when creating the database
- URI: Usually
Open a terminal in this directory and run:
pip install -r requirements.txtOr install directly:
pip install neo4jOpen neo4j_test.py and update the connection settings in the main() function:
URI = "bolt://localhost:7687" # Your Neo4j URI
USER = "neo4j" # Your username
PASSWORD = "your_password" # Your passwordpython neo4j_test.pyThe test script performs the following operations:
- Connection Test: Verifies that it can connect to your Neo4j database
- Create Sample Data:
- Creates 3 Person nodes (Alice, Bob, Charlie)
- Creates 2 Company nodes (ACME Corp, TechCo)
- Creates relationships (WORKS_AT, KNOWS)
- Query Data: Retrieves and displays the created data
- Count Nodes: Shows the total number of nodes in the database
=== Neo4j Connection Test ===
INFO:__main__:Connected to Neo4j at bolt://localhost:7687
INFO:__main__:✓ Connection successful!
Cleaning existing data...
INFO:__main__:✓ Cleaned up all test data
Creating sample data...
INFO:__main__:✓ Created Person nodes
INFO:__main__:✓ Created Company nodes
INFO:__main__:✓ Created relationships
Querying data...
--- All Persons ---
Alice, Age: 30
Bob, Age: 25
Charlie, Age: 35
--- Employment Relationships ---
Alice works at ACME Corp
Bob works at TechCo
--- Friendships ---
Alice knows Bob
INFO:__main__:✓ Total nodes in database: 5
=== Test completed successfully! ===
If you see connection errors:
- Verify Neo4j is running: Check Neo4j Desktop to ensure your database is started
- Check the URI: In Neo4j Desktop, click on your database and verify the Bolt URL
- Verify credentials: Make sure username and password are correct
- Check port: Default is 7687, but verify in Neo4j Desktop settings
If you see ModuleNotFoundError: No module named 'neo4j':
pip install neo4jAfter running the script, you can view the data in Neo4j Browser:
- In Neo4j Desktop, click "Open" on your database
- Run this Cypher query to see everything:
MATCH (n) RETURN nOr try these specific queries:
// View all persons
MATCH (p:Person) RETURN p
// View relationships
MATCH (p:Person)-[r]->(c:Company) RETURN p, r, c
// View the graph
MATCH (n)-[r]->(m) RETURN n, r, mThe script includes a cleanup function that removes all test data. By default, it runs at the start to ensure a clean slate. If you want to also clean up at the end, uncomment these lines in neo4j_test.py:
# logger.info("\nCleaning up...")
# neo4j_test.cleanup()- Modify the script to create your own data models
- Explore more complex Cypher queries
- Try the Neo4j Browser for visual exploration
- Check out the Neo4j Python Driver documentation