liveBook · Manning

Environment variable

As explained in Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi

An environment variable is a named operating-system value made available to programs at runtime for configuration or other contextual information.

An environment variable is a named configuration value supplied by Linux to a running Python program. It keeps settings outside the code, so the same script can behave differently without editing its source. For a two-motor robot, separate values can compensate for motors that run at slightly different speeds and help the robot drive straighter. section 3.3 section 3.5

Use a clear shared prefix when naming variables. Names such as ROBO_DC_ADJUST_R and ROBO_DC_ADJUST_L are easy to find with `env | grep ROBO_`. Set a value in Bash with `export ROBO_DC_ADJUST_R="0.8"`, then start the Python script in that terminal session. section 3.5

Read configuration defensively in Python:

  • Import `os`, which provides access to environment variables. section 3.5
  • Treat every environment value as a string. Convert it to the type the program needs, such as `float` for a motor-power adjustment. section 3.5
  • Use `os.environ.get(name, default)` for optional settings. Direct indexing with `os.environ[name]` raises `KeyError` when the name is absent. section 3.5
  • Choose a safe default such as `1`, so an unset motor adjustment leaves the requested throttle unchanged. section 3.5

Adjust each motor without changing movement code

Read right and left adjustments once, store them by motor name, and apply the matching adjustment inside a reusable throttle function. `forward()` can still request equal throttle for both motors, while configuration scales each side independently. Set the right adjustment to `0.8` when that motor needs less power; leave an unset side at the default of `1`. section 3.5

Listing 3.2 envforward.py: Reading configuration values from environment variables
#!/usr/bin/env python3
from adafruit_crickit import crickit
import time
import os
 
DC_ADJUST_R = float(os.environ.get('ROBO_DC_ADJUST_R', '1'))
DC_ADJUST_L = float(os.environ.get('ROBO_DC_ADJUST_L', '1'))
ADJUST = dict(R=DC_ADJUST_R, L=DC_ADJUST_L)
MOTOR = dict(R=crickit.dc_motor_1, L=crickit.dc_motor_2)
 
def set_throttle(name, value):
    MOTOR[name].throttle = value * ADJUST[name]
 
def forward():
    set_throttle('R', 0.9)
    set_throttle('L', 0.9)
    time.sleep(0.2)
    set_throttle('R', 0)
    set_throttle('L', 0)
 
forward()

This runnable script reads optional motor-adjustment variables, converts them to floats, and applies them during forward motion. section 3.5

Environment variables can also select application behavior. A web application can set its Tornado `debug` setting from whether `ROBO_DEBUG` is defined, allowing development debugging behavior to be controlled outside the code base while keeping one script for development and production use. section 6.3.1

How Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi covers it

How deep it goes
Demonstrates Bash setup, Python lookup, missing-value defaults, string-to-float conversion, and integration into motor control.
Examples
Uses ROBO_DC_ADJUST_R and ROBO_DC_ADJUST_L to correct motor power, plus ROBO_DEBUG for a Tornado application.
What it assumes
Introduces terminal-based variable setup and Python access while using Python scripts, Linux, and Bash.
What you can do after
Apply it via Python’s os module to configure motor throttle adjustments and Tornado debug behavior externally.
How it connects
Connects Linux, Bash, Python, DC motors, throttle control, Tornado, and debug mode.

Also explained in

Compare how 19 books explain environment variable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi. The section links point to the parts of the book it draws on.