Explain Codes LogoExplain Codes Logo

Using Pip to install packages to Anaconda Environment

python
venv
pip
conda
Alex KataevbyAlex Kataev·Nov 10, 2024
TLDR

By activating your target Anaconda environment and with pip inside it, ensures the packages will install inside your environment and not system-wide Python:

conda activate myenv pip install package-name

Activate your environment makes sure that pip installs only into your Anaconda environment, instead it being system-wide.

Installing pip in a conda environment

To segregate your workspace, you can activate the conda environment. This will keep your projects separate, organized and tidy like Marie Kondo's closet.

This ensures pip is installed within the specific environment:

conda activate myenv conda install pip # Marie Kondo approves!

Once pip is installed in the environment, calling it from that environment makes sure you are adding packages in the correct environment:

(myenv) pip install package-name # Adding package in the Marie Kondo's way

Keeping pip in check

Ensuring the pip in use is in the environment

You always want to use the pip specific to your conda environment. It's like making sure you're calling the right sibling:

(myenv) which pip # Ensure it's pointing to the `pip` within the environment's directory.

In Windows:

(myenv) where pip # Ensure it's pointing to the `pip` within the environment's directory.

Be aware of PYTHONPATH interference:

unset PYTHONPATH # Shoo! PYTHONPATH

Maintaining harmony between pip and python

Reconfirm that pip installs packages for the Anaconda's Python installation by counter checking the paths:

(myenv) which -a python (myenv) which -a pip # Paths, assemble!

Install packages in Jupyter notebooks

Note: If you're in a Jupyter notebook, use the below to install packages directly, like how you directly get your coffee beans from the farm:

import sys !{sys.executable} -m pip install package-name # Farm-fresh installations

Handling sensitive packages

Sensitive packages like Pillow sometimes prefers pip over conda due to potential compilation benefits:

(myenv) pip install Pillow # Pillow prefers pip alright.

Package installation checks

To check where an installation was set up:

(myenv) pip show package-name # Where did you move in, package?

Aesthetic Environment-specific scripts and shell functions

Garnish the use of packages with environment-specific shell functions or scripts. This enables better condo environment's pip:

alias myenvpip="conda activate myenv && pip" # Stylin' with alias!

One-liner way to download a package:

myenvpip install package-name # Easy-peasy-lemon-squeezy

Official documentation - the guiding light

Keep the official documentation for conda and pip as your North Star for a smooth sailing.

Working with different operating systems

  • On Linux and macOS for older versions, use source activate myenv.
  • For Windows for better environment management, use Anaconda Prompt.
  • Take care of slashes in paths as they can differ between UNIX and Windows-based systems.

Resolving dependencies and conflicts

  • Inspect for conflicting packages with pip check.
  • Rely on conda for complex dependencies.
  • For pip only packages, manage versions with a requirements file.