VSCode Settings for Python & ML: Virtual Environment Setup

VSCode Settings for Python & ML: Virtual Environment Setup

Let’s go through the steps to set up a Python virtual environment in VSCode, which is essential when working with different versions of Python and maintaining package dependencies for machine learning (ML) projects.

Prerequisite:

Before we start, ensure you have Anaconda or Miniconda installed on your system. Conda helps manage Python environments efficiently, which is particularly useful when handling ML libraries.

Steps to Create a Virtual Environment in VSCode:

  1. Navigate to your Project Folder and Create a Virtual Environment:
    • Use the conda command to create a new environment with a specific version of Python (here, Python 3.12):
    conda create -p venv python==3.12
    • This command creates a virtual environment in a folder named venv.
  2. Activate the Virtual Environment:
    • Once the virtual environment is created, you can activate it using the command:
    conda activate <your_directory_path>/venv
  3. Install Required Python Packages:
    • After activating the environment, you can install the required libraries by creating or using a requirements.txt file.

Here’s an example of how your requirements.txt might look for a basic ML setup:

Use below command to install required libraries from requirements.txt or you can follow step 5 to install specific library

pip install -r requirements.txt

5. Set Up Jupyter Notebooks in VSCode:

To use Jupyter notebooks within VSCode, you need to install the ipykernel package inside the virtual environment

pip install ipykernel

open the Jupyter notebook in VSCode (example.ipynb) using the .ipynb extension.

To open VScode Editor using command line:

  • Open your terminal and navigate to the folder where you want to practice or already have your project files.Use the following command to open the folder in VSCode:
code .

By following these steps, you’ll be ready to run and manage Python and ML projects efficiently in VSCode using virtual environments. This setup helps in keeping dependencies isolated for each project and ensures reproducibility.

Feel free to adjust these settings based on your specific project needs, and happy coding!


vamsi manyam Avatar