pythonmacoszshpython-poetryzshrc

When I run `poetry shell` on MacOS with ZSH shell, it gives me the "command not found" error


I'm using Poetry to manage Python dependencies, running on MacOS. I am trying to run the Poetry shell and have the commands work correctly in the shell.

When I run poetry shell I get the following error:

Spawning shell within /Users/r_2009/Workspace/my_project/.venv
/etc/zshrc:7: command not found: locale
/Users/r_2009/.zshrc:7: command not found: brew
/Users/r_2009/.zshrc:source:7: no such file or directory: /nvm.sh
/Users/r_2009/.zshrc:31: command not found: pyenv
/Users/r_2009/.zshrc:37: command not found: register-python-argcomplete

Solution

  • Other people have reported this issue in the GitHub issues for Poetry and the poetry shell command. The explanation is that poetry shell does some work (using pexpect) to detect the shell it is running within.

    If you run into issues with it, you can source the virtualenv activation script directly:

    # local virtual env
    source .venv/bin/activate
    
    # poetry-managed virtual env
    VENV_PATH=`poetry env info | sed -n '/Virtualenv/,/System/p' | grep 'Path:' | awk '{print $2}'`
    source "$VENV_PATH/bin/activate"