Installing a Python Library in Visual Studio Code — Windows
In this quick blogpost, I will share the steps that you can follow in order to install a Python library using pip through either the Terminal or a Jupyter Notebook in Visual Studio Code (VSCode) on a Windows computer.
Pre-requisites
In order to complete the steps of this blogpost, you need to install the following in your windows computer:
- Visual Studio Code : you can find the steps to install it here.
- Python Extension for Visual Studio Code : you can find the steps to install it here.
- Python Interpreter : you can find the steps to install it here.
Installing a Python Library Using the Terminal in VSCode
1) Accessing Visual Studio Code Terminal
- Open VSCode application
- Go to the Terminal menu and select New Terminal .
- A new terminal (PowerShell based) window is opened.
2) Importing a Python Library
- Run the following command to validate that pip is installed in your computer.
- Let us say that you want to install Pandas Python library.
- Run the following command
- Pandas library is now ready to be imported by any python application. You can repeat this process for any Python library.
Installing a Python Library Using a Jupyter Notebook in VSCode
1) Creating a Jupyter Notebook in VSCode
- Create a Jupyter Notebook following the steps of My First Jupyter Notebook on Visual Studio Code (Python kernel)
2) Importing a Python Library
- Run the following command to validate that pip is installed in your computer.
- Let us say that you want to install Pandas Python library.
- Run the following command.
How to install a new python module on VSCode?
I’m trying to install new python modules on my computer and I know how to install through the terminal, but I wish to know if there is a way to install a new module directly through VSCode (like it is possible on PyCharm)?
I already installed through the terminal, it isn’t a problem, but I want to install without be obligate to open the terminal when I’m working on VSCode.
3 Answers 3
You should open the terminal inside the VSCode and install the modules you want. something like
if that’s not you meant, please let me know.
- First of all I would advise you to select the current Python version you have. It has been explained here:
- Next, you should check is the pip installed in your Python main directory or not, by checking how to do on this website:
in your terminal, like
You should have the list of commands and general options which can be used. One of them is install
On the Python library platform, you always have the command to be copied in order to the installation of package you want.
In your terminal, the initial command should look as:
to which you should append the command prepared on the Python library platform (by copying it and pasting).
That’s it. The package should be installed in your Python folder, what you will see in the terminal.
Install Python and libraries with Visual Studio
This tutorial explains how to install Python and libraries with Visual Studio. I will also show how to install libraries in the command prompt (CMD). Python is a very powerful language for machine learning as it has many libraries that can be used to help you develop great models for prediction.
Visual Studio Installer
Use Visual Studio Installer to install Python with Visual Studio. Modify your Visual Studio Version, check Python development and click on the Modify button. Python will be installed to “C:\Program Files (x86)\Microsoft Visual Studio\Shared”.
Add to path environment variable
You need to add the path to the Python folder and to the Scripts folder in environment variables, if you want to be able to use Python and Pip (install libraries) from anywhere in the Command Prompt. It is also useful to add paths to directories as environment variables if other programs references these programs in code.
Search for environment variables and click on the Edit environment variables result. Add the Python directory (C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64) and the Scripts directory (C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\Scripts) as new rows in the Path variable.
You will need to browse to the Python folder in the Command Prompt to be able to execute python scripts if you don’t have added paths to environment variables.
Install libraries
You will need some libraries to be able to work with machine learning. You can install libraries with Pip from the command prompt or in Visual Studio. Some useful libraries is joblib, numpy, pandas, matplotlib, pytesseract, scipy, statsmodels, scikit-learn, tensorflow and Keras. You need to know that names of libraries is case sensitive. To use pytesseract you will also need to install Tesseract OCR and unpack poppler, you will also need to add the path to poppler and Tesseract OCR as environment variables.
Open the Command Prompt (CMD) as an administrator to install libraries with Pip. You can install specific versions of a library and uninstall libraries with Pip.
You can also install, uninstall and upgrade Python libraries in Visual Studio. This is done inside Python Environments. Select Packages (PyPI) in the dropdown (Overview).
Test if libraries is installed
Create a new Python project in Visual Studio and add an Empty Python File to the project. Right click the new file and set it as Startup File. Add the following contents and run the script.
Installing Python package in Visual Studio Code
This short post shows you how to install the Python package in Visual Studio Code, which is widely used in the programming environment. The prerequisite for our operation is the Python extension in Visual Studio code, which should be installed at the very beginning (Pic. 1).
Pic. 1 Python extension in Visual Studio Code.
You can do the installation from this link and get the newest version of the Python language.
Next, you can start programming with Python in Visual Studio Code without a problem. However, it seems, like you are somewhat restricted when you discover, that there are problems with installing the external Python libraries and importing them to Visual Studio Code. I hope, that this quick solution will help you manage these issues.
The biggest problem at the initial stage is the presumed lack of the Pip installation in our Visual Studio Code software. The Pip is the most popular tool for installing Python packages and includes a modern version of Python. It provides the essential core features for finding, downloading, and installing Python packages.
In Visual Studio Code, the Pip seems to be not recognized as an internal or external command (Pic. 2). Usually, we choose the quickest way of Python module installation, where after visiting the module we want to get, we simply make the copy of the top installation command and next, paste it to our Python dedicated software, i.e. PyCharm.
Pic. 2 The “Pip not recognized” error in Visual Studio Code.
In the Visual Studio Code, we need to change our pip command a bit. The proper command in our terminal should be:
Once we hit enter, the whole list of commands should be populated as you can see below (Pic. 3).
Pic. 3 A part of the list of commands for Pip in Visual Studio Code.
Now we are sure, that the Pip has been recognized by our Visual Studio Code. It means, that the Python library can be installed, which is great.
In order to make the installation correctly, we can’t simply make a copy of the main command available just below the library name (Pic. 2).
If we would do so, then our command would look like this:
and we would have to remove one “ pip ” preventing at least one space between the strings as you can see here.
I think, much easier is just type the py-m in our terminal path:
and then append the library command, which we want to install (Pic. 2)…
After hitting the “Enter” button, you can see the installation progress. Next, the tool will inform you the information about the target path where the library has been installed as well as the new pip version if available (Pic. 4).
Pic. 4 The Python library installation progress in Visual Studio Code.
In order to make sure, that everything is alright, you can import the library to the file you are working on, by simply typing:
If something would be wrong, the Python console will underline your code with a wavy red line. Since everything has been installed properly, the library is to be recognized instantly by the Visual Studio Code (Pic. 5).
Pic. 5 Python library imported successfully to Visual Studio Code.
I highly recommend using Visual Studio Code, which appears to be the best coding application in the market. The biggest advantage is a broad opportunity for coding in various programming languages. This application is great and supersedes other programming applications dedicated to a specified language. As a result, a lot of memory is saved keeping our computers faster.