Search code examples
pythonubuntupippython-poetrypipx

Local Package Development with Poetry on Ubuntu 24.04?


I'm working on a Python project that uses Poetry for dependency management. Recently, I found a bug in a third-party package, so I cloned its repository to work on it locally. My goal is to integrate this local version of the package into my project, but I've run into some issues.

First, I attempted to manually add the package path to my pyproject.toml like this:

[tool.poetry.dependencies]
buggy-one = { path = './buggy-one' }

This does install the package, but not in editable mode, which is not ideal for development.

Next, I tried using pip install -e ./buggy-one to install the package in editable mode. Unfortunately, on newer versions of Ubuntu (like 24.04), this results in the following error:

error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install python3-xyz...

The error message suggests using pipx as an alternative, but this isn't suitable for my needs since pipx installs the package in a separate environment, and I need to work on it within my Poetry-managed project.

Given these challenges, what is the correct approach in 2024 to work on a local package within a Poetry-managed project on Ubuntu? Is there a way to properly link the local package so that I can develop and test it simultaneously wi

New pip message on Ubuntu 24.04

Here the full new message you get if you try to use pip on Ubuntu 24.04:

$ pip install gettext 
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.
    
    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.
    
    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage 
    a virtual environment for you. Make sure you have pipx 
    installed.
    
    See /usr/share/doc/python3.12/README.venv for more information.

note: If you believe this is a mistake, please contact your Python installation or
OS distribution provider. You can override this, at the risk of
breaking your Python installation or OS, by passing 
--break-system-packages.

Solution

  • If you want to install a package under poetry, you must activate the environment on your current shell. This is possible with:

    poetry shell
    

    Then you can simply install whatever package you want.