How to install numpy python

How to install numpy python

Installing NumPy

CONDA

PIP

Python and NumPy installation guide

Installing and managing packages in Python is complicated, there are a number of alternative solutions for most tasks. This guide tries to give the reader a sense of the best (or most popular) solutions, and give clear recommendations. It focuses on users of Python, NumPy, and the PyData (or numerical computing) stack on common operating systems and hardware.

Recommendations

We’ll start with recommendations based on the user’s experience level and operating system of interest. If you’re in between “beginning” and “advanced”, please go with “beginning” if you want to keep things simple, and with “advanced” if you want to work according to best practices that go a longer way in the future.

Beginning users

On all of Windows, macOS, and Linux:

Advanced users

Conda

Alternative if you prefer pip/PyPI

For users who know, from personal preference or reading about the main differences between conda and pip below, they prefer a pip/PyPI-based solution, we recommend:

Python package management

Managing packages is a challenging problem, and, as a result, there are lots of tools. For web and general purpose Python development there’s a whole host of tools complementary with pip. For high-performance computing (HPC), Spack is worth considering. For most NumPy users though, conda and pip are the two most popular tools.

Pip & conda

The first difference is that conda is cross-language and it can install Python, while pip is installed for a particular Python on your system and installs other packages to that same Python install only. This also means conda can install non-Python libraries and tools you may need (e.g. compilers, CUDA, HDF5), while pip can’t.

The second difference is that pip installs from the Python Packaging Index (PyPI), while conda installs from its own channels (typically “defaults” or “conda-forge”). PyPI is the largest collection of packages by far, however, all popular packages are available for conda as well.

The third difference is that conda is an integrated solution for managing packages, dependencies and environments, while with pip you may need another tool (there are many!) for dealing with environments or complex dependencies.

Reproducible installs

As libraries get updated, results from running your code can change, or your code can break completely. It’s important to be able to reconstruct the set of packages and versions you’re using. Best practice is to:

NumPy packages & accelerated linear algebra libraries

The NumPy wheels on PyPI, which is what pip installs, are built with OpenBLAS. The OpenBLAS libraries are included in the wheel. This makes the wheel larger, and if a user installs (for example) SciPy as well, they will now have two copies of OpenBLAS on disk.

In the conda defaults channel, NumPy is built against Intel MKL. MKL is a separate package that will be installed in the users’ environment when they install NumPy.

The MKL package is a lot larger than OpenBLAS, it’s about 700 MB on disk while OpenBLAS is about 30 MB.

MKL is typically a little faster and more robust than OpenBLAS.

Besides install sizes, performance and robustness, there are two more things to consider:

Troubleshooting

If your installation fails with the message below, see Troubleshooting ImportError.

Building from source#

There are two options for building NumPy- building with Gitpod or locally from source. Your choice depends on your operating system and familiarity with the command line.

Gitpod#

Gitpod is an open-source platform that automatically creates the correct development environment right in your browser, reducing the need to install local development environments and deal with incompatible dependencies.

If you are a Windows user, unfamiliar with using the command line or building NumPy for the first time, it is often faster to build with Gitpod. Here are the in-depth instructions for building NumPy with building NumPy with Gitpod.

Building locally#

Building locally on your machine gives you more granular control. If you are a MacOS or Linux user familiar with using the command line, you can continue with building NumPy locally by following the instructions below.

Prerequisites#

Building NumPy requires the following software installed:

Python 3.8.x or newer

Please note that the Python development headers also need to be installed, e.g., on Debian/Ubuntu one needs to install both python3 and python3-dev. On Windows and macOS this is normally not an issue.

Much of NumPy is written in C. You will need a C compiler that complies with the C99 standard.

Part of Numpy is now written in C++. You will also need a C++ compiler that complies with the C++11 standard.

While a FORTRAN 77 compiler is not necessary for building NumPy, it is needed to run the numpy.f2py tests. These tests are skipped if the compiler is not auto-detected.

Note that NumPy is developed mainly using GNU compilers and tested on MSVC and Clang compilers. Compilers from other vendors such as Intel, Absoft, Sun, NAG, Compaq, Vast, Portland, Lahey, HP, IBM are only supported in the form of community feedback, and may not work out of the box. GCC 4.x (and later) compilers are recommended. On ARM64 (aarch64) GCC 8.x (and later) are recommended.

Linear Algebra libraries

For building NumPy, you’ll need a recent version of Cython.

Basic Installation#

To install NumPy, run:

To perform an in-place build that can be run from the source folder run:

Testing#

Make sure to test your builds. To ensure everything stays in shape, see if all tests pass.

The test suite requires additional dependencies, which can easily be installed with:

Parallel builds#

It’s possible to do a parallel build with:

This will compile numpy on 4 CPUs and install it into the specified prefix. to perform a parallel in-place build, run:

Choosing the fortran compiler#

For more information see:

How to check the ABI of BLAS/LAPACK libraries#

One relatively simple and reliable way to check for the compiler used to build a library is to use ldd on the library. If libg2c.so is a dependency, this means that g77 has been used (note: g77 is no longer supported for building NumPy). If libgfortran.so is a dependency, gfortran has been used. If both are dependencies, this means both have been used, which is almost always a very bad idea.

Accelerated BLAS/LAPACK libraries#

NumPy searches for optimized linear algebra libraries such as BLAS and LAPACK. There are specific orders for searching these libraries, as described below and in the site.cfg.example file.

Note that both BLAS and CBLAS interfaces are needed for a properly optimized build of NumPy.

The default order for the libraries are:

If you wish to build against OpenBLAS but you also have BLIS available one may predefine the order of searching via the environment variable NPY_BLAS_ORDER which is a comma-separated list of the above names which is used to determine what to search for, for instance:

will prefer to use ATLAS, then BLIS, then OpenBLAS and as a last resort MKL. If neither of these exists the build will fail (names are compared lower case).

will allow using anything but NetLIB BLAS and ATLAS libraries, the order of the above list is retained.

One cannot mix negation and positives, nor have multiple negations, such cases will raise an error.

LAPACK#

The default order for the libraries are:

If you wish to build against OpenBLAS but you also have MKL available one may predefine the order of searching via the environment variable NPY_LAPACK_ORDER which is a comma-separated list of the above names, for instance:

will prefer to use ATLAS, then OpenBLAS and as a last resort MKL. If neither of these exists the build will fail (names are compared lower case).

will allow using anything but the NetLIB LAPACK library, the order of the above list is retained.

One cannot mix negation and positives, nor have multiple negations, such cases will raise an error.

Deprecated since version 1.20: The native libraries on macOS, provided by Accelerate, are not fit for use in NumPy since they have bugs that cause wrong output under easily reproducible conditions. If the vendor fixes those bugs, the library could be reinstated, but until then users compiling for themselves should use another linear algebra library or use the built-in (but slower) default, see the next section.

Disabling ATLAS and other accelerated libraries#

Usage of ATLAS and other accelerated libraries in NumPy can be disabled via:

64-bit BLAS and LAPACK#

You can tell Numpy to use 64-bit BLAS/LAPACK libraries by setting the environment variable:

when building Numpy. The following 64-bit BLAS/LAPACK libraries are supported:

OpenBLAS ILP64 with 64_ symbol suffix ( openblas64_ )

OpenBLAS ILP64 without symbol suffix ( openblas_ilp64 )

Using non-symbol-suffixed 64-bit BLAS/LAPACK in a program that also uses 32-bit BLAS/LAPACK can cause crashes under certain conditions (e.g. with embedded Python interpreters on Linux).

The 64-bit OpenBLAS with 64_ symbol suffix is obtained by compiling OpenBLAS with settings:

The symbol suffix avoids the symbol name clashes between 32-bit and 64-bit BLAS/LAPACK libraries.

Supplying additional compiler flags#

Cross compilation#

Although numpy.distutils and setuptools do not directly support cross compilation, it is possible to build NumPy on one system for different architectures with minor modifications to the build environment. This may be desirable, for example, to use the power of a high-performance desktop to create a NumPy package for a low-power, single-board computer. Because the setup.py scripts are unaware of cross-compilation environments and tend to make decisions based on the environment detected on the build system, it is best to compile for the same type of operating system that runs on the builder. Attempting to compile a Mac version of NumPy on Windows, for example, is likely to be met with challenges not considered here.

When building and installing NumPy for a host system, the CC environment variable must provide the path the cross compiler that will be used to build NumPy C extensions. It may also be necessary to set the LDSHARED environment variable to the path to the linker that can link compiled objects for the host system. The compiler must be told where it can find Python libraries and development headers. On Unix-like systems, this generally requires adding, e.g., the following parameters to the CFLAGS environment variable:

for Python version 3.y. (Replace the “y” in this path with the actual minor number of the installed Python runtime.) Likewise, the linker should be told where to find host libraries by adding a parameter to the LDFLAGS environment variable:

To make sure Python-specific system configuration options are provided for the intended host and not the build system, set:

When using external linear algebra libraries, include and library directories should be provided for the desired libraries in site.cfg as described above and in the comments of the site.cfg.example file included in the NumPy repository or sdist. In this example, set:

under appropriate sections of the file to allow numpy.distutils to find the libraries.

As of NumPy 1.22.0, a vendored copy of SVML will be built on x86_64 Linux hosts to provide AVX-512 acceleration of floating-point operations. When using an x86_64 Linux build system to cross compile NumPy for hosts other than x86_64 Linux, set the environment variable NPY_DISABLE_SVML to prevent the NumPy build script from incorrectly attempting to cross-compile this platform-specific library:

With the environment configured, NumPy may be built as it is natively:

When the wheel package is available, the cross-compiled package may be packed into a wheel for installation on the host with:

It may be possible to use pip to build a wheel, but pip configures its own environment; adapting the pip environment to cross-compilation is beyond the scope of this guide.

The cross-compiled package may also be installed into the host prefix for cross-compilation of other packages using, e.g., the command:

When cross compiling other packages that depend on NumPy, the host npy-pkg-config file must be made available. For further discussion, refer to numpy distutils documentation.

How to Install NumPy

Home » SysAdmin » How to Install NumPy

NumPy (Numerical Python) is an open-source library for the Python programming language. It is used for scientific computing and working with arrays.

Apart from its multidimensional array object, it also provides high-level functioning tools for working with arrays.

In this tutorial, you will learn how to install NumPy.

How to install numpy python. Смотреть фото How to install numpy python. Смотреть картинку How to install numpy python. Картинка про How to install numpy python. Фото How to install numpy python

Prerequisites

Installing NumPy

You can follow the steps outlined below and use the commands on most Linux, Mac, or Windows systems. Any irregularities in commands are noted along with instructions on how to modify them to your needs.

Step 1: Check Python Version

Before you can install NumPy, you need to know which Python version you have. This programming language comes preinstalled on most operating systems (except Windows; you will need to install Python on Windows manually).

Most likely, you have Python 2 or Python 3 installed, or even both versions.

To check whether you have Python 2, run the command:

The output should give you a version number.

To see if you have Python 3 on your system, enter the following in the terminal window:

In the example below, you can see both versions of Python are present.

How to install numpy python. Смотреть фото How to install numpy python. Смотреть картинку How to install numpy python. Картинка про How to install numpy python. Фото How to install numpy python

If these commands do not work on your system, take a look at this article on How To Check Python Version In Linux, Mac, & Windows.

Note: If you need help installing a newer version of Python, refer to one of our installation guides – How to Install Python on CentOS 8, How to Install Python 3.7 on Ubuntu, or How to Install Python 3 on Windows.

Step 2: Install Pip

The easiest way to install NumPy is by using Pip. Pip a package manager for installing and managing Python software packages.

Unlike Python, Pip does not come preinstalled on most operating systems. Therefore, you need to set up the package manager that corresponds to the version of Python you have. If you have both versions of Python, install both Pip versions as well.

The commands below use the apt utility as we are installing on Ubuntu for the purposes of this article.

Install Pip (for Python 2) by running:

If you need Pip for Python 3, use the command:

Important: Depending on the operating system you are using, follow the instructions in one of our Pip installation guides:

How to install numpy python. Смотреть фото How to install numpy python. Смотреть картинку How to install numpy python. Картинка про How to install numpy python. Фото How to install numpy python

Step 3: Install NumPy

With Pip set up, you can use its command line for installing NumPy.

Install NumPy with Python 2 by typing:

Pip downloads the NumPy package and notifies you it has been successfully installed.

How to install numpy python. Смотреть фото How to install numpy python. Смотреть картинку How to install numpy python. Картинка про How to install numpy python. Фото How to install numpy python

To install NumPy with the package manager for Python 3, run:

As this is a newer version of Python, the Numpy version also differs as you can see in the image below.

How to install numpy python. Смотреть фото How to install numpy python. Смотреть картинку How to install numpy python. Картинка про How to install numpy python. Фото How to install numpy python

Step 4: Verify NumPy Installation

Use the show command to verify whether NumPy is now part of you Python packages:

And for Pip3 type:

The output should confirm you have NumPy, which version you are using, as well as where the package is stored.

Step 5: Import the NumPy Package

After installing NumPy you can import the package and set an alias for it.

To do so, move to the python prompt by typing one of the following commands:

Once you are in the python or python3 prompt you can import the new package and add an alias for it (in the example below it is np ):

Upgrading NumPy

If you already have NumPy and want to upgrade to the latest version, for Pip2 use the command:

If using Pip3, run the following command:

By following this guide, you should have successfully installed NumPy on your system.

Check out our introduction tutorial on Python Pandas, an open-source Python library primarily used for data analysis, which is built on top of the NumPy package and is compatible with a wide array of existing modules. The collection of tools in the Pandas package is an essential resource for preparing, transforming, and aggregating data in Python.

For more Python package tutorials, check out our other KB articles such as Best Python IDEs and more!

Building and installing NumPyВ¶

Binary installersВ¶

In most use cases the best way to install NumPy on your system is by using an installable binary package for your operating system.

WindowsВ¶

Good solutions for Windows are, Enthought Canopy, Anaconda (which both provide binary installers for Windows, OS X and Linux) and Python (x, y). Both of these packages include Python, NumPy and many additional packages.

The NumPy installer includes binaries for different CPU’s (without SSE instructions, with SSE2 or with SSE3) and installs the correct one automatically. If needed, this can be bypassed from the command line with

LinuxВ¶

All major distributions provide packages for NumPy. These are usually reasonably up-to-date, but sometimes lag behind the most recent NumPy release.

Mac OS XВ¶

Building from sourceВ¶

A general overview of building NumPy from source is given here, with detailed instructions for specific platforms given seperately.

PrerequisitesВ¶

Building NumPy requires the following software installed:

Python 2.6.x, 2.7.x, 3.2.x or newer

On Debian and derivatives (Ubuntu): python, python-dev (or python3-dev)

On Windows: the official python installer at www.python.org is enough

Make sure that the Python package distutils is installed before continuing. For example, in Debian GNU/Linux, installing python-dev also installs distutils.

Python must also be compiled with the zlib module enabled. This is practically always the case with pre-packaged Pythons.

To build any extension modules for Python, you’ll need a C compiler. Various NumPy modules use FORTRAN 77 libraries, so you’ll also need a FORTRAN 77 compiler installed.

Note that NumPy is developed mainly using GNU compilers. Compilers from other vendors such as Intel, Absoft, Sun, NAG, Compaq, Vast, Porland, Lahey, HP, IBM, Microsoft are only supported in the form of community feedback, and may not work out of the box. GCC 4.x (and later) compilers are recommended.

Linear Algebra libraries

NumPy does not require any external linear algebra libraries to be installed. However, if these are available, NumPy’s setup script can detect them and use them for building. A number of different LAPACK library setups can be used, including optimized LAPACK libraries such as ATLAS, MKL or the Accelerate/vecLib framework on OS X.

Basic InstallationВ¶

To install NumPy run:

To perform an in-place build that can be run from the source folder run:

Note: for build instructions to do development work on NumPy itself, see :ref:`development-environment`.

Parallel buildsВ¶

From NumPy 1.10.0 on it’s also possible to do a parallel build with:

This will compile numpy on 4 CPUs and install it into the specified prefix. to perform a parallel in-place build, run:

FORTRAN ABI mismatchВ¶

The two most popular open source fortran compilers are g77 and gfortran. Unfortunately, they are not ABI compatible, which means that concretely you should avoid mixing libraries built with one with another. In particular, if your blas/lapack/atlas is built with g77, you must use g77 when building numpy and scipy; on the contrary, if your atlas is built with gfortran, you must build numpy/scipy with gfortran. This applies for most other cases where different FORTRAN compilers might have been used.

Choosing the fortran compilerВ¶

To build with g77:

To build with gfortran:

For more information see:

How to check the ABI of blas/lapack/atlasВ¶

One relatively simple and reliable way to check for the compiler used to build a library is to use ldd on the library. If libg2c.so is a dependency, this means that g77 has been used. If libgfortran.so is a a dependency, gfortran has been used. If both are dependencies, this means both have been used, which is almost always a very bad idea.

Disabling ATLAS and other accelerated librariesВ¶

Usage of ATLAS and other accelerated libraries in Numpy can be disabled via:

Supplying additional compiler flagsВ¶

Building with ATLAS supportВ¶

UbuntuВ¶

You can install the necessary package for optimized ATLAS with this command:

Building from source#

There are two options for building NumPy- building with Gitpod or locally from source. Your choice depends on your operating system and familiarity with the command line.

Gitpod#

Gitpod is an open-source platform that automatically creates the correct development environment right in your browser, reducing the need to install local development environments and deal with incompatible dependencies.

If you are a Windows user, unfamiliar with using the command line or building NumPy for the first time, it is often faster to build with Gitpod. Here are the in-depth instructions for building NumPy with building NumPy with Gitpod.

Building locally#

Building locally on your machine gives you more granular control. If you are a MacOS or Linux user familiar with using the command line, you can continue with building NumPy locally by following the instructions below.

Prerequisites#

Building NumPy requires the following software installed:

Python 3.8.x or newer

Please note that the Python development headers also need to be installed, e.g., on Debian/Ubuntu one needs to install both python3 and python3-dev. On Windows and macOS this is normally not an issue.

Much of NumPy is written in C. You will need a C compiler that complies with the C99 standard.

Part of Numpy is now written in C++. You will also need a C++ compiler that complies with the C++11 standard.

While a FORTRAN 77 compiler is not necessary for building NumPy, it is needed to run the numpy.f2py tests. These tests are skipped if the compiler is not auto-detected.

Note that NumPy is developed mainly using GNU compilers and tested on MSVC and Clang compilers. Compilers from other vendors such as Intel, Absoft, Sun, NAG, Compaq, Vast, Portland, Lahey, HP, IBM are only supported in the form of community feedback, and may not work out of the box. GCC 4.x (and later) compilers are recommended. On ARM64 (aarch64) GCC 8.x (and later) are recommended.

Linear Algebra libraries

For building NumPy, you’ll need a recent version of Cython.

Basic Installation#

To install NumPy, run:

To perform an in-place build that can be run from the source folder run:

Testing#

Make sure to test your builds. To ensure everything stays in shape, see if all tests pass.

The test suite requires additional dependencies, which can easily be installed with:

Parallel builds#

It’s possible to do a parallel build with:

This will compile numpy on 4 CPUs and install it into the specified prefix. to perform a parallel in-place build, run:

Choosing the fortran compiler#

For more information see:

How to check the ABI of BLAS/LAPACK libraries#

One relatively simple and reliable way to check for the compiler used to build a library is to use ldd on the library. If libg2c.so is a dependency, this means that g77 has been used (note: g77 is no longer supported for building NumPy). If libgfortran.so is a dependency, gfortran has been used. If both are dependencies, this means both have been used, which is almost always a very bad idea.

Accelerated BLAS/LAPACK libraries#

NumPy searches for optimized linear algebra libraries such as BLAS and LAPACK. There are specific orders for searching these libraries, as described below and in the site.cfg.example file.

Note that both BLAS and CBLAS interfaces are needed for a properly optimized build of NumPy.

The default order for the libraries are:

If you wish to build against OpenBLAS but you also have BLIS available one may predefine the order of searching via the environment variable NPY_BLAS_ORDER which is a comma-separated list of the above names which is used to determine what to search for, for instance:

will prefer to use ATLAS, then BLIS, then OpenBLAS and as a last resort MKL. If neither of these exists the build will fail (names are compared lower case).

will allow using anything but NetLIB BLAS and ATLAS libraries, the order of the above list is retained.

One cannot mix negation and positives, nor have multiple negations, such cases will raise an error.

LAPACK#

The default order for the libraries are:

If you wish to build against OpenBLAS but you also have MKL available one may predefine the order of searching via the environment variable NPY_LAPACK_ORDER which is a comma-separated list of the above names, for instance:

will prefer to use ATLAS, then OpenBLAS and as a last resort MKL. If neither of these exists the build will fail (names are compared lower case).

will allow using anything but the NetLIB LAPACK library, the order of the above list is retained.

One cannot mix negation and positives, nor have multiple negations, such cases will raise an error.

Deprecated since version 1.20: The native libraries on macOS, provided by Accelerate, are not fit for use in NumPy since they have bugs that cause wrong output under easily reproducible conditions. If the vendor fixes those bugs, the library could be reinstated, but until then users compiling for themselves should use another linear algebra library or use the built-in (but slower) default, see the next section.

Disabling ATLAS and other accelerated libraries#

Usage of ATLAS and other accelerated libraries in NumPy can be disabled via:

64-bit BLAS and LAPACK#

You can tell Numpy to use 64-bit BLAS/LAPACK libraries by setting the environment variable:

when building Numpy. The following 64-bit BLAS/LAPACK libraries are supported:

OpenBLAS ILP64 with 64_ symbol suffix ( openblas64_ )

OpenBLAS ILP64 without symbol suffix ( openblas_ilp64 )

Using non-symbol-suffixed 64-bit BLAS/LAPACK in a program that also uses 32-bit BLAS/LAPACK can cause crashes under certain conditions (e.g. with embedded Python interpreters on Linux).

The 64-bit OpenBLAS with 64_ symbol suffix is obtained by compiling OpenBLAS with settings:

The symbol suffix avoids the symbol name clashes between 32-bit and 64-bit BLAS/LAPACK libraries.

Supplying additional compiler flags#

Cross compilation#

Although numpy.distutils and setuptools do not directly support cross compilation, it is possible to build NumPy on one system for different architectures with minor modifications to the build environment. This may be desirable, for example, to use the power of a high-performance desktop to create a NumPy package for a low-power, single-board computer. Because the setup.py scripts are unaware of cross-compilation environments and tend to make decisions based on the environment detected on the build system, it is best to compile for the same type of operating system that runs on the builder. Attempting to compile a Mac version of NumPy on Windows, for example, is likely to be met with challenges not considered here.

When building and installing NumPy for a host system, the CC environment variable must provide the path the cross compiler that will be used to build NumPy C extensions. It may also be necessary to set the LDSHARED environment variable to the path to the linker that can link compiled objects for the host system. The compiler must be told where it can find Python libraries and development headers. On Unix-like systems, this generally requires adding, e.g., the following parameters to the CFLAGS environment variable:

for Python version 3.y. (Replace the “y” in this path with the actual minor number of the installed Python runtime.) Likewise, the linker should be told where to find host libraries by adding a parameter to the LDFLAGS environment variable:

To make sure Python-specific system configuration options are provided for the intended host and not the build system, set:

When using external linear algebra libraries, include and library directories should be provided for the desired libraries in site.cfg as described above and in the comments of the site.cfg.example file included in the NumPy repository or sdist. In this example, set:

under appropriate sections of the file to allow numpy.distutils to find the libraries.

As of NumPy 1.22.0, a vendored copy of SVML will be built on x86_64 Linux hosts to provide AVX-512 acceleration of floating-point operations. When using an x86_64 Linux build system to cross compile NumPy for hosts other than x86_64 Linux, set the environment variable NPY_DISABLE_SVML to prevent the NumPy build script from incorrectly attempting to cross-compile this platform-specific library:

With the environment configured, NumPy may be built as it is natively:

When the wheel package is available, the cross-compiled package may be packed into a wheel for installation on the host with:

It may be possible to use pip to build a wheel, but pip configures its own environment; adapting the pip environment to cross-compilation is beyond the scope of this guide.

The cross-compiled package may also be installed into the host prefix for cross-compilation of other packages using, e.g., the command:

When cross compiling other packages that depend on NumPy, the host npy-pkg-config file must be made available. For further discussion, refer to numpy distutils documentation.

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *