Advanced use cases
Programmatically determining the environment
Sometimes you will want to adjust your app logic depending on the environment the app is running in. For example, you might want to write to DuckDB when the app is running locally, and to Snowflake when the app is running in the Tower cloud. If you were using Snowflake in both cases, you could have solved this by having different secrets values in your local and production environments. But when you want to use different databases, libraries, or connectors, you need to use conditional logic and define entirely different secrets.
Tower provides two ways to access the environment your code is running in: the tower.info.environment() function from the Tower SDK, and the TOWER_ENVIRONMENT environment variable.
Using the Tower SDK (recommended)
import tower
env = tower.info.environment()
if env == "production":
print("Running in production!")
else:
print(f"You are in {env}!")
The tower.info.environment() function returns the name of the current environment as a string. See the API reference for details.
Using the environment variable
Alternatively, you can read the TOWER_ENVIRONMENT environment variable directly:
import os
tower_env = os.getenv("TOWER_ENVIRONMENT")
print(f"You are in {tower_env}!")
Example output
When run with the --local parameter:
tower run --local
...
You are in local!
When run without the --local parameter:
tower run
✔ Scheduling run... Done!
Success! Run #2 for app `datafusion-transform` has been scheduled
tower apps logs datafusion-transform#2
✔ Fetching logs... Done!
You are in default!