Skip to main content

virvar is a Python utility designed to manage environment variables in virtual environments (venv)

Project description

virvar

Description

virvar is a Python utility designed to manage environment variables in virtual environments (venv).

When a virtual environment is activated, virvar automatically adds the defined environment variables to the activation script (activate). These variables are also removed when the virtual environment is deactivated.

Key Features

  • Add environment variables to an existing activate file.
  • Ensure automatic removal of variables when the virtual environment is deactivated.
  • Convenient for automating environment configuration for Python projects requiring API keys, specific paths, or other custom settings.

Prerequisites

Before using virvar, ensure you have the following:

  1. Python: Version 3.9 or newer. While the project might work with older versions, they are no longer supported due to security concerns, as per the official Python documentation.
  2. A virtual environment: Created using the venv module or equivalent.
  3. Git (optional): To clone the repository for local usage or development.

You can verify your Python version by running:

python --version

Compatibility Notice

While virvar is designed to be operating system independent, it has primarily been tested on Linux. As such, no guarantees are made regarding its functionality on other operating systems like macOS or Windows.

If you encounter compatibility issues on non-Linux platforms, you are encouraged to report them. However, addressing such issues will not be a priority for the maintainers.

Windows Support

For Windows environments, virvar is compatible with Bash only. Other shells (e.g.,PowerShell, CMD) are not officially supported at this time. Please ensure Bash is installed before using the project on Windows. You can get Bash on Windows using:

  • Git Bash (included with Git for Windows)
  • Windows Subsystem for Linux (WSL)
  • Other Bash-compatible environments.

Note: Bash is the currently the only supported option for execution on Windows.

Installation

Via pip

pip install virvar

With source code

If you want to work with the source code, clone the repository using Git:

git clone https://github.com/yourusername/virvar.git
cd virvar

Alternatively, if you have the source code, you can install it locally using:

pip install .

Verify the Installation

After installation, ensure that virvar is available:

virvar --help

Usage

1. Basic Command

The virvar command allows you to add environment variables to a virtual environment's activate script.
The basic syntax is:

virvar <path_to_venv> KEY1="value1" KEY2="value2" ...

2. Example

Suppose you have a virtual environment located at /path/to/venv, and you want to add two environment variables:

  • API_KEY with the value 12345
  • DEBUG with the value true

Run the following command:

virvar /path/to/venv API_KEY=12345 DEBUG="true"

After running the command:

  • The activate script in the virtual environment will include the export statements to set these variables when the virtual environment is activated.
  • The unset statements will ensure these variables are removed when the environment is deactivated.

**3. Verifying changes

To verify the changes made by the virvar command, open the activate file of your virtual environment. You should see lines similar to the following:

[...]
# Self destruct!
unset API_KEY
unset DEBUG
[...]
# set environment variables
export API_KEY=12345
export DEBUG="true"

These lines ensure that:

  • The environment variables are loaded when the environment is activated.
  • They are cleaned up when the environment is deactivated.

4. Help Command

If you need assistance or want to explore available options, use the --help flag:

virvar --help

This command displays a help message with details on the syntax, arguments, and available options:

usage: virvar [-h] path env_var [env_var ...]

positional arguments:
  path         Path to the virtual environment created with the venv command.
  env_var      One or more environment variables in the format KEY="value".

optional arguments:
  -h, --help   Show this help message and exit.

Python Usage

virvar can be directly integrated into Python scripts, offering a flexible and tailored solution for automation scenarios.

Example Code

from virvar.virvar import virvar

# Configure environment variables
virvar(
    "/path/to/venv",
    API_KEY=12345,
    DEBUG="true",
    TIMEOUT=30
)

Testing

To ensure that the functionality of virvar is working correctly, we recommend running the tests. This project uses pytest for testing.

Running the Tests

To run all the tests in the project, you can execute the following command:

pytest

This will automatically discover and run all tests within the tests/ directory.

Test Coverage

The tests cover:

  • Creation of a fake virtual environment with an activate file.
  • Adding environment variables to the activate file.
  • Validating the contents of the activate file to ensure proper variable insertion.

Test Output

Upon running pytest, the test results will be displayed in your terminal. If all tests pass, you will see:

======================================================
1 passed in 0.02s
======================================================

Support

If you encounter any issues while using virvar or have any questions, feel free to open an issue in the project's GitLab repository.

Please provide the following information when creating an issue:

  • A clear description of the problem or question.
  • Steps to reproduce the issue (if applicable).
  • Any relevant error messages or logs.

We will do our best to assist you and address the issue as soon as possible.

Roadmap

The following features are planned for future versions of virvar:

  • Reset the file: Add functionality to reset the activate file to its original state, removing any added environment variables.
  • Remove environment variables: Implement a feature to remove specific environment variables from the activate file or unset them during deactivation.

These features will help improve the flexibility and ease of use for managing virtual environments and their environment variables.

Contributing

We welcome contributions to virvar! To contribute, please follow these guidelines:

  1. Fork the repository: Fork the repository on GitLab to your own account.

  2. Create a feature branch: Before making any changes, create a new branch for your feature or bug fix. Use descriptive names for branches (e.g., fix-bug-in-activate, add-reset-feature).

    git checkout -b feature/your-feature-name
    
  3. Make your changes: Make your changes and ensure that they follow the project's code style.

  4. Test your changes: Ensure that all tests pass locally before submitting a pull request. If necessary, write new tests to cover your changes.

  5. Commit your changes: Commit your changes with a clear and concise commit message. Ensure your commit message follows the conventional commit format.

    git commit -m "feat: add functionality to reset the activate file"
    
  6. Push to your fork: Push your changes to your forked repository.

    git push origin feature/your-feature-name
    
  7. Create a pull request: Open a pull request (PR) on GitLab to merge your changes into the main branch. Provide a clear description of the changes in the PR.

  8. License: Contributions must comply with the project's license, which is typically the MIT License, unless otherwise stated.

By Contributing to virvar, you agree to follow the project's code of conduct and abide by the guidelines for submetting contributions.

We look forward to your contributions!
Thank you!: We appreciate your contributions to virvar! Your help makes the project better for everyone.

License

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

virvar-1.0.0.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

virvar-1.0.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file virvar-1.0.0.tar.gz.

File metadata

  • Download URL: virvar-1.0.0.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for virvar-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0e9031b89fcadae7069c3b6f2ce7a78f3e08e36b4f2d66f6518aa149b5762f1e
MD5 174a3e9fca1fe065c9278bb7c3ec6dcb
BLAKE2b-256 d5547b0ee991f1a873e63ea568ecca13afcff3d1e8e16580f32082b6f1c8d4a8

See more details on using hashes here.

File details

Details for the file virvar-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: virvar-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for virvar-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 87a6fbd0d895b4a52ed97a3c6bb0171176ea261ce2087a4a11b3b380587dcf6f
MD5 cbed740cbe0428126ea5679088f52567
BLAKE2b-256 5987964213618b05964c35e3e96940366fd6a54bec9a4d795a8e3ed47a4df120

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page