Module 2: Set up your environment¶
Learning objectives
By the end of this module, you can:
- O1. Install ddigraph with pip, with the extras this course uses, or from a clone with uv.
- O2. Clone the repository and work from its root, so that
tests/fixtures/...paths work. - O3. Check the install with
ddigraph versionandddigraph detect. - O4. Tell ddigraph where Neo4j is with
DDIGRAPH_NEO4J_URI,DDIGRAPH_NEO4J_USERandDDIGRAPH_NEO4J_PASSWORD, in Bash, in PowerShell or in a.envfile. - O5. Start the servers that later modules use: Neo4j 5 in Docker or on Aura Free, PostgreSQL 18 and Gremlin Server 3.8.2.
Prerequisites: Module 1
Time: 30 min self-paced / 45 min instructor-led.
Needs: No server. Docker or a free Neo4j Aura account is optional here, and needed from Module 7.
New in this module: pip install "ddigraph[...]", uv sync, ddigraph version, DDIGRAPH_NEO4J_URI, .env, docker run
1. What you need¶
You need three things on your computer:
- Python 3.12 to 3.15. ddigraph runs inside Python.
- Git, to copy the course's sample files.
- A terminal: Terminal on macOS, a shell on Linux, or PowerShell on Windows.
Check that your Python is new enough:
python -c "import sys; print(sys.version_info >= (3, 12))"
Expected output:
True
False means your Python is too old. Install a newer one from
https://www.python.org/downloads/. On some systems the command is
python3, not python.
Docker is optional. Later modules use it to run database servers on your computer. You do not need it until Module 7.
2. Clone the repository¶
The course reads small DDI sample files that live in the ddigraph
repository, in tests/fixtures/. Cloning means copying the repository
to your computer with Git:
git clone https://github.com/pbisson44/ddigraph.git
cd ddigraph
Every command in this course runs from this folder, the root of the
repository. A path such as tests/fixtures/lfs_codebook.xml is
relative: it starts from the folder you are in. List the sample files
to check that you are in the right place:
ls tests/fixtures/*.xml
Expected output:
tests/fixtures/cdi_correspondence.xml
tests/fixtures/cdi_inherited_endpoints.xml
tests/fixtures/cdi_sample.xml
tests/fixtures/cdi_valid_minimal.xml
tests/fixtures/codebook_sample.xml
tests/fixtures/fragment_instance.xml
tests/fixtures/ir_cdi_valid.xml
tests/fixtures/lfs_cdi.xml
tests/fixtures/lfs_codebook.xml
tests/fixtures/lfs_correspondence.xml
tests/fixtures/lfs_lifecycle.xml
tests/fixtures/reusable_fragments.xml
If ls says there is no such file, you are in another folder. Run cd
to the folder that git clone made.
The four files whose names start with lfs_ hold the course's sample
survey, the Labour Force Survey from
Module 1:
| File | What it holds |
|---|---|
lfs_codebook.xml |
The survey in DDI-Codebook: its data file, 7 variables, their questions and categories. |
lfs_lifecycle.xml |
The survey in DDI-Lifecycle: the questionnaire and its routing, 8 questions, 6 code lists and 7 variables. |
lfs_cdi.xml |
The survey's 7 variables and 5 code lists in DDI-CDI. |
lfs_correspondence.xml |
Two maps in DDI-CDI: the survey's age groups to broader ones, and the education levels of ISCED 1997 to those of ISCED 2011, its newer version. |
Most examples use these four. The other files are small test files. Each one is made to show one odd case, such as a broken reference, and the modules that use them say so.
3. Install ddigraph¶
Pick one of two ways. Both give you the same ddigraph command and the
same Python package.
With pip, in a virtual environment¶
A virtual environment is a private copy of Python for one project, so its packages do not clash with others. Create one in the repository root, then turn it on:
python -m venv .venv
source .venv/bin/activate
python -m venv .venv
.venv\Scripts\Activate.ps1
Your prompt now starts with (.venv). Install ddigraph with the extras
the course uses. An extra is an optional part of a package, named in
square brackets. Keep the quotes: some shells read square brackets as a
pattern.
# Modules 1 to 7: the base package and SHACL validation
pip install "ddigraph[shacl]"
# The whole course: every backend (NetworkX, pandas, Oxigraph, SQL, Gremlin, Arrow, the API)
pip install "ddigraph[all]"
The base install already holds the Neo4j driver. The shacl extra adds
rdflib and pyshacl, which Module 6 uses. all
adds every other backend, for the later modules. On Python 3.15, all
leaves out DuckDB, which has no build for it yet.
Installation lists each extra.
From the clone, with uv¶
uv is a fast Python package manager. It can install ddigraph from the files you cloned, with the exact versions the project tests:
uv sync --locked --extra all
source .venv/bin/activate
On Windows, turn the environment on with .venv\Scripts\Activate.ps1.
You can also skip that step and put uv run before each command, as in
uv run ddigraph version.
4. Check the install¶
Ask ddigraph for its version:
ddigraph version
Expected output (a later release prints a higher number):
0.6.0
Then read one sample file, as in Module 1:
ddigraph detect tests/fixtures/lfs_codebook.xml
Expected output (your path will differ):
Format: codebook
File: .../tests/fixtures/lfs_codebook.xml
The same check from Python:
import ddigraph
print(ddigraph.__version__)
print(ddigraph.detect("tests/fixtures/lfs_codebook.xml"))
Expected output:
0.6.0
codebook
If you see command not found or No module named 'ddigraph', the
virtual environment is not turned on in this terminal. Run the activate
line again.
5. Tell ddigraph where Neo4j is¶
From Module 7 on, ddigraph writes to Neo4j, a graph database. It finds the server through three environment variables: named values that your shell passes to every program it starts.
| Variable | What it holds | For a local Docker Neo4j |
|---|---|---|
DDIGRAPH_NEO4J_URI |
The server's address | bolt://localhost:7687 |
DDIGRAPH_NEO4J_USER |
The user name | neo4j |
DDIGRAPH_NEO4J_PASSWORD |
The password | password |
Set them in the terminal you work in:
export DDIGRAPH_NEO4J_URI=bolt://localhost:7687
export DDIGRAPH_NEO4J_USER=neo4j
export DDIGRAPH_NEO4J_PASSWORD=password
$env:DDIGRAPH_NEO4J_URI = "bolt://localhost:7687"
$env:DDIGRAPH_NEO4J_USER = "neo4j"
$env:DDIGRAPH_NEO4J_PASSWORD = "password"
These last until you close the terminal. To keep them, write them in a
file called .env in the repository root instead. ddigraph reads that file
from the folder you run it in:
DDIGRAPH_NEO4J_URI=bolt://localhost:7687
DDIGRAPH_NEO4J_USER=neo4j
DDIGRAPH_NEO4J_PASSWORD=password
When a value is set in both places, the shell's variable wins over the
.env file. Never commit a .env file that holds a real password.
Configuration lists every setting.
Once a server runs (next section), check that ddigraph can reach it. The
Settings class reads the variables and the .env file, the same way the
ddigraph command does:
from neo4j import GraphDatabase
from ddigraph.config import Settings
settings = Settings()
auth = (settings.neo4j_user, settings.neo4j_password.get_secret_value())
with GraphDatabase.driver(settings.neo4j_uri, auth=auth) as driver:
driver.verify_connectivity()
print("Connected as", settings.neo4j_user)
Expected output:
Connected as neo4j
An error such as Couldn't connect to localhost:7687 means no server
answers at that address. Unauthorized means the user name or password
is wrong.
6. Start the servers for later modules¶
None of these is needed before Module 7. Start them when a module asks for them. Each command runs a server in Docker, which keeps it in a container: a small, separate system that you can stop and start.
Neo4j 5, in Docker¶
docker run -d --name neo4j -p 7474:7474 -p 7687:7687 -e NEO4J_AUTH=neo4j/password neo4j:5
-p 7687:7687opens the port that ddigraph and the Python driver use.-p 7474:7474opens Neo4j Browser, a web page where you type Cypher. Open http://localhost:7474 and log in asneo4jwith the passwordpassword.neo4j:5asks for the newest Neo4j 5 release. Always name the version: a tag without one can move to a new major version.
Stop the server with docker stop neo4j, and start it again with
docker start neo4j. Your data stays until you remove the container with
docker rm neo4j.
Neo4j on Aura Free, without Docker¶
Neo4j Aura runs Neo4j for you in the cloud, and its free tier is enough
for this course. Create a free instance at https://console.neo4j.io.
Aura shows its password once, and offers it as a file to download. Save
that file as .env in the repository root. It holds lines like these:
NEO4J_URI=neo4j+s://xxxxxxxx.databases.neo4j.io
NEO4J_USERNAME=neo4j
NEO4J_PASSWORD=your-generated-password
ddigraph reads these bare NEO4J_ names too, so you need nothing else.
The neo4j+s:// scheme turns on encryption. If you also set the
DDIGRAPH_ variables, they win: unset them in that terminal.
PostgreSQL 18 and Gremlin Server 3.8.2¶
Module 20 uses PostgreSQL, and Module 22 uses a Gremlin server:
docker run -d --name ddigraph-postgres -p 5432:5432 \
-e POSTGRES_PASSWORD=postgres postgres:18
docker run -d --name ddigraph-gremlin -p 8182:8182 tinkerpop/gremlin-server:3.8.2
Their modules connect to
postgresql://postgres:postgres@localhost:5432/postgres and
ws://localhost:8182/gremlin. The Gremlin server keeps its graph in
memory, so it starts empty each time.
Exercises¶
Scenario
You are preparing a laptop for a ddigraph workshop. Before the day, you check that Python, ddigraph and the sample files are ready, and that the laptop can reach a Neo4j server.
Exercise 1 (O1, O3). In one Python script, print whether your Python is 3.12 or newer, then the installed ddigraph version.
Expected output:
True
0.6.0
Answer
import sys
import ddigraph
print(sys.version_info >= (3, 12))
print(ddigraph.__version__)
True
0.6.0
sys.version_info compares as a tuple, so (3, 14, 0) >= (3, 12) is
True. A version number at all shows that this Python can import
ddigraph.
Exercise 2 (O2, O3). A teammate types cd tests by mistake, then runs
ddigraph detect tests/fixtures/lfs_codebook.xml. Show what they see,
and the fix.
Expected output: an error that the file is not readable, then the flavor once you are back at the root.
Answer
cd tests
ddigraph detect tests/fixtures/lfs_codebook.xml 2>&1 || echo "exit code $?"
cd ..
ddigraph detect tests/fixtures/lfs_codebook.xml
usage: ddigraph detect [-h] [--json] xml_path
ddigraph detect: error: argument xml_path: DDI XML path must reference a readable file: tests/fixtures/lfs_codebook.xml
exit code 2
Format: codebook
File: .../tests/fixtures/lfs_codebook.xml
A relative path starts from the current folder. From inside tests,
tests/fixtures/... points at tests/tests/fixtures/..., which does
not exist. Going back to the root fixes it.
Exercise 3 (O4, O5). Start Neo4j 5 in Docker, or create an Aura Free
instance. Set the three DDIGRAPH_NEO4J_ variables, or save Aura's
.env file, then check that ddigraph can reach the server.
Expected output:
Connected as neo4j
Answer
For Docker, start the server and set the variables (Bash shown):
docker run -d --name neo4j -p 7474:7474 -p 7687:7687 -e NEO4J_AUTH=neo4j/password neo4j:5
export DDIGRAPH_NEO4J_URI=bolt://localhost:7687
export DDIGRAPH_NEO4J_USER=neo4j
export DDIGRAPH_NEO4J_PASSWORD=password
Wait a few seconds for Neo4j to start, then run:
from neo4j import GraphDatabase
from ddigraph.config import Settings
settings = Settings()
auth = (settings.neo4j_user, settings.neo4j_password.get_secret_value())
with GraphDatabase.driver(settings.neo4j_uri, auth=auth) as driver:
driver.verify_connectivity()
print("Connected as", settings.neo4j_user)
Connected as neo4j
Settings() reads the same variables, and the same .env file, as
every ddigraph command. So if this connects, ddigraph load will
too. With Aura, the user name is the one in its file.
Quiz¶
Question 1 (O1): Module 6 checks graphs with SHACL. Which command installs what it needs?
A. pip install ddigraph
B. pip install "ddigraph[shacl]"
C. pip install ddigraph shacl
D. pip install "ddigraph[neo4j]"
Answer
B. An extra goes in square brackets after the package name,
and the quotes stop the shell from reading the brackets. C installs
an unrelated package called shacl. A gives the base package,
which has no SHACL validator.
Question 2 (O2): You run ddigraph detect tests/fixtures/lfs_codebook.xml from your home folder and get 'must reference a readable file'. What is wrong?
A. ddigraph is not installed.
B. The file is not valid DDI.
C. The path is relative, and your home folder has no tests/fixtures in it.
D. Neo4j is not running.
Answer
C. A relative path starts from the current folder. Run cd to
the repository root first. A is tempting, but then the shell would
say command not found, before ddigraph could check the file.
Question 3 (O3): ddigraph version prints command not found, yet you installed ddigraph an hour ago. What is the most likely cause?
A. The install failed without a message.
B. This terminal has not turned on the virtual environment you installed into.
C. ddigraph needs Neo4j to run.
D. version is not a ddigraph command.
Answer
B. A new terminal starts outside the virtual environment, so
it cannot find the ddigraph command. Run the activate line
again, or use uv run ddigraph version. ddigraph needs no server
to print its version (C).
Question 4 (O4): The shell has DDIGRAPH_NEO4J_URI=bolt://localhost:7687, and the .env file has DDIGRAPH_NEO4J_URI=neo4j+s://abc.databases.neo4j.io. Which server does ddigraph load use?
A. bolt://localhost:7687, because the shell's variable wins over the .env file.
B. The Aura one, because the .env file wins.
C. Neither: ddigraph stops because they disagree.
D. Both: it loads into each server.
Answer
A. An environment variable beats the .env file. To use Aura,
unset the variable in that terminal, or close it and open a new
one.
Question 5 (O5): Neo4j runs in Docker with -p 7474:7474 -p 7687:7687. Which address do you open in a web browser to type Cypher?
A. bolt://localhost:7687
B. http://localhost:7687
C. http://localhost:7474
D. http://localhost:5432
Answer
C. Port 7474 serves Neo4j Browser over HTTP. Port 7687 speaks
Bolt, the protocol that ddigraph and the driver use, so A is the
value of DDIGRAPH_NEO4J_URI, not a web page. Port 5432 is
PostgreSQL.
Check yourself¶
- I can install ddigraph with pip and the extras the course uses, or from a clone with uv. (O1)
- I can clone the repository and work from its root, so that
tests/fixtures/...paths work. (O2) - I can check the install with
ddigraph versionandddigraph detect. (O3) - I can tell ddigraph where Neo4j is with the
DDIGRAPH_NEO4J_variables or a.envfile. (O4) - I can start Neo4j 5 in Docker or on Aura Free, and PostgreSQL and Gremlin when a module needs them. (O5)