conda-inject
Conda-inject provides helper functions for dynamically injecting a specified conda environment into the current Python environment. It achieves this by modifying `sys.path`, allowing access to packages within the target conda environment without fully activating it in the shell. The library aims to simplify the use of conda environments in scripts and applications where a full shell activation might be cumbersome. Version 1.3.2 was released on May 27, 2024, indicating an active development status with several minor releases in the past year.
Common errors
-
ModuleNotFoundError: No module named 'some_package'
cause The package 'some_package' is installed in the target conda environment, but the `inject_env` call did not correctly add the environment's `site-packages` to `sys.path`, or the environment path provided was incorrect.fixVerify the `conda_env_path` provided to `inject_env` points directly to the root of the conda environment (e.g., `/opt/miniconda3/envs/myenv`). Ensure 'some_package' is indeed installed in that specific conda environment using `conda list -n myenv` or `conda list -p /path/to/myenv`. -
Python interpreter shows a different version than the injected conda environment's Python.
cause Conda-inject modifies `sys.path` to include packages, but it does not change the Python interpreter that initiated the script. If your script started with Python 3.9 and you inject an environment with Python 3.10, `sys.version` will still reflect 3.9.fixThis is expected behavior. If you need to *switch* the Python interpreter, you must activate the conda environment in your shell *before* running the Python script, or use a tool like `conda run -n myenv python my_script.py`.
Warnings
- gotcha Conda-inject modifies `sys.path` but does not perform full shell environment activation (e.g., setting environment variables or modifying the shell prompt). This means tools or scripts relying on an 'activated' shell environment might not behave as expected.
- gotcha Injecting environments with different major Python versions or highly conflicting dependencies can lead to runtime errors or unexpected behavior due to `sys.path` order and incompatible shared libraries.
Install
-
pip install conda-inject
Imports
- inject_env
from conda_inject import inject_env
Quickstart
import os
from conda_inject import inject_env
# Replace with the actual path to your conda environment
# Example: /Users/youruser/miniconda3/envs/my_env or C:\Users\youruser\miniconda3\envs\my_env
conda_env_path = os.environ.get('CONDA_ENV_PATH', '/path/to/your/conda/envs/myenv')
try:
inject_env(conda_env_path)
print(f"Successfully injected conda environment from: {conda_env_path}")
# Now you can import packages from the injected environment
# For example, if 'requests' is in myenv:
import requests
print(f"Successfully imported 'requests' (from injected env if present): {requests.__version__}")
except FileNotFoundError:
print(f"Error: Conda environment path not found at {conda_env_path}")
except Exception as e:
print(f"An error occurred: {e}")