[Conda] Error: CondaKeyError — key 'pkgs_dirs' not found — How to Fix It

Summary

The CondaKeyError: key 'pkgs_dirs' not found occurs when Conda tries to read a configuration key that’s missing, invalid, or misformatted in your .condarc file. This file controls Conda’s settings such as channel sources, package directories, and environment paths. The fix is to correct or recreate the .condarc configuration using valid YAML syntax.

Context

Conda reads configuration data from a YAML file called .condarc, which defines parameters like channels, pkgs_dirs, and envs_dirs. If this file becomes corrupted, edited incorrectly, or missing required keys, Conda cannot locate its package cache directories and raises a CondaKeyError. This typically happens after manual edits, removal of configuration keys, or upgrading between major Conda versions.

Probable Cause

  • Corrupted or incomplete .condarc configuration file.
  • YAML syntax errors (missing dashes or indentation issues).
  • Manual deletion of pkgs_dirs or envs_dirs keys.
  • Outdated Conda installation referencing deprecated configuration keys.
  • Accidental removal of default paths after running conda config --remove.

Quick Fix

Follow these steps to restore a valid Conda configuration:

  1. Locate your .condarc file:
# Windows
%USERPROFILE%\.condarc

# Linux/macOS
~/.condarc
  1. Open it and ensure it contains properly formatted YAML, for example:
channels:
  - defaults
pkgs_dirs:
  - ~/.conda/pkgs
envs_dirs:
  - ~/.conda/envs
  1. If the file is missing or corrupted, delete it and rebuild it:
# Windows
del %USERPROFILE%\.condarc

# Linux/macOS
rm ~/.condarc
  1. Recreate keys with default paths:
conda config --add pkgs_dirs ~/.conda/pkgs
conda config --add envs_dirs ~/.conda/envs
  1. Check that the configuration loads correctly:
conda config --show pkgs_dirs
conda info

Full Example

Example scenario:

CondaKeyError: key 'pkgs_dirs' not found

Fix process:

del %USERPROFILE%\.condarc
conda config --add pkgs_dirs ~/.conda/pkgs
conda config --add envs_dirs ~/.conda/envs
conda config --show

Output should include both directories listed under pkgs_dirs and envs_dirs. You can now install or update packages normally.

Pitfalls & Debug

  • Symptom: YAML parsing error → Fix: Use spaces only, never tabs.
  • Symptom: “Key not found” persists → Fix: Delete .condarc and rebuild from defaults.
  • Symptom: Missing environment path → Fix: Add envs_dirs manually using conda config --add.
  • Symptom: Conda still crashes after recreation → Fix: Update Conda to the latest version.

Validation & Next Steps

After fixing, verify Conda recognizes valid directories:

conda info | grep -E "pkgs_dirs|envs_dirs"

Then install a small package (e.g., conda install requests) to confirm proper functionality.

Sources

Anaconda Documentation — Conda configuration reference
conda-forge discussions — troubleshooting invalid or missing keys
Stack Overflow — solutions for CondaKeyError and .condarc repair
YAML.org — syntax rules for list-based configuration files

Labels: Tool/Conda, OS/Windows-macOS-Linux, Topic/Configuration-Repair