Hello World: Your First openLCA Script

This is the first code example in the series. If you haven’t started the IPC server and installed the Python packages yet, the Connecting article in the Getting Started section covers this. If you have, this is the quickest way to check that everything is working.

This example is available on the Below280 Life Cycle Assessment Github, within openLCA-IPC-tools

The script

This can be saved as B280_olca_hello.py:

from olca_ipc import Client
import olca_schema as o

client = Client(8080)

processes = client.get_descriptors(o.Process)
flows = client.get_descriptors(o.Flow)

print(f"Connected to openLCA")
print(f"  Processes: {len(processes)}")
print(f"  Flows:     {len(flows)}")

Then run it:

python B280_olca_hello.py

What you should see

Something like:

Connected to openLCA
  Processes: 21473
  Flows:     47291

Your numbers will be different depending on which database you have open. If you see counts, the connection is working.

If it doesn’t work

Connection refused usually means the IPC server isn’t running. Worth checking that openLCA is open, the server is started, and the port is 8080.

Import error on olca_ipc means the packages may not be installed, or you might be running a different Python from the one you installed them into. pip show olca-ipc will confirm whether they’re there.

Counts are zero means the connection is working but the database is empty. Worth checking you’ve opened the right database in openLCA.

What just happened

Client(8080) opened a connection to the IPC server running inside openLCA. get_descriptors(o.Process) asked for a lightweight list of every process in the database, and the script printed how many it found. The scenario and sensitivity scripts in this series work the same way.