Installing NumPy
The only prerequisite for installing NumPy is Python itself. If you don’t have Python yet and want the simplest way to get started, we recommend you use the Anaconda Distribution — it includes Python, NumPy, and many other commonly used packages for scientific computing and data science.
NumPy can be installed with conda , with pip , with a package manager on macOS and Linux, or from source. For more detailed instructions, consult our Python and NumPy installation guide below.
CONDA
If you use conda , you can install NumPy from the defaults or conda-forge channels:
# Best practice, use an environment rather than install in the base env conda create -n my-env conda activate my-env # If you want to install from conda-forge conda config --env --add channels conda-forge # The actual install command conda install numpy
PIP
If you use pip , you can install NumPy with:
pip install numpy
Also when using pip, it’s good practice to use a virtual environment — see Reproducible Installs below for why, and this guide for details on using virtual environments.
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:
- Install Anaconda (it installs all packages you need and all other tools mentioned below).
- For writing and executing code, use notebooks in JupyterLab for exploratory and interactive computing, and Spyder or Visual Studio Code for writing scripts and packages.
- Use Anaconda Navigator to manage your packages and start JupyterLab, Spyder, or Visual Studio Code.
Advanced users
Conda
- Install Miniforge.
- Keep the base conda environment minimal, and use one or more conda environments to install the package you need for the task or project you’re working on.
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:
- Install Python from python.org, Homebrew, or your Linux package manager.
- Use Poetry as the most well-maintained tool that provides a dependency resolver and environment management capabilities in a similar fashion as conda does.
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 two main tools that install Python packages are pip and conda . Their functionality partially overlaps (e.g. both can install numpy ), however, they can also work together. We’ll discuss the major differences between pip and conda here — this is important to understand if you want to manage packages effectively.
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:
- use a different environment per project you’re working on,
- record package names and versions using your package installer; each has its own metadata format for this:
- Conda: conda environments and environment.yml
- Pip: virtual environments and requirements.txt
- Poetry: virtual environments and pyproject.toml
NumPy packages & accelerated linear algebra libraries
NumPy doesn’t depend on any other Python packages, however, it does depend on an accelerated linear algebra library — typically Intel MKL or OpenBLAS. Users don’t have to worry about installing those (they’re automatically included in all NumPy install methods). Power users may still want to know the details, because the used BLAS can affect performance, behavior and size on disk:
- 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.
- In the conda-forge channel, NumPy is built against a dummy “BLAS” package. When a user installs NumPy from conda-forge, that BLAS package then gets installed together with the actual library — this defaults to OpenBLAS, but it can also be MKL (from the defaults channel), or even BLIS or reference BLAS.
- 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:
- Intel MKL is not open source. For normal use this is not a problem, but if a user needs to redistribute an application built with NumPy, this could be an issue.
- Both MKL and OpenBLAS will use multi-threading for function calls like np.dot , with the number of threads being determined by both a build-time option and an environment variable. Often all CPU cores will be used. This is sometimes unexpected for users; NumPy itself doesn’t auto-parallelize any function calls. It typically yields better performance, but can also be harmful — for example when using another level of parallelization with Dask, scikit-learn or multiprocessing.
Troubleshooting
If your installation fails with the message below, see Troubleshooting ImportError.
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE! Importing the numpy c-extensions failed. This error can happen for different reasons, often due to issues with your setup.
How to Install numpy in Visual Studio Code : 5 Steps Only
Numpy is a Python library that allows you to do mathematical calculations very easily. It has many functions that allow you to perform these calculations. Many data science learner readers have asked that they are unable to install numpy in visual studio code. In this tutorial, you will know how to install numpy in visual studio code through the steps.
Steps to install numpy in Visual Studio Code
In this section, you will know all the steps that you will follow for better understanding.
Step 1: Open your visual studio code and go to the extension tab. You will find it on the left side of the window.
Step 2: Go to the search bar and type Python. There you will see the Python by Mircosoft.
Step 3: Click on the install button to install. It will be a blue in color.
Step 4: After the installation of Python. Go to the terminal and click on the new terminal.
Step 5: Type the following command on it to install the numpy.
Using NumPy in Visual Studio
I downloaded http://pytools.codeplex.com/ (Python Tools for Visual Studio) so that I could write Python in Visual Studio. The problem is when I try to use the most basic package «numpy» like so:
import numpy
It says «No module named ‘numpy’.» How can I use NumPy and SciPy in Visual Studio? Note: I am using Canopy Express on another machine which works perfectly; however, I don’t want to install it on this machine since I already have Visual Studio installed. I added the Python 3.3 environment to my Python «solution» by right-clicking Python environments and clicking add an environment. But, after right-clicking my environment and clicking install Python package, I typed in «numpy» and got this error when trying to install it:
. creating build creating build\src.win32-3.3 creating build\src.win32-3.3\numpy creating build\src.win32-3.3\numpy\distutils building library "npymath" sources No module named 'numpy.distutils.msvccompiler' in numpy.distutils; trying from distutils error: Unable to find vcvarsall.bat ---------------------------------------- Cleaning up. Command python setup.py egg_info failed with error code 1 in c:\users\dom\appdata\local\temp\pip_build_Dom\numpy Storing complete log in C:\Users\Dom\pip\pip.log 'numpy' failed to install. Exit code: 1
Getting Started with Python in VS Code
In this tutorial, you will learn how to use Python 3 in Visual Studio Code to create, run, and debug a Python «Roll a dice» application, work with virtual environments, use packages, and more! By using the Python extension, you turn VS Code into a great, lightweight Python editor.
To learn more about the Python language, follow any of the programming tutorials listed on python.org within the context of VS Code.
If you are looking for a Data Science focused tutorial with Python, check out our Data Science section.
Prerequisites
To successfully complete this tutorial, you need to first setup your Python development environment. Specifically, this tutorial requires:
- Python 3
- VS Code
- VS Code Python extension (For additional details on installing extensions, see Extension Marketplace)
Install a Python interpreter
Along with the Python extension, you need to install a Python interpreter. Which interpreter you use is dependent on your specific needs, but some guidance is provided below.
Windows
Install Python from python.org. Use the Download Python button that appears first on the page to download the latest version.
Note: If you don’t have admin access, an additional option for installing Python on Windows is to use the Microsoft Store. The Microsoft Store provides installs of supported Python versions.
For additional information about using Python on Windows, see Using Python on Windows at Python.org
macOS
The system install of Python on macOS is not supported. Instead, a package management system like Homebrew is recommended. To install Python using Homebrew on macOS use brew install python3 at the Terminal prompt.
Note: On macOS, make sure the location of your VS Code installation is included in your PATH environment variable. See these setup instructions for more information.
Linux
The built-in Python 3 installation on Linux works well, but to install other Python packages you must install pip with get-pip.py.
Other options
- Data Science: If your primary purpose for using Python is Data Science, then you might consider a download from Anaconda. Anaconda provides not just a Python interpreter, but many useful libraries and tools for data science.
- Windows Subsystem for Linux: If you are working on Windows and want a Linux environment for working with Python, the Windows Subsystem for Linux (WSL) is an option for you. If you choose this option, you’ll also want to install the WSL extension. For more information about using WSL with VS Code, see VS Code Remote Development or try the Working in WSL tutorial, which will walk you through setting up WSL, installing Python, and creating a Hello World application running in WSL.
Note: To verify that you’ve installed Python successfully on your machine, run one of the following commands (depending on your operating system):
Linux/macOS: open a Terminal Window and type the following command:
python3 --version
Windows: open a command prompt and run the following command:
py -3 --version
If the installation was successful, the output window should show the version of Python that you installed. Alternatively, you can use the py -0 command in the VS Code integrated terminal to view the versions of python installed on your machine. The default interpreter is identified by an asterisk (*).
Start VS Code in a workspace folder
By starting VS Code in a folder, that folder becomes your «workspace».
Using a command prompt or terminal, create an empty folder called «hello», navigate into it, and open VS Code ( code ) in that folder ( . ) by entering the following commands:
mkdir hello cd hello code .
Note: If you’re using an Anaconda distribution, be sure to use an Anaconda command prompt.
Alternately, you can create a folder through the operating system UI, then use VS Code’s File > Open Folder to open the project folder.
Create a virtual environment
A best practice among Python developers is to use a project-specific virtual environment . Once you activate that environment, any packages you then install are isolated from other environments, including the global interpreter environment, reducing many complications that can arise from conflicting package versions. You can create non-global environments in VS Code using Venv or Anaconda with Python: Create Environment.
Open the Command Palette ( ⇧⌘P (Windows, Linux Ctrl+Shift+P ) ), start typing the Python: Create Environment command to search, and then select the command.
The command presents a list of environment types, Venv or Conda. For this example, select Venv.
The command then presents a list of interpreters that can be used for your project. Select the interpreter you installed at the beginning of the tutorial.
After selecting the interpreter, a notification will show the progress of the environment creation and the environment folder ( /.venv ) will appear in your workspace.
Ensure your new environment is selected by using the Python: Select Interpreter command from the Command Palette.
Note: For additional information about virtual environments, or if you run into an error in the environment creation process, see Environments.
Create a Python source code file
From the File Explorer toolbar, select the New File button on the hello folder:
Name the file hello.py , and VS Code will automatically open it in the editor:
By using the .py file extension, you tell VS Code to interpret this file as a Python program, so that it evaluates the contents with the Python extension and the selected interpreter.
Note: The File Explorer toolbar also allows you to create folders within your workspace to better organize your code. You can use the New folder button to quickly create a folder.
Now that you have a code file in your Workspace, enter the following source code in hello.py :
msg = "Roll a dice" print(msg)
When you start typing print , notice how IntelliSense presents auto-completion options.
IntelliSense and auto-completions work for standard Python modules as well as other packages you’ve installed into the environment of the selected Python interpreter. It also provides completions for methods available on object types. For example, because the msg variable contains a string, IntelliSense provides string methods when you type msg. :
Finally, save the file ( ⌘S (Windows, Linux Ctrl+S ) ). At this point, you’re ready to run your first Python file in VS Code.
For full details on editing, formatting, and refactoring, see Editing code. The Python extension also has full support for Linting.
Run Hello World
Click the Run Python File in Terminal play button in the top-right side of the editor.
The button opens a terminal panel in which your Python interpreter is automatically activated, then runs python3 hello.py (macOS/Linux) or python hello.py (Windows):
There are three other ways you can run Python code within VS Code:
- Right-click anywhere in the editor window and select Run > Python File in Terminal (which saves the file automatically):
- Select one or more lines, then press Shift+Enter or right-click and select Run Selection/Line in Python Terminal. This command is convenient for testing just a part of a file.
- From the Command Palette ( ⇧⌘P (Windows, Linux Ctrl+Shift+P ) ), select the Python: Start REPL command to open a REPL terminal for the currently selected Python interpreter. In the REPL, you can then enter and run lines of code one at a time.
Configure and run the debugger
Let’s now try debugging our Hello World program.
First, set a breakpoint on line 2 of hello.py by placing the cursor on the print call and pressing F9 . Alternately, click in the editor’s left gutter, next to the line numbers. When you set a breakpoint, a red circle appears in the gutter.
Next, to initialize the debugger, press F5 . Since this is your first time debugging this file, a configuration menu will open from the Command Palette allowing you to select the type of debug configuration you would like for the opened file.
Note: VS Code uses JSON files for all of its various configurations; launch.json is the standard name for a file containing debugging configurations.
Select Python File, which is the configuration that runs the current file shown in the editor using the currently selected Python interpreter.
Start the debugger by clicking on the down-arrow next to the run button on the editor, and selecting Debug Python File in Terminal.
The debugger will stop at the first line of the file breakpoint. The current line is indicated with a yellow arrow in the left margin. If you examine the Local variables window at this point, you will see now defined msg variable appears in the Local pane.
A debug toolbar appears along the top with the following commands from left to right: continue ( F5 ), step over ( F10 ), step into ( F11 ), step out ( ⇧F11 (Windows, Linux Shift+F11 ) ), restart ( ⇧⌘F5 (Windows, Linux Ctrl+Shift+F5 ) ), and stop ( ⇧F5 (Windows, Linux Shift+F5 ) ).
The Status Bar also changes color (orange in many themes) to indicate that you’re in debug mode. The Python Debug Console also appears automatically in the lower right panel to show the commands being run, along with the program output.
To continue running the program, select the continue command on the debug toolbar ( F5 ). The debugger runs the program to the end.
Tip Debugging information can also be seen by hovering over code, such as variables. In the case of msg , hovering over the variable will display the string Roll a dice! in a box above the variable.
You can also work with variables in the Debug Console (If you don’t see it, select Debug Console in the lower right area of VS Code, or select it from the . menu.) Then try entering the following lines, one by one, at the > prompt at the bottom of the console:
msg msg.capitalize() msg.split()
Select the blue Continue button on the toolbar again (or press F5 ) to run the program to completion. «Roll a dice!» appears in the Python Debug Console if you switch back to it, and VS Code exits debugging mode once the program is complete.
If you restart the debugger, the debugger again stops on the first breakpoint.
To stop running a program before it’s complete, use the red square stop button on the debug toolbar ( ⇧F5 (Windows, Linux Shift+F5 ) ), or use the Run > Stop debugging menu command.
For full details, see Debugging configurations, which includes notes on how to use a specific Python interpreter for debugging.
Tip: Use Logpoints instead of print statements: Developers often litter source code with print statements to quickly inspect variables without necessarily stepping through each line of code in a debugger. In VS Code, you can instead use Logpoints. A Logpoint is like a breakpoint except that it logs a message to the console and doesn’t stop the program. For more information, see Logpoints in the main VS Code debugging article.
Install and use packages
Let’s build upon the previous example by using packages.
In Python, packages are how you obtain any number of useful code libraries, typically from PyPI, that provide additional functionality to your program. For this example, you use the numpy package to generate a random number.
Return to the Explorer view (the top-most icon on the left side, which shows files), open hello.py , and paste in the following source code:
import numpy as np msg = "Roll a dice" print(msg) print(np.random.randint(1,9))
Tip: If you enter the above code by hand, you may find that auto-completions change the names after the as keywords when you press Enter at the end of a line. To avoid this, type a space, then Enter .
Next, run the file in the debugger using the «Python: Current file» configuration as described in the last section.
You should see the message, «ModuleNotFoundError: No module named ‘numpy'». This message indicates that the required package isn’t available in your interpreter. If you’re using an Anaconda distribution or have previously installed the numpy package you may not see this message.
To install the numpy package, stop the debugger and use the Command Palette to run Terminal: Create New Terminal ( ⌃⇧` (Windows, Linux Ctrl+Shift+` ) ). This command opens a command prompt for your selected interpreter.
To install the required packages in your virtual environment, enter the following commands as appropriate for your operating system:
-
Install the packages
# Don't use with Anaconda distributions because they include matplotlib already. # macOS python3 -m pip install numpy # Windows (may require elevation) py -m pip install numpy # Linux (Debian) apt-get install python3-tk python3 -m pip install numpy
Next steps
To learn to build web apps with the Django and Flask frameworks, see the following tutorials:
- Use Django in Visual Studio Code
- Use Flask in Visual Studio Code
There is then much more to explore with Python in Visual Studio Code:
- Python profile template — Create a new profile with a curated set of extensions, settings, and snippets
- Editing code — Learn about autocomplete, IntelliSense, formatting, and refactoring for Python.
- Linting — Enable, configure, and apply a variety of Python linters.
- Debugging — Learn to debug Python both locally and remotely.
- Testing — Configure test environments and discover, run, and debug tests.
- Settings reference — Explore the full range of Python-related settings in VS Code.
- Deploy Python to Azure App Service
- Deploy Python to Container Apps