Skip to main content

A Python client working example for connecting to ClickHouse Cloud Service

Learn how to connect to ClickHouse Cloud Service using Python with a step-by-step example using the clickhouse-connect driver.

This is a step by step example on how to start using Python with ClickHouse Cloud service.

Note

Keep in mind that Python versions and libraries dependencies are constantly evolving. Make also sure to use the latest supported versions of both the driver and Python environment when trying this.

At the time of writing this article, we're using the clickhouse-connect driver version 0.5.23 and python 3.11.2 respectively.

Steps

  1. Check the Python version:
  1. We'll assemble the project in a folder called ch-python:
  1. Create a dependencies file named requirements.txt with:
  1. Create a python source file named main.py:
  1. Create the virtual environment:
  1. Load the virtual environment:

Once loaded, your terminal prompt should be prefixed with (venv), install dependencies:

  1. Launch the code!
Tip

If using an older Python version (e.g. 3.9.6) you might be getting an ImportError related to urllib3 library. In that case either upgrade your Python environment to a newer version or pin the urllib3 version to 1.26.15 in your requirements.txt file.

· 2 min read