Bird
Raised Fist0
MLOpsdevops~10 mins

Environment management with conda and pip in MLOps - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Environment management with conda and pip
Start: Need new environment
Create conda env
Activate env
Install packages with conda
If package missing
Yes
Install package with pip
Use environment
Deactivate env
Delete env if needed
End
This flow shows how to create and manage a Python environment using conda, install packages with conda or pip, then use and clean up the environment.
Execution Sample
MLOps
conda create -n myenv python=3.12
conda activate myenv
conda install numpy
pip install requests
conda deactivate
Create a conda environment, activate it, install numpy with conda, install requests with pip, then deactivate the environment.
Process Table
StepCommandActionResult/Output
1conda create -n myenv python=3.12Create environment named myenv with Python 3.12Environment 'myenv' created with Python 3.12
2conda activate myenvActivate the environment myenvPrompt changes to (myenv), environment active
3conda install numpyInstall numpy package using condanumpy installed successfully
4pip install requestsInstall requests package using pip inside conda envrequests installed successfully
5conda deactivateDeactivate the current environmentPrompt returns to base, environment deactivated
6conda env listList all conda environmentsShows 'myenv' and 'base' environments
7conda remove -n myenv --allDelete the environment myenvEnvironment 'myenv' deleted successfully
💡 Environment deleted or deactivated, no active conda environment
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 7
Active Environmentbasebasemyenvmyenvmyenvbasebase
Installed Packagesbase packagesbase packagesbase packagesbase + numpybase + numpy + requestsbase packagesbase packages
Key Moments - 3 Insights
Why do we sometimes use pip inside a conda environment instead of conda?
Some packages are not available in conda repositories, so pip is used inside the active conda environment to install those packages, as shown in step 4 of the execution_table.
What happens if you try to install packages without activating the conda environment?
Packages will install in the base environment or system Python, not the intended environment. Step 2 shows activation is needed before installing packages in 'myenv'.
Why do we deactivate the environment after use?
Deactivating returns you to the base environment or system Python, preventing accidental package installs or runs in the wrong environment, as shown in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the active environment after step 3?
ANone
Bbase
Cmyenv
Droot
💡 Hint
Check the 'Active Environment' row in variable_tracker after step 3
At which step does the environment become inactive?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look at the 'Active Environment' variable in variable_tracker and the command in execution_table
If you skip step 2 and run 'conda install numpy', where will numpy be installed?
AIn the base environment
BIn the 'myenv' environment
CNowhere, command fails
DIn a new environment
💡 Hint
Refer to key_moments about activating environment before installing packages
Concept Snapshot
conda create -n envname python=3.12  # create env
conda activate envname                # activate env
conda install package                  # install with conda
pip install package                    # install with pip if missing
conda deactivate                      # leave env
conda remove -n envname --all         # delete env
Use conda envs to isolate projects and avoid conflicts.
Full Transcript
This lesson shows how to manage Python environments using conda and pip. First, you create a new environment with a specific Python version using 'conda create'. Then you activate it with 'conda activate' to work inside it. You install packages preferably with 'conda install', but if a package is missing, you use 'pip install' inside the active environment. After finishing work, you deactivate the environment with 'conda deactivate' to return to the base system. You can list environments with 'conda env list' and delete an environment with 'conda remove -n envname --all'. Activating the environment before installing packages ensures they go to the right place. Deactivating prevents accidental installs in the wrong environment. This process helps keep projects clean and dependencies separate.

Practice

(1/5)
1. What is the main purpose of using conda create -n myenv python=3.8?
easy
A. To delete the environment named 'myenv' and install Python 3.8 globally
B. To update Python to version 3.8 in the current environment
C. To create a new isolated environment named 'myenv' with Python 3.8 installed
D. To install all packages listed in a file named 'myenv' with Python 3.8

Solution

  1. Step 1: Understand the conda create command

    This command is used to create a new environment in conda, isolating packages and Python versions.
  2. Step 2: Analyze the flags and arguments

    The -n myenv specifies the environment name, and python=3.8 sets the Python version inside it.
  3. Final Answer:

    To create a new isolated environment named 'myenv' with Python 3.8 installed -> Option C
  4. Quick Check:

    conda create -n myenv python=3.8 = D [OK]
Hint: Remember: 'conda create -n' makes new isolated envs [OK]
Common Mistakes:
  • Confusing 'create' with 'install' or 'update'
  • Thinking it affects the global Python installation
  • Misunderstanding the '-n' flag as package name
2. Which of the following commands correctly activates a conda environment named dataenv?
easy
A. activate conda dataenv
B. conda activate dataenv
C. conda start dataenv
D. source deactivate dataenv

Solution

  1. Step 1: Recall the syntax to activate conda environments

    The correct command to activate an environment is conda activate <env_name>.
  2. Step 2: Check each option

    conda activate dataenv matches the correct syntax. Options B, C, and D use incorrect command order or wrong commands.
  3. Final Answer:

    conda activate dataenv -> Option B
  4. Quick Check:

    Activate env = conda activate env_name [OK]
Hint: Use 'conda activate env_name' to switch environments [OK]
Common Mistakes:
  • Using 'activate conda' instead of 'conda activate'
  • Confusing 'source deactivate' with activation
  • Trying 'conda start' which is invalid
3. Given the following commands run in order:
conda create -n testenv python=3.9 -y
conda activate testenv
pip install numpy
pip list | grep numpy

What will be the output of the last command?
medium
A. numpy with its installed version number
B. No output because pip list does not work inside conda
C. Error: 'pip' command not found
D. List of all packages except numpy

Solution

  1. Step 1: Understand environment creation and activation

    The environment 'testenv' is created with Python 3.9 and then activated, so all commands run inside it.
  2. Step 2: Installing numpy with pip inside the active environment

    Running pip install numpy installs numpy in 'testenv'. The pip list | grep numpy command will show numpy and its version.
  3. Final Answer:

    numpy with its installed version number -> Option A
  4. Quick Check:

    pip install inside active env = numpy listed [OK]
Hint: pip installs packages in active conda env, visible with pip list [OK]
Common Mistakes:
  • Thinking pip installs globally ignoring conda env
  • Assuming pip commands fail inside conda
  • Expecting no output from pip list
4. You run conda activate myenv but get the error: CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'. What is the most likely fix?
medium
A. Reinstall Python globally on your system
B. Deactivate any active environment before activating 'myenv'
C. Use pip install conda to fix the error
D. Run conda init to configure your shell, then restart the terminal

Solution

  1. Step 1: Understand the error cause

    This error means the shell does not know how to run conda activate because it lacks proper initialization.
  2. Step 2: Apply the fix by initializing conda for the shell

    Running conda init sets up the shell scripts needed. Restarting the terminal applies changes.
  3. Final Answer:

    Run conda init to configure your shell, then restart the terminal -> Option D
  4. Quick Check:

    Shell config for conda = conda init + restart [OK]
Hint: Run 'conda init' once after install, then restart terminal [OK]
Common Mistakes:
  • Trying to reinstall Python instead of fixing shell config
  • Using pip to install conda which is incorrect
  • Ignoring the need to restart terminal after init
5. You want to create a reproducible environment for a project using conda and pip. Which sequence of commands correctly creates an environment, installs packages from a requirements.txt file using pip, and exports the environment including pip packages?
hard
A. conda create -n projenv python=3.10 -y && conda activate projenv && pip install -r requirements.txt && conda env export > environment.yml
B. conda create -n projenv python=3.10 -y && pip install -r requirements.txt && conda activate projenv && conda env export > environment.yml
C. conda create -n projenv python=3.10 -y && conda activate projenv && pip install -r requirements.txt && conda env export --from-history > environment.yml
D. conda activate projenv && conda create -n projenv python=3.10 -y && pip install -r requirements.txt && conda env export > environment.yml

Solution

  1. Step 1: Create and activate the environment before installing packages

    You must first create the environment, then activate it to install packages inside it.
  2. Step 2: Install pip packages and export full environment

    After activation, install packages from requirements.txt using pip. Then export the full environment including pip packages with conda env export.
  3. Final Answer:

    conda create -n projenv python=3.10 -y && conda activate projenv && pip install -r requirements.txt && conda env export > environment.yml -> Option A
  4. Quick Check:

    Create, activate, pip install, export full env = C [OK]
Hint: Activate env before pip install; export full env to include pip packages [OK]
Common Mistakes:
  • Installing pip packages before activating environment
  • Using --from-history which excludes pip packages
  • Activating environment after installing packages