zsh: command not found: virtualenv
Published ยท Updated
zsh: command not found: virtualenv means the virtualenv command is not in
any directory on your PATH. virtualenv is a third-party package; Python
does not include it. Python 3 does include venv, which covers what most
people need:
python3 -m venv .venv
source .venv/bin/activate
python --version
Inside the activated environment python and pip point at the project's
copy. Run deactivate to leave. If that is all you wanted, you are done.
zsh: command not found: python
macOS has no python command; Apple removed Python 2 in macOS 12.3 and never
added a python alias for Python 3. Use python3. Inside an activated venv,
python works again because the venv provides it.
When you really need virtualenv
Some tools and older guides call virtualenv directly, and it has features
venv lacks, such as creating environments for other Python versions. How
to install it depends on which Python you have.
Homebrew Python: pip refuses with externally-managed-environment
pip3 install virtualenv
# error: externally-managed-environment
Homebrew marks its Python as managed by Homebrew (PEP 668), so pip will not
install packages into it. Do not add --break-system-packages. Install the
command with Homebrew or pipx instead:
brew install virtualenv
# or
brew install pipx
pipx ensurepath
pipx install virtualenv
pipx ensurepath adds ~/.local/bin to your shell startup files. Open a new
terminal tab afterwards.
Apple's python3: the script is installed in ~/Library/Python
The python3 that comes with the Xcode Command Line Tools lets you install
with pip3 install --user virtualenv. pip then prints a warning that is easy
to miss:
WARNING: The script virtualenv is installed in
'/Users/you/Library/Python/3.9/bin' which is not on PATH.
That folder is the reason zsh cannot find the command. Either call it through Python, which needs no PATH change:
python3 -m virtualenv .venv
or add the folder from the warning to ~/.zprofile, using the version number
pip printed:
export PATH="$HOME/Library/Python/3.9/bin:$PATH"
To find the folder without the warning, run
python3 -m site --user-base and add /bin.
pyenv virtualenv and mkvirtualenv
Two similar errors come from add-ons rather than from virtualenv itself:
pyenv: no such command `virtualenv'means the pyenv-virtualenv plugin is not installed or not loaded. Install it withbrew install pyenv-virtualenvand addeval "$(pyenv virtualenv-init -)"to~/.zshrcafter thepyenv initline.zsh: command not found: mkvirtualenvcomes from virtualenvwrapper. Its commands are shell functions, so installing the package is not enough: your~/.zshrcmustsourcethevirtualenvwrapper.shscript, as its documentation describes.
Check where things are
command -v python3 pip3 virtualenv pipx
print -l $path
If a tool is installed but its folder is missing from the list, the fix is a
PATH line in the right startup file.
zsh: command not found on macOS: how PATH works
covers which file to edit and why.
Sources
related.
Ruslan Osipov
About the author