VSCode: Setting up Virtual Environments & Git repositories

Dipan Saha
5 min readApr 28, 2023

--

Photo by Milad Fakurian on Unsplash

A virtual environment is a self-contained Python environment that allows you to install and use different versions of Python and its libraries without affecting your system Python installation. This can be useful for development, testing, and deployment.

Create a Virtual Environment in VSCode

To create a virtual environment in VSCode, you can use the following steps:

Step 1: Open a new VSCode session.

Open VSCode and click on source control.

Step 2: Clone a repository & open it in your workspace.

Select Clone Repository and provide the Git Repo link you want to clone.

Select the destination path where you want to clone the git repository.

Open the cloned repo.

Step 3: Create a virtual environment and attach that to your workspace folder.

Open a powershell terminal within VSCode and use the command python -m venv .venv to create a virtual environment. Attach this virtual environment to your workspace.

Step 4: Select the Python Interpreter.

Press Shift + Control + P to open the Command Palette and click on the Python: Select Interpreter.

Select the Python interpreter which came with your virtual environment.

Step 5: Activate the virtual environment.

The latest version of the virtual environment supports PowerShell out-of-the-box.

Activate the virtual environment using the command: .venv\Scripts\activate.ps1

Step 6: Install required libraries and complete your development.

Install all the required libraries (pip install -r requirements.txt) and complete your development (Build & Test). Once done, promote your changes to Git.

Step 7: Deactivate the virtual environment.

Deactivate the virtual environment using the command:deactivate

Step 8: Change the interpreter back to the Global one.

Press Shift + Control + P to open the Command Palette and click on the Python: Select Interpreter. This time, select the Global interpreter (The one you original had).

Step 9: Remove the virtual environment along with all the associated packages and dependencies.

Remove the virtual environment using the command:rm -r .venv/

This will remove all the associated packages and dependencies without affecting your system’s global Python installation or other projects on your machine.

Benefits of using a Virtual Environment

Using a virtual environment has several benefits:

  1. Isolation: Virtual environments provide a sandboxed space to work in, where you can install and use packages and dependencies without affecting your system’s global Python installation or other projects on your machine.
  2. Version control: A virtual environment allows you to install specific versions of packages and libraries for your project, avoiding any conflicts or unexpected behavior caused by package updates or changes.
  3. Portability: Virtual environments make it easy to share your code and dependencies with others, as they can quickly set up a virtual environment with the same configuration as yours.
  4. Consistency: With a virtual environment, you can ensure that all developers working on a project use the same packages and dependencies, providing consistency in development and avoiding errors or conflicts caused by different package versions.
  5. Security: By keeping your project’s dependencies separate from your system-wide installation, you can avoid security vulnerabilities caused by outdated or incompatible packages.

Setting up Virtual Environment using virtualenv instead

If you want to use virtualenv instead of venv, you can use the following script:

# Upgrade pip
python.exe -m pip install --upgrade pip
pip --version

# Install virtualenv
pip install virtualenv

# Set Python version to use
set-variable -name VERSION -value "38"

# Create a virtual environment
python -m virtualenv --python python$VERSION .venv
pause
# Press Shift + Control + P to open the Command Palette and click on the Python: Select Interpreter. Select the Python interpreter which came with your virtual environment.

# Activate environment and install dependency libraries
.venv\Scripts\activate.ps1
pip install -r requirements.txt

# <<PERFORM ALL REQUIRED STEPS>>

# Deactivate environment
deactivate
pause

# Press Shift + Control + P to open the Command Palette and click on the Python: Select Interpreter. Select the Python interpreter which came with your virtual environment.
rm -r .venv

Conclusion

Hope you found this blog useful! Let me know what you think about this, if you have any suggestion of a topic you would love to see here get in touch.

If you enjoyed the writings leave your claps 👏 to recommend this article so that others can see it.

--

--

Dipan Saha

Cloud Architect (Certified GCP Professional Architect & Snowflake Core Pro)