How to Install Python Packages in Visual Studio Code?

Visual Studio Code (VS Code) is a powerful, lightweight code editor that’s popular among developers. If you are new to Python development and want to use VS Code as your primary code editor, this tutorial will guide you through the process of installing Python packages in VS Code. Let’s get started!

Install Python Packages in Visual Studio Code

Now, let us see how to install Python packages in Visual Studio Code.

Prerequisites

Before we begin, make sure you have the following installed on your machine:

  1. Python: Download and install the latest version of Python from the official Python website.
  2. Visual Studio Code: Download and install VS Code from the official VS Code website.

Step 1: Install Python Extension for VS Code

The first step is to install the Python extension for VS Code, which provides rich support for the Python language.

  1. Open VS Code.
  2. Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window or by pressing Ctrl+Shift+X.
  3. In the search bar, type Python and select the extension provided by Microsoft.
  4. Click the Install button.

This extension will provide features like IntelliSense, linting, debugging, and more.

You can see the screenshot below for your reference:

Install Python Packages in Visual Studio Code

Step 2: Set Up a Python Environment

It’s a good practice to use a virtual environment for your Python projects. A virtual environment is an isolated environment that allows you to manage dependencies for different projects separately.

  1. Open the terminal in VS Code by selecting View > Terminal or pressing Ctrl+`.
  2. Navigate to your project directory using the cd command.
  3. Create a virtual environment by running the following command:
   python -m venv myenv

Replace myenv with the name you prefer for your virtual environment.

  1. Activate the virtual environment:
  • On Windows:
    sh .\myenv\Scripts\activate
  • On macOS and Linux:
    sh source myenv/bin/activate

You should see (myenv) at the beginning of your terminal prompt, indicating that the virtual environment is active.

Step 3: Install Python Packages

Now that your virtual environment is set up, you can install Python packages using pip, which is the package installer for Python.

  1. In the terminal, ensure your virtual environment is active.
  2. To install a package, use the following command:
   pip install package_name

Replace package_name with the name of the package you want to install. For example, to install the requests package, you would run:

   pip install requests

You can see the screenshot below; I ran the above command.

start with python in visual studio code
  1. To verify the installation, you can list the installed packages by running:
   pip list

Step 4: Managing Python Packages

You can also manage your Python packages directly from VS Code:

  1. Open the Command Palette by pressing Ctrl+Shift+P.
  2. Type Python: Select Interpreter and select the interpreter from your virtual environment.
  3. To install additional packages, you can use the integrated terminal or add them to a requirements.txt file and run:
   pip install -r requirements.txt

Step 5: Writing and Running Python Code

With your environment set up and packages installed, you can now write and run your Python code in VS Code.

  1. Create a new Python file by selecting File > New File and saving it with a .py extension.
  2. Write your Python code in the editor.
  3. To run your code, right-click in the editor and select Run Python File in Terminal or simply press F5 to start debugging.

Conclusion

Congratulations! You have successfully set up Python in Visual Studio Code, created a virtual environment, and installed Python packages. I hope, if you are a beginner, this tutorial will be helpful to you in installing Python packages in Visual Studio Code.

You may also like:

Leave a Comment