@fnet/auto-conda-env
v0.1.14
Published
## Introduction
Downloads
657
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 simplifies the management of Conda environments by dynamically creating and reusing environments based on specified Python and package requirements. This tool is designed to streamline the workflow of developers who frequently need to set up Python environments with specific dependencies. By leveraging this library, you can ensure consistency in package versions and maximize efficiency by reusing pre-existing environments when possible.
Installation
To install @fnet/auto-conda-env
, you can use npm or yarn. This will automatically handle all necessary dependencies.
npm install @fnet/auto-conda-env
Or using yarn:
yarn add @fnet/auto-conda-env
Usage
The library provides a simple interface to create or reuse Conda environments. Below is a step-by-step example of how to use the library in a typical use case.
Create or Use Environment
To create or reuse a Conda environment, use the main function exported by the library. You need to specify the desired Python version and packages. The function will either reuse a compatible environment or create a new one if necessary.
import autoCondaEnv from '@fnet/auto-conda-env';
(async () => {
const environment = await autoCondaEnv({
pythonVersion: '3.9',
packages: [
{ package: 'numpy', version: '^1.21.0' },
{ package: 'pandas' } // Latest version will be used
],
defaultChannel: 'conda-forge' // Optional, defaults to 'pypi'
});
console.log(`Environment directory: ${environment.envDir}`);
// Run a Python script within the environment
await environment.runFile('example.py', ['arg1', 'arg2']);
})();
Utility Functions
Once you have an environment, you can interact with it using utility functions provided by the environment object:
runFile(filePath, args, options)
: Run a Python script with the specified arguments.runBin(bin, args, options)
: Execute a binary within the environment with specified arguments.runCode(code, args, options)
: Run a block of Python code directly, useful for testing snippets or one-off tasks.
// Running a binary command within the environment
await environment.runBin('python', ['-m', 'http.server', '8000']);
// Running a Python code snippet
await environment.runCode(`
print("Hello from Python code!")
`, []);
Examples
Here are some additional examples showcasing core functionalities:
Reusing an Environment
If the same environment has been previously created, the library will reuse it for efficiency.
const env = await autoCondaEnv({
pythonVersion: '3.9',
packages: [{ package: 'flask', version: '^2.0.0' }]
});
// Reusing the environment in subsequent runs
const reusedEnv = await autoCondaEnv({
pythonVersion: '3.9',
packages: [{ package: 'flask', version: '^2.0.0' }]
});
Acknowledgment
The development of @fnet/auto-conda-env
relies on contributions and ideas from developers utilizing Conda for managing Python environments.
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