Skip to main content

SmartEnv - OS Environment Wrapper Library

A flexible, easy-to-use library for managing environment variables in your Python script.

Why yet another one?

This library comes with a few major points to use it:

  • It is easy and intuitive (see examples)
  • It's compatible with Python 2 (can be useful in old large systems)
  • Continuous support of the library

Getting started

This library is pretty easy to use:

from smart_env import ENV

print(ENV.HOME)  # Equals print(os.environ['HOME'])

# Apply type cast wherever you need by using ENV as a context manager

with ENV:
   # assuming you set env variable MYVAR to "True" in shell
   my_var = ENV.MY_VAR  # Equals boolean True
# And here type cast is automatically disabled
my_second_var = ENV.MY_VAR  # Equals 'True' as string

# Enabling automatic type cast for all the further commands
ENV.enable_automatic_type_cast()

# assuming you set env variable MYVAR to "True" in shell
my_var = ENV.MY_VAR  # Equals boolean True

ENV.NEW_VAR = 100  # Sets a new environment variable

# Disabling automatic type cast
ENV.enable_automatic_type_cast()

How to use

In a few words, the logic is next:

  1. You can store a lot of serialized values in Environment variables and then deserialize them on fly using ENV class.
  2. Each environment variable can be accessed as an attribute of ENV class:
    ENV.<variable_name>
    
  3. The internal decoding mechanism is based on json and ast packages. That means, you can parse even some JSON-incompatible values (for example, with single quotes used for defining strings).

Installing

Simply run

pip install smart-env

Running the tests

This library contains tests written using unittest module, so just run in the project directory

python -m unittest

It's possible to run tests using Tox as well:

tox -e <env>

or just

tox

Tests coverage is one of the important goals of this project. For now coverage is next:

  • For Python 2.7: 99%
  • For Python 3.x: 97%

Supported environments:

  • py27
  • py35
  • py36
  • py37
  • py38
  • py39
  • py310
  • py311
  • py312
  • coverage (using Python 3)
  • coverage27 (using Python 2.7)
  • pep8 (style checking)

Restrictions

  1. Old versions of Python in both generations (e.g. 2.6, 3.4, etc) will never be supported. However, you always can implement such support in your forks.

  2. Parsing set() objects is not working in Python 2. See this for details.

Authors

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Release files for smart-env 1.1.0

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

Built distribution (wheel)

Table of built distributions (wheels) for smart-env 1.1.0
File Interpreter ABI Platform
smart_env-1.1.0-py3-none-any.whl Python 3 none any Details

Release files / smart_env-1.1.0-py3-none-any.whl

Download URL smart_env-1.1.0-py3-none-any.whl
Size 11.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
59289ad4e1a5febde4f9fe8be3ab40e891fbce1bef7669e818633f2ab2658f10
BLAKE2b-256 checksum
How to use checksums
1d215040ce9753174977221f55defb72633d8bdcba6c1c1e9932b71a3cecb1e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.2

Release history Release notifications | RSS feed

This release

1.1.0 This release

1 release file

1.0.6

2 release files

1.0.5

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.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