HudsonMoore
Posts: 10
Joined: Mon Apr 20, 2020 3:29 pm

install python packages via apt or pip?

Sat Aug 08, 2020 1:47 pm

I'm new to the raspberry pi and wondering what are the best practices for installing python packages, for example, NumPy. Are there any drawbacks to using pip vs. apt?

The version of NumPy available through apt is 1.16.2, approximately a year and a half old. NumPy current release is 1.19.0. I don't need the very latest release but there were changes in 1.17 that I'd like to use.

If it's OK to use pip but I've previously installed python-numpy with apt, should I uninstall that before using pip?
RPi 4B Buster

theodore_dream
Posts: 3
Joined: Wed Jul 15, 2020 2:46 am

Re: install python packages via apt or pip?

Sat Aug 08, 2020 7:35 pm

Hello,

Basically, pip is specialized for python packages/modules type of stuff. This is not for OS wide packages, only python specific packages. In general most OS packages on Raspbian you'll be installing with apt. But pip is also important for Python specific packages.

Here's a relevant thread: https://askubuntu.com/questions/431780/ ... ip-install

I think you should probably uninstall one version before installing another version of the same package. Honestly not sure about if that would cause any error or not, here's just some general notes.

========

PyPI is the Python Package index — repository of python modules.

pip is used to download and install packages directly from PyPI. PyPI is hosted by Python Software Foundation. It is a specialized package manager that only deals with python packages.

apt-get is used to download and install packages from Ubuntu repositories which are hosted by Canonical.

=======

I'll add to this that "apt" is the Debian package management tool.

I would advise you to take a read of the man page and try some different commands, specifically you can use:

# pip show <package>
# apt show <package>

https://manpages.debian.org/stretch/apt/apt.8.en.html
https://manpages.debian.org/stretch/python-pip/pip.1

The above commands to inspect packages should give you more helpful information about the package version and source.

Hope this is helpful

hippy
Posts: 7911
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: install python packages via apt or pip?

Sat Aug 08, 2020 7:41 pm

I have tended to prefer 'apt' installs over 'pip' because 'pip' could be a nightmare at times but things do seem to have improved.

Note it will be 'pip3' and likely 'apt install python3-" if using Python 3.

I would say yes; 'apt purge' installed Python packages before installing with 'pip'.

sora03
Posts: 275
Joined: Mon Dec 29, 2014 4:11 pm
Location: Philippines
Contact: Website YouTube

Re: install python packages via apt or pip?

Sun Aug 09, 2020 4:14 am

I prefer installing python packages via pip as they are newer than of the apt package versions. But they are not installed systemwide, as far as I know the current user will benefit from the pip installs.
Mastodon: https://mastodon.social/@ssora

hippy
Posts: 7911
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: install python packages via apt or pip?

Sun Aug 09, 2020 11:22 am

'sudo pip install' and 'sudo pip3 install' will, I believe, install packages and make them available system-wide.

The risk with installing any 'pip' package is that its setup.py can cause malicious or inadvertent damage during installation, and a 'sudo' will allow it to do even more damage.

There are efforts under way to make 'pip' more understandable and better. As part of that, Bernard Tyers has reached out to Raspberry Pi 'pip' and Python users here -

viewtopic.php?t=282153

HudsonMoore
Posts: 10
Joined: Mon Apr 20, 2020 3:29 pm

Re: install python packages via apt or pip?

Mon Aug 10, 2020 10:38 am

Thanks, all, for the good advice. I've used pip/pip3 for several years on other machines and avoided dependency trouble.

Sounds like no real advantage to using apt for installing python packages? If not, I'd rather use pip3 to be able to get more recent releases.
RPi 4B Buster

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: install python packages via apt or pip?

Mon Aug 10, 2020 11:42 am

HudsonMoore wrote:
Mon Aug 10, 2020 10:38 am
Thanks, all, for the good advice. I've used pip/pip3 for several years on other machines and avoided dependency trouble.

Sounds like no real advantage to using apt for installing python packages? If not, I'd rather use pip3 to be able to get more recent releases.
If you install with apt then update with pip3 there's a risk that a package could get regressed on a future apt update/apt dist-upgrade command. I'd prefer it if no python packages (other than the absolute essentials) came with apt and I had to use pip3 to install everything. But that's not possible when the RaspiOS folks are trying to give the novice users something that works from the first boot up on their new RPi. The other solution would be for pip3 to have an option to deliver an *.deb package that would then work with apt.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

hippy
Posts: 7911
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: install python packages via apt or pip?

Mon Aug 10, 2020 11:56 am

HudsonMoore wrote:
Mon Aug 10, 2020 10:38 am
Sounds like no real advantage to using apt for installing python packages? If not, I'd rather use pip3 to be able to get more recent releases.
One disadvantage I could imagine from using 'pip3' to get the latest version is where that latest requires a Python version later than what is installed.

I have no idea how that would be handled; whether it would get the latest version for the Python you have if available, say it's not suitable for what you have, or install it and fail when run.

I don't however think that would be a reason not to use 'pip3', and most likely an unusual situation to occur. Though more likely as people migrate to Python 3.8 and 3.9 while the Pi remains on 3.7

plugwash
Forum Moderator
Forum Moderator
Posts: 3630
Joined: Wed Dec 28, 2011 11:45 pm

Re: install python packages via apt or pip?

Mon Aug 10, 2020 12:32 pm

DougieLawson wrote:
Mon Aug 10, 2020 11:42 am
If you install with apt then update with pip3 there's a risk that a package could get regressed on a future apt update/apt dist-upgrade command.
IIRC debian python packages install to "dist-packages" while pip installs to "site-packages", so they won't directly overwrite each other.

plugwash
Forum Moderator
Forum Moderator
Posts: 3630
Joined: Wed Dec 28, 2011 11:45 pm

Re: install python packages via apt or pip?

Mon Aug 10, 2020 12:36 pm

The main risk with using pip in a system wide or user context is that upgrading one package to a newer version than that shipped by Debian/Raspbian can break other packages. How great this risk is depends heavilly on the particular package, some are good at keeping backwards compatibility others aren't. I belive numpy is on the good side but I wouldn't proffess to be an expert.

One alternative is to use "virtual environment", within such an environment you can safely use pip without any risk of breaking things outside that environment.

https://docs.python.org/3/tutorial/venv.html

(there is also the seperate virtualenv package which is older and apparently has more features than the vitual environment support in the standard library)

hippy
Posts: 7911
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: install python packages via apt or pip?

Mon Aug 10, 2020 1:15 pm

plugwash wrote:
Mon Aug 10, 2020 12:32 pm
DougieLawson wrote:
Mon Aug 10, 2020 11:42 am
If you install with apt then update with pip3 there's a risk that a package could get regressed on a future apt update/apt dist-upgrade command.
IIRC debian python packages install to "dist-packages" while pip installs to "site-packages", so they won't directly overwrite each other.
I'm no expert on 'pip' but it seems plain 'pip3' will install to -

/home/pi/.local/lib/python3.7/site-packages

while 'sudo pip3' will install to -

/usr/local/lib/python3.7/dist-packages

That is where issues of 'apt' regressing 'pip3' likely comes from. That 'no one really knows what happens with any certainty' is probably why the recommendation is to use one or the other but not both, in general, and particularly per-package.

Return to “Beginners”