You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

mydotenv

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mydotenv

A simple package to manage environment variables with command-line interface

0.2.6
pipPyPI
Maintainers
1

mydotenv

rrr A simple Python package to manage environment variables in your .env file with command-line interface.

Installation

pipx install mydotenv

Using pip

pip install mydotenv

From Source

git clone https://github.com/banddude/mydotenv.git
cd mydotenv
pip install -e .

Usage

Basic Command Line Interface

  • Print a variable's value:
mydotenv API_KEY
  • Add or update a variable:
mydotenv NEW_KEY=value
mydotenv "KEY_WITH_SPACES=value with spaces"
  • Delete a variable:
mydotenv delete VARIABLE_NAME
  • View all variables:
mydotenv

Custom Command Name

You can set a custom command name to use instead of mydotenv:

mydotenv --set-command dotman

Now you can use either mydotenv or dotman to run commands:

dotman NEW_KEY=value
dotman delete NEW_KEY

When you set a new command name, it replaces any previous custom command name.

Advanced Features (New)

Scanning for Environment Variables

The new CLI functionality lets you scan your codebase for env.VARIABLE_NAME references and manage your .env file based on what it finds:

  • Replace existing .env with newly discovered variables (empty values):
mydotenv --replace env [<path>]
  • Add newly discovered variables as empty placeholders (preserving existing ones):
mydotenv --add-empty env [<path>]
  • Add newly discovered variables (same as --add-empty in current implementation):
mydotenv --add env [<path>]

Import Statement Rewriting

You can rewrite Python files to directly access environment variables without needing to use the env object:

# Rewrite imports for a specific file or directory
mydotenv --add imports [<path>]

# Rewrite imports for all Python files in the current directory
mydotenv --add imports

This will:

  • Remove any import mydotenv or from mydotenv import ... statements
  • Add code to automatically find and load the .env file from your project
  • Make environment variables directly accessible as Python variables

For example, if your .env contains API_KEY=123, you can simply do:

# This works after running mydotenv --add imports on your file
print(API_KEY)  # Prints: 123

No more need for: from mydotenv import env and print(env.API_KEY)!

Optional Parameters

  • --ignore: Specify paths to ignore when scanning
mydotenv --add env . --ignore tests venv
  • --env-file: Specify which .env file to use (default: .env)
mydotenv --replace env . --env-file .env.dev

Features

  • Manage environment variables from the command line
  • Set custom command names for easier use
  • Preserves comments and formatting in .env file
  • Handles values with spaces and special characters
  • Works from any directory
  • Creates and manages .env file in your current directory
  • NEW: Scan codebase for environment variable references
  • NEW: Replace or update .env files based on discovered variables
  • NEW: Rewrite imports to allow direct environment variable access

Dependencies

  • python-dotenv >= 1.0.0

Configuration

  • The package stores its configuration in ~/.config/mydotenv/config.env
  • Custom command names are managed through symlinks in ~/.local/bin

License

MIT License - feel free to use this package in your projects!

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts