Setting up a Python virtual environment

Setting up a Python virtual environment is a crucial step in creating a clean and isolated workspace for your Python projects. It helps in managing dependencies and avoiding conflicts between different projects. In this guide, we’ll walk you through the steps to set up a Python virtual environment and configure it in Visual Studio Code (VS Code).

1. Install Python

Before you begin, make sure Python is installed on your system. You can download and install the latest version of Python from the official Python website.

To verify that Python is installed, open a terminal or command prompt and type:

python --version

This should display the version of Python installed on your system.

2. Install Visual Studio Code

If you haven’t installed Visual Studio Code yet, you can download it from the official Visual Studio Code website.

3. Install Python Extension for Visual Studio Code

To enable Python support in VS Code, you’ll need to install the Python extension. Here’s how you can do it:

  1. Open Visual Studio 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 click on the Python extension provided by Microsoft.

  4. Click the “Install” button.

This extension provides features like IntelliSense, linting, debugging, and more.

4. Create a Project Directory

Before creating a virtual environment, it’s a good practice to have a dedicated directory for your project. Open a terminal or command prompt and navigate to the location where you want to create your project directory:

mkdir myproject
cd myproject

5. Create a Virtual Environment

Now, you can create a virtual environment inside your project directory. The most common way to create a virtual environment is by using the venv module, which is included in Python’s standard library:

python -m venv venv

Here, venv is the name of the virtual environment directory. You can name it anything, but it’s common to use names like venv or env.

6. Activate the Virtual Environment

If you are getting this error in Windows system while activating virtual environment

Command for the User rights error: Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned

Read this -> Command for User Rights Error in Visual Studio Code: Set-ExecutionPolicy Fix

Once the virtual environment is created, you need to activate it. Activation scripts are platform-specific:

  • On Windows:
venv\Scripts\activate

On macOS and Linux:

source venv/bin/activate

After activation, your terminal prompt should change to indicate that the virtual environment is active.

7. Open the Project in Visual Studio Code

Open Visual Studio Code in your project directory by typing the following command in your terminal:

code .

This will launch VS Code with the current directory as your workspace.

8. Select the Python Interpreter

To ensure VS Code uses the Python interpreter from your virtual environment, you need to select it manually:

  1. In VS Code, press Ctrl+Shift+P to open the Command Palette.

  2. Type “Python: Select Interpreter” and select it from the list.

  3. From the options, choose the Python interpreter that points to your virtual environment. It should be something like .\venv\Scripts\python.exe on Windows or ./venv/bin/python on macOS/Linux.

VS Code will now use this interpreter for running Python scripts in this project.

9. Installing Dependencies

With the virtual environment active, you can now install dependencies specific to your project using pip. For example:

pip install requests

This installs the requests library into your virtual environment, isolated from your global Python installation.

10. Deactivating the Virtual Environment

When you’re done working on your project, you can deactivate the virtual environment by simply typing:

deactivate

This will return your terminal to the global Python environment.

Conclusion

Setting up a Python virtual environment in Visual Studio Code ensures that your development environment is clean, organized, and project-specific. By following these steps, you’ll have a setup that helps you manage dependencies effectively and prevents potential conflicts between different projects.