Welcome to SparkyLinux forums
Zapraszamy również na polsko-języczne Forum

pip cleanup after upgrade

Started by tom44, July 14, 2023, 10:48:58 AM

Previous topic - Next topic


Hello, I did an upgrade of Sparky from 6 to 7. The upgrade went smoothly, but I had some packages installed via pip (pip3), which changed the way it works between Debian 11 and 12. Now it is not possible to install packages with pip install command directly by default, pip packages should be installed within the virtual environment. This is a good idea and should be easy with pipx. However, there are the packages installed with the previous system version. I believe all should reside in my /home, since I never installed anything with sudo, AFAIK. I cannot uninstall these packages - some work, some do not. I would like to remove them all to start over with pipx. If I try to remove anything I get the same error as when trying to install something outside of the virtual environment.
tom@uozp46:~$ pip3 uninstall nbconvert
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
    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.11/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.
hint: See PEP 668 for the detailed specification.
Also, trying to install the same package with pipx may lead to conflicts, for example with symbolic links already in /home/<user>/.local/bin.
What is the "proper" way to clean old pip packages completely and start over? Would it be safe to use the --break-system-packages flag in pip to override the externally-managed-environment for uninstalling everything? Something like this?
pip freeze | xargs pip uninstall -y --break-system-packagesFrom the previous system version I have also installed jupyterlab via pip, which has quite some dependencies and python libraries, so there are lots of packages in pip and some of these are also concurrently installed via apt system wide.

Edit: Later I convinced myself that running the above command should not break anything in the system, as long as I would not issue a password for elevated privileges. So I tried and it seems it did not uninstall anything, the output was full of messages like this:
Not uninstalling zict at /usr/lib/python3/dist-packages, outside environment /usr
Can't uninstall 'zict'. No files were found to uninstall.
Found existing installation: zipp 1.0.0
Not uninstalling zipp at /usr/lib/python3/dist-packages, outside environment /usr
Can't uninstall 'zipp'. No files were found to uninstall.
Also, I noticed that for example the package soundcoverter listed by pip is installed via apt. So I do not know now, does 'pip list' and 'pip freeze' really list only packages installed via pip, or also the same packages installed via apt? I clearly do not understand it at all...

At least I installed the jupyterlab using pipx with the -f flag allowing overwriting the links in the /home/<user>/.local/bin, so it works OK now.

If I knew how, I would delete this post, because the main practical problem of jupyterlab not working is solved and the "cleanup" question is probably based on wrong assumptions..

View the most recent posts on the forum