Combining Conda and pip
Last updated on 2026-08-24 | Edit this page
Overview
Questions
- How do I use pip inside a conda environment?
- How do I export an environment that uses both conda and pip packages?
- How do I use these combined environments in job scripts?
Objectives
- Combine conda and pip appropriately in one environment
- Export combined environments for reproducibility
- Use combined environments in SLURM job scripts
Why Combine Conda and pip?
- Conda handles complex binary dependencies (MKL, BLAS, etc.)
- Pip handles packages not on conda (newer packages, smaller libraries)
- A single
environment.ymlfile captures both for full reproducibility
Recommended Approach
- Create a conda environment with major scientific packages:
- Activate and add pip packages:
- Export for reproducibility:
The environment.yml includes both conda and pip packages:
Practical Examples
Web development project
Using venv and pip:
BASH
$ module load miniconda3
$ python -m venv flask_app
$ source flask_app/bin/activate
(flask_app) $ pip install flask flask-sqlalchemy flask-login
(flask_app) $ pip freeze > requirements.txt
Collaborator sets up:
In Job Scripts
Using conda with pip packages
BASH
#!/bin/bash
#SBATCH --job-name=ml_inference
#SBATCH --time=01:00:00
#SBATCH --gpus=1
#SBATCH --mem=32G
module load miniconda3
conda activate ml_project
python inference.py
Both work; choose based on your project needs.
Challenge 1: Export and Recreate with requirements.txt
- Create a venv and install some packages
- Generate requirements.txt
- Create a new venv and install from the requirements file
- Verify the same packages are installed
BASH
$ module load miniconda3
$ python -m venv testapp
$ source testapp/bin/activate
(testapp) $ pip install requests beautifulsoup4
(testapp) $ pip freeze > requirements.txt
(testapp) $ deactivate
$ python -m venv testapp_copy
$ source testapp_copy/bin/activate
(testapp_copy) $ pip install -r requirements.txt
(testapp_copy) $ pip list
The requirements.txt file contains:
beautifulsoup4==4.12.2
certifi==2023.X.X
requests==2.31.0
...
After installing in the new environment, pip list shows
identical packages and versions. This demonstrates reproducibility with
pure pip and venv.
- Combine conda (for scientific packages) and pip (for pure Python packages) in one environment
- The exported environment.yml captures both conda and pip packages
- Use conda for the base, then pip for packages not available in conda
- Both venv/pip and conda approaches work in SLURM job scripts
- Always export your environment for reproducibility regardless of which tools you use