Visual Studio Code (VS Code) is a free, open-source code editor that is lightweight yet powerful. One of its many features is the ability to manage Python virtual environments, which are tools for isolating project-specific dependencies from the global Python interpreter.
This guide will walk you through the process of setting up a virtual environment in VS Code.
Ensure you have the following installed:
- Python: You can verify this by running
python --versionorpython3 --versionin your terminal or command prompt. - Visual Studio Code: You can download it from here.
- Python extension for Visual Studio Code: You can install it from within VS Code by clicking on the Extensions view icon on the Sidebar (or press
Ctrl+Shift+X), searching for "Python", and clicking onInstallfor the one by Microsoft.
-
Open the Terminal in VS Code
To open the terminal in VS Code, you can use the
Ctrl+backtick shortcut, or navigate toTerminal > New Terminal` from the menu. This will open a new terminal at the bottom of your VS Code window. -
Navigate to Your Project Directory
Use the
cdcommand followed by the path of your project directory to navigate into it. For example, if your project directory is named "my_project" and is located in your "Documents" folder, you could use the following command:cd Documents/my_project -
Create the Virtual Environment
Once you're in your project directory, you can create a new virtual environment using the
venvmodule that comes with Python. The following command creates a new virtual environment named "venv" in your project directory:python3 -m venv venv
If you're using Windows, use
pythoninstead ofpython3. if your using linux -
Activate the Virtual Environment
The method to activate your virtual environment depends on your operating system:
-
On macOS and Linux:
source venv/bin/activate -
On Windows:
.\venv\Scripts\activate
Once the virtual environment is activated, you'll see
(venv)at the beginning of your command prompt. -
-
Set the Python Interpreter in VS Code
After creating and activating your virtual environment, you need to tell VS Code to use the Python interpreter in this environment. You can do this by clicking on the Python version in the bottom-left corner of VS Code (or using
Ctrl+Shift+Pand searching for "Python: Select Interpreter"), then selecting the interpreter that's located in your project directory under the "venv" folder. -
Install Packages as Needed
Now that your virtual environment is set up and activated, you can install any necessary packages using
pip. For example, to install the requests package, you would use the following command:pip install requests
And that's it! You now have a Python virtual environment set up in VS Code, isolated from your global Python interpreter. This allows you to manage your project's dependencies more effectively.
Remember to always activate your virtual environment before you start working on your project, and deactivate it when you're done. You can deactivate the environment with the following command:
deactivateHappy coding!