Running and testing your virtual environment in Visual Studio Code: Beginner's guide
By Ed Malaker
Setting up a virtual environment is an important step in creating your development workflow. It allows you to manage the dependencies for each of your projects, and it prevents your Python installation from getting cluttered with a lot of libraries.
We chose to use Visual Studio Code because it has a user-friendly interface and provides developers with the tools they need to create, run, and test virtual environments efficiently. Let’s take a look at how to get started, create a virtual environment, test that it’s working, and deactivate it. You can then use the same process in your other programs.
Before you get started
If you haven’t installed Visual Studio Code (VS Code) yet or aren’t sure if it’s right for you, we recommend checking out our article about what it is, why you should use it, and how to get started before reading this one.
Opening the terminal in VS Code
In VS Code, we do a lot of work in the terminal, and it’s very similar to the command prompt if you are familiar with that. You open a new terminal by clicking Terminal> New Terminal at the top of the screen. You will then see it at the bottom of your screen.
Test commands
Next, we can run some test commands to make sure everything is working properly. First, we will check our system to see if we have Python installed correctly. Typing python –version in the prompt should return something like “Python 3.12.3.” Otherwise, you likely need to visit the Python website to download it on your system.
Next, you will need to create a project folder and navigate to that folder in VS code. Every project should have a unique virtual environment so we can put it right in the project file. Create a folder on your system with your project name (no spaces), then type:
cd path_to_your_project_folder
WINDOWS TIP – an easy way to get the path to any folder is to open the folder, and in the address bar, you can right-click the name of the folder and pick save as path. Then, when you right-click on the command prompt in VS Code, it will paste it.
Create the virtual environment
With a projet folder created and the path to that folder set in VS Code it’s time to create our virtual environment by running the following command in your terminal:
python -m venv myenv
You can use “myenv” or change it to whatever name you want for your virtual environment. Entering the prompt will create a folder in your project folder with the name of your environment, which will contain the environment we are making. If you open it after hitting enter, you will see the folder, and the VS Code will automatically populate it with files and folders that are important for creating the environment.
Top modern programming languages driving the future of development
Open your environment
To activate the environment, type in the following code depending on your operating system.
Windows: myenv\Scripts\activate
macOS/Linux: source myenv/bin/activate
After you enter the code, you should notice that your terminal prompt will change to show your environment name. For instance, (myenv) C:\path\to\your\project>
Verify your Python interpreter in VS Code
Open the Command Palette in VS Code with Ctrl + Shift + P or Cmd + Shift + P. Type Python: Select Interpreter in the search bar and select it.
Then, choose the Interpreter associated with your virtual environment: path\to\myenv\bin\python (macOS/Linux) or path\to\myenv\Scripts\python (Windows).
Install required libraries
With your virtual environment active, you can start installing the libraries you need for the project using the pip command. Here’s an example where we will install three libraries to test the idea:
pip install numpy pandas matplotlib
Entering that code will install the libraries, and you will see some action on the screen as it does. When it’s finished, you can use the code pip list to get a list of libraries the environment sees, and you should see numpy, pandas, and matplotlib on that list.
Test the setup
On the sidebar or VS Code, open the project folder right, click inside the folder to create a new file titled test.py and add the following code:
import numpy
import pandas
print("Virtual environment and libraries are working!")
Next, you will run the script by typing python test.py into the terminal, which should output the following: Virtual environment and libraries are working!
Deactivating your virtual environment
When you finish working in the virtual environment, you can type deactivate to return to your global Python environment. You can then open a different environment to work on another project.
If you close your VS Code, or open a new file or terminal, your environment will shut down, and you will need to reopen it to begin working on the project again.
Create a new virtual environment for each project you work on!