python-poetry

How to force reinstall Poetry environment from scratch?


Poetry has some stale dependencies because the use of develop = true packages. Poetry cannot figure out on its own that dependencies have been updated. How do I force Poetry to reinstall everything in its virtualenv to work around this issue?


Solution

  • These instructions are only for Linux/macOS for Windows Subsystem for Linux. For the Microsoft Windows shell, please use your own command-line knowledge to apply these instructions.

    Recreating Poetry environment

    To reinstall the packages for the Poetry environment in the current working directory (UNIX shell):

    # Enter the current Poetry environment
    poetry shell
    
    # Remove the current environment
    # as referred by path to Python interpreter 
    poetry env remove $(which python)
    
    # Reinstall from Poetry's cache
    poetry install
    

    Recreating Poetry environment manually

    Do the following in the folder with pyproject.toml:

    # Remove all the files of the current environment of the folder we are in
    POETRY_LOCATION=`poetry env info -p` 
    echo "Poetry is $POETRY_LOCATION"
    rm -rf "$POETRY_LOCATION"
    
    # Stop the current virtualenv if active or alternative use
    # `exit` to exit from a Poetry shell session
    deactivate
    
    # Reactivate Poetry shell
    poetry shell
    
    # Install everything
    poetry install
    
    

    Recreating Poetry environment with different Python version

    Poetry may refer to your installed Python version, so you might tell it to change its link to your python interpreter as well:

    # Make Poetry to use python 3.9 from Homebrew, installed earlier
    poetry env use `which python3.9`
    poetry shell
    python -V
    
    Python 3.9.9
    

    Kudos to this tip about removing the virtualenv.

    Fixing damaged poetry command

    If the poetry command itself is damaged and no longer runs, you can reinstall Poetry by:

    which poetry
    
    /Users/mikkoohtamaa/.poetry/bin/poetry
    

    Then remove this and install:

    # macOS
    rm -rf /Users/mikkoohtamaa/.poetry
    rm -rf  "/Users/$USER/Library/Application Support/pypoetry/venv"
    
    
    # Linux
    rm -rf ~/.local/share/pypoetry/
    
    # Download and run Poetry install script
    # If your curl is messed up you can try with --insecure flag
    curl -sSL https://install.python-poetry.org/ | python3 - --force
    

    Extreme painful issues

    Sometimes Poetry may just fail to install after you have upgraded your Python e.g. with Homebrew. This might be to do with a fact who Poetry determines which Python version to use.

    Errors you can see may include:

    cat /Users/moo/poetry-installer-error-t0zfi7kb.log
    
    dyld[6526]: Library not loaded: @loader_path/../Python
      Referenced from: <7295E559-55D4-3ACA-9820-D95D1F4AE151> /Users/moo/Library/Application Support/pypoetry/venv/bin/python3.10
      Reason: tried: '/Users/moo/Library/Application Support/pypoetry/venv/bin/../Python' (no such file), '/System/Volumes/Preboot/Cryptexes/OS@loader_path/../Python' (no such file), '/Users/moo/Library/Application Support/pypoetry/venv/bin/../Python' (no such file), '/usr/local/lib/Python' (no such file), '/usr/lib/Python' (no such file, not in dyld cache)
    
    Traceback:
    
      File "<stdin>", line 923, in main
      File "<stdin>", line 562, in run
    

    In this case, recommended approach is to switch away from Homebrew based Pythons and start using pyenv:

    brew install pyenv
    
    # Follow the instructions
    pyenv init 
    

    Then in a new shell install a specific Python version and check it is being used:

    pyenv install 3.10
    pyenv global 3.10
    which python
    

    Points to pyenv:

    /Users/moo/.pyenv/shims/python
    

    Now reinstall Poetry using a specific Python version and it should work:

    curl -sSL https://install.python-poetry.org/ | python3.10 -