@fnet/auto-conda-env
v0.1.21
Published
## Introduction
Downloads
1,405
Readme
@fnet/auto-conda-env
Introduction
The @fnet/auto-conda-env
project is designed to facilitate the dynamic management of Conda environments. Its primary purpose is to create and manage Python environments efficiently by checking existing setups and reusing them when possible, based on the specified requirements. This feature is particularly useful for developers and data scientists who frequently work with different projects requiring varying Python environments and packages.
How It Works
The project operates by creating Conda environments on demand while actively reusing any existing environments that match the specified Python version and package requirements. It handles the necessary setup by checking for Python version compatibility and package matching, thus eliminating redundant environment creations. Users provide the desired Python version and list of packages, and the project automates the environment setup and management behind the scenes.
Key Features
- Environment Reuse: Reutilizes pre-existing Conda environments if they match the specified configurations (Python version and required packages), saving time and resources.
- Flexible Environment Management: Offers options for strict or flexible management, allowing users to choose how environments should be matched and reused.
- Automatic Environment Setup: Creates new environments with the desired Python version and packages when no existing environment matches the criteria.
- Metadata Management: Maintains a metadata file for each environment to track installed packages and configurations, aiding in environment validation and reuse.
- Python and Package Management Utilities: Provides utility functions to run Python files and scripts within the created environments.
Conclusion
This project simplifies the process of managing Conda environments by promoting efficient reuse and setup based on user-specified configurations. Its automation capabilities help users maintain consistency across projects requiring different Python setups, making it a valuable tool for anyone who regularly works in environments reliant on specific Python versions and package configurations.
Developer Guide for @fnet/auto-conda-env
Overview
The @fnet/auto-conda-env
library is designed to automate the management and creation of Conda environments with specific package requirements. This tool is particularly useful for developers who frequently need to set up Python environments, ensuring that required packages and versions are correctly installed while minimizing redundant environment setups. The library allows users to create environments that can be reused if they match the specified package requirements and Python version.
Installation
To integrate @fnet/auto-conda-env
into your project, you can install it via npm or yarn. Use one of the following commands in your terminal:
npm install @fnet/auto-conda-env
or
yarn add @fnet/auto-conda-env
Usage
The library's core functionality revolves around creating and managing Conda environments. Here's a breakdown of how you can use this tool in real-world scenarios:
Creating an Environment with Specific Packages
To create a new Conda environment or reuse an existing one based on package and Python version specifications, utilize the library's main function. This function checks if an environment that fulfills the requirements exists and either reuses it or sets up a new one accordingly.
Example
Let's say you need a Python environment with version 3.8 and a particular set of packages:
import autoCondaEnv from '@fnet/auto-conda-env';
async function setupEnvironment() {
const environmentDetails = await autoCondaEnv({
pythonVersion: '3.8',
packages: [
{ package: 'numpy', version: '^1.21.0' },
{ package: 'pandas', version: '^1.3.0' }
]
});
console.log('Environment setup at:', environmentDetails.envDir);
}
setupEnvironment().catch(console.error);
Utility Functions
Upon setting up or retrieving an environment, several utility functions are available to run scripts, execute code, or handle package management within this environment.
Running a Python Script
async function runScript() {
const env = await autoCondaEnv({ pythonVersion: '3.8', packages: [] });
await env.runFile('/path/to/your/script.py');
}
Executing Inline Python Code
async function runInlineCode() {
const env = await autoCondaEnv({ pythonVersion: '3.8', packages: [] });
await env.runCode('print("Hello from within the Conda environment!")');
}
Managing Packages with Pip
async function managePackages() {
const env = await autoCondaEnv({ pythonVersion: '3.8', packages: [] });
await env.runPip(['install', 'requests']);
}
Examples
Here are more code snippets that showcase the typical usage patterns of @fnet/auto-conda-env
:
Using an Existing Environment
async function useExistingEnvironment() {
const env = await autoCondaEnv({
pythonVersion: '>=3.6 <3.9',
packages: [{ package: 'matplotlib', version: '*' }]
});
console.log(`Using environment located at: ${env.envDir}`);
}
Creating Environments Strictly by Channel
async function environmentWithChannel() {
const env = await autoCondaEnv({
pythonVersion: '3.8',
packages: [{ package: 'scipy', version: '1.5.2', channel: 'conda-forge' }],
strictChannelMatch: true
});
console.log('Environment with specific channel set up at:', env.envDir);
}
Acknowledgement
If applicable, please acknowledge the use of any external libraries or contributors that have influenced the development of @fnet/auto-conda-env
. This helps to foster a supportive community and gives credit to those whose work was beneficial.
Input Schema
$schema: https://json-schema.org/draft/2020-12/schema
type: object
properties:
pythonVersion:
type: string
description: Desired Python version.
packages:
type: array
minItems: 0
description: Array of package definitions.
items:
type: object
properties:
package:
type: string
description: Package name.
version:
type: string
description: Package version range.
channel:
type: string
description: Package channel.
required:
- package
defaultChannel:
type: string
default: pypi
description: Default channel for unspecified packages.
strictChannelMatch:
type: boolean
default: false
description: Strict channel matching for packages.
envDir:
type: string
description: Path to the dedicated Conda environment.
required:
- pythonVersion
- packages