At the end of this guide, you will have created a simple Python
Hello, World! program that connects to the Memgraph database and executes simple queries.
To follow this guide, you will need:
- A running Memgraph instance. If you need to set up Memgraph, take a look at the Installation guide.
- A basic understanding of graph databases and the property graph model.
- Docker installed and running. Instructions on how to setup Docker can be found on the official Docker website.
- The pymgclient driver. A Memgraph database adapter for the Python programming language. (if you are not using Docker)
NOTE: We recommend using Docker because it simplifies installing the driver dependencies. If you don't wish to use Docker, you will need to install the pymgclient driver locally.
We'll be using a Dockerized Python program to demonstrate how to connect to a running Memgraph database instance.
If you don't wish to use Docker, the steps might be slightly different, but the code is most likely the same or very similar.
Let's jump in and connect a simple program to Memgraph.
1. Create a new directory for your program, for example
/memgraph_python and position yourself in it.
2. Create a
requirements.txt file which will contain a list of items to be installed using pip. Leave it empty for the purposes of this guide.
3. Create a new file in the project root directory
/memgraph_python and name it
Dockerfile. Add the following code to it:
4. Create a directory for your source code, for example,
/src and position yourself in it.
5. To make the actual program, create a new Python script. Name it
program.py and add the following code:
6. Don't forget to change the host address in your code.
CONTAINER ID with
docker ps and use it in the following command to retrieve the address:
7. To run the application, first, you need to create a Docker image with:
8. Now, you can run the application with the following command:
You should see an output similar to the following:
For real-world examples of how to use Memgraph, we suggest you take a look at the Tutorials page. You can also browse through the Database functionalities section to get an overview of all the functionalities Memgraph offers.
Visit the Getting help page in case you run into any kind of problem or you have additional questions.