Skip to main content

A simple wrapper for executing virtualenv commands and passing them environment variables via a config file.

Use case

Let’s say you deploy a Django site named “example.com”. You follow all the best practices and deploy it as a Python package, installed in an isolated virtualenv at /usr/share/python/example-com (as dh_virtualenv would do).

Configuration is read from environment variables in the Django settings.

When you want to run a Django management command on your server, you would type:

ENV_VAR_1=value ENV_VAR_2=other … /usr/share/python/example-com/bin/django-admin <command>

You could use envdir or bash’s source command to expose your environment variables, but Env-cmd eases that further. It allows you to read a configuration file, expose values in the environment and run a command simply by adding a setuptools entry point in your setup.py. With the entry point symlinked to /usr/bin, your command becomes:

example-com django-admin <command>

Env-cmd:

  • Reads a configuration file using the default location provided, or an override.

  • Exposes configuration values as environment variables.

  • Resolves the path of your virtualenv.

  • Shells out to the virtualenv command.

Installation

pip install env-cmd

Usage

In <yourproject>/cli.py:

import env_cmd

read_environ = env_cmd.read_environ(
    'PRCONFIG',  # name of the env var that allows overriding of the
                 # config path.
    '/etc/yourproject.conf', # default config path.
    {'SOME_ENV_VARIABLE': 'DEFAULT_VALUE', # Some default environment
     'OTHER_VARIABLE': 'OTHER VALUE'},     # variables.
)
main = env_cmd.main(read_environ)

In your setup.py:

setup(
    …
    entry_points={'console_scripts': ['yourproject=yourproject.cli:main']},
)

Then symlink /path/to/virtualenv/bin/yourproject to /usr/bin/yourproject.

You can run a command from your virtualenv with:

yourproject <command>

Override the default config path with:

PRCONFIG=/path/to/config.conf yourproject <command>

Configuration syntax is the following:

KEY=value
OTHER=some other value
QUOTED="quotes are stripped."
ALSO='single quotes too'
WHITESPACE=    is stripped as well.
# comments work like this

# empty lines are skipped

If you run a web server with Gunicorn, you can use the on_reload hook to read configuration on server reload, allowing zero-downtime configuration updates:

# gunicorn.conf.py
import os

from yourproject import read_environ

def on_reload(server):
    server.log.info("Reading environ")
    os.environ = read_environ()
    server.log.info(os.environ)

Release files for env-cmd 1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for env-cmd 1.1
File Size Uploaded
env-cmd-1.1.tar.gz 4.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for env-cmd 1.1
File Interpreter ABI Platform
env_cmd-1.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 7.2 kB

Release files / env-cmd-1.1.tar.gz

Download URL env-cmd-1.1.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c94f2d3ca10d65bf0d3104154a7d5ac13abbae0aa3247847537a1af58ff33f21
BLAKE2b-256 checksum
How to use checksums
e32790ff01179657e80f404418347c245880364858dedaa8d43f436d7d2d42e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / env_cmd-1.1-py2.py3-none-any.whl

Download URL env_cmd-1.1-py2.py3-none-any.whl
Size 3.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
628a061097bc9054955eac3e8bdaeb7399e5ffaa9997f9cac73532c61cedaf85
BLAKE2b-256 checksum
How to use checksums
085e37eba25ca32befe998230c6bc661e3c67ca57b6db464a9e6ec3eb56d64bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.1 This release

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page