My OS is Ubuntu 22.04.4 LTS. The "pip list" command tells that I have the NumPy 1.21.5. But I need a newer version of the NumPy.
I am trying to install the NumPy package whith next command:
pip install --upgrade numpy==2.1.2
But here is an error:
<...>
Run-time dependency python found: NO (tried pkgconfig, pkgconfig and sysconfig)
../meson.bould:41:12: ERROR: python dependency not found
I find that I must install -dev package. So I do that:
sudo apt install python3-dev
…but the NumPy installation error is still there.
What shall I do to upgrade the NumPy?
4
Answers
The command you’re using has a syntax error. It should be
Notice the
==
instead of=
. This might fix it.pip install <package>
. If you see a newer version on pypi but is not being installed by pip, it’s because your python version does not support that numpy version. Confirm if it is supported for your python version or upgrade your python version.Use a virtual-env first
Your prompt will change accordingly, to indicate you are using the virtual environment. Any next session (either a new terminal, new terminal tab or simply a new login) only requires the
source venv/bin/activate
in that working directory, not thepython -m venv
part.Now install NumPy
and install whatever else you need.
This will install a newer NumPy in that virtual environment; it is shielded from the system Python, so it can’t break your system.
Now do your analysis / coding / whatever you want to do.
Note that I have explicitly used Python 3 here. The
python
command (and similar thepip
command) may refer to Python 2, and that won’t work with newer packages such as NumPy 2. Usingpython3
explicitly will ensure you don’t accidentally use Python 2.Usuing
python3 -m pip
in a virtual environment, is technically not necessary anymore(*), but it’s a good enough habit to be clear what is going on, that I’ll leave it like that.(*) within a virtual environment,
python
andpip
will refer to the Python you used to create that v-venv, which was the system Python 3.What’s most likely going on here is: You are using the system version of both
python
andnumpy
. With "system version" I mean a version installed as anapt
package (so via Ubuntu’s package management) as opposed to being installed as apip
package (so via Python’s default package manager).What is usually not a good idea (and often simply does not work, as you experienced), is trying to update a Python package with
pip
that has been installed withapt
.The better idea would usually be: keep the Python environment that you want to work with and the system’s Python environment separate. This separation can happen on different levels:
python
, but might want to install your own version ofnumpy
. This can be achieved, for example, using a virtual environment, as has been suggested in AKX’s comment. The article linked there is a good deep-dive into the subject and also might provide you with further motivation as to why to choose this approach. The answer of 9769953 describes the necessary steps of setting up a virtual environment and installingnumpy
there.python
version separate from the system version ofpython
. This can be achieved, for example, usinganaconda
/miniconda
/miniforge
. Withconda
and its ecosystem, you have yet another package management tool and repository available, which is also Python-focused, but includes more than Python packages.There are plenty more options starting from there (obligatory xkcd cartoon, which, though being outdated, still captures the essence of the situation), and it may be hard to judge which setup will be best for your needs. For the pros and cons of virtual environments vs.
conda
in particular, you might also want to have a look at the answers to this related question that comparesvirtualenv
(a superset ofvenv
) toconda
.