How to set up a python virtual environment in Visual Studio Code for Windows

In this article we will look at the steps involved in setting up Visual Studio Code for Python including creating and working with a virtual environment in Windows.

Pre-Requisite

    1. Download and install Python 3
    2. Download and install VS Code

    Basic Setup

    1. Install Python extension from Microsoft.
    2. Open the command palette using Ctrl + Shift + P and type in “Select: Python Interpreter” to select the command.
    3. Select an interpreter from the list of Python interpreters displayed. The selected interpreter would be the one used in this workspace.
    4. Create a new file and name it “helloworld.py” and enter the below code.
    msg = "Hello World!"
    print(msg)

    There are a few ways to run the program, like below:

    • Open the command palette using Ctrl + Shift + P and type in “Terminal: Create New Terminal”. In the terminal type python helloworld.py to run the program.
    • Click the Run button on top right corner in VS Code to run the program

    Virtual Environment Setup

    To install and use python packages in a project, it is usually best practice to set up a separate virtual environment specific to this project. This way any packages we install are local to this virtual environment and will not impact any other environment including the global one.

    1. Create a virtual environment by executing the below command. The “.venv” is the environment name and we can provide any name to it. A new folder named “.venv” will be created within the project workspace with a copy of the python interpreter. When this environment is activated, all the packages will be installed in this folder.

    py -3 -m venv .venv

    2. Activate the virtual environment manually by executing the below command

    .venv/scripts/activate

    Note: If you get this error – “Activate.ps1 cannot be loaded because running scripts is disabled on this system.”, then follow the steps in this link to set the Execution Policy in Powershell.

    3. Open the command palette (Ctrl + Shift + P) and select the command “Select: Python Interpreter”. Now select the virtual environment created in the above step as the default interpreter from the list of interpreters displayed. Going forward, when a new terminal is opened, it will automatically activate the virtual environment.

    Test Virtual Environment Setup

    1. Update the “helloworld.py” with the below code which utilizes the “requests” module to make a HTTP request.

    import requests
    
    response = requests.get("https://www.google.com")
    print(response.status_code)

    2. Activate the virtual env using the below command or if the virtual environment is set as the default one, then opening up a new terminal will automatically activate it.

    .venv/scripts/activate

    3. Install the requests module.

    pip install requests

    4. Run the program and it should print the status code “200”.

    py helloworld.py

    That’s it for setting up the virtual environment and using it from within VS Code. You can follow the same process each time you work on a new project by isolating the Python environment and keeping it specific and local to the project.

    References

    Leave a Comment

    Your email address will not be published. Required fields are marked *