Set Up a Virtual Environment as an ML Engineer

Of all the time spent on a machine learning project, how much of it is spent on complex algorithms versus just trying to get a project to run? A significant, yet often overlooked, aspect of ML Engineering is managing project setups and dependencies. So, in this article, I’ll take you through a guide to set up a virtual environment as an ML Engineer.

A Guide to Set Up a Virtual Environment as an ML Engineer

To set up a virtual environment as an ML Engineer, we used Python’s built-in venv module. It is lightweight, requires no extra installation, and is perfect for 95% of use cases.

Step 1: Creating a Virtual Environment

First, navigate to the root folder of your project in the terminal. This is where the code, notebooks, and data folders are stored.

Once you are there, run the following command:

python -m venv env

Here, Python is instructed to run the venv module and create a new virtual environment in a folder named env. This env folder now contains a lightweight copy of the Python interpreter and basic tools for installing other packages.

You can name it anything (my_project_env, venv, etc.), but env is a common and simple choice.

Step 2: Step Inside and Activate It

Your environment is created, but it is not yet active. You must turn it on for your current terminal session. To activate on macOS or Linux:

source env/bin/activate

To activate on Windows:

env\Scripts\activate

You will know it worked because your terminal prompt will change to show the environment’s name, something like (env) C:\Users\YourUser\MyProject>. This is a signal that you are now working in an isolated environment. Any Python package you install from now on will only exist within this env folder.

Step 3: Install Only What You Need

The next step is to install the core libraries for your ML projects. Here’s how:

pip install numpy pandas scikit-learn matplotlib

Note that we are using pip, not conda or anything else. Because venv is active, pip knows to install these packages inside the env folder, leaving your global Python installation untouched.

Resist the urge to install every library that you might need. Start with essentials. A lean environment is fast and manageable. You can always install more applications later.

So, this is how to set up a virtual environment as an ML Engineer.

Final Words

Before writing a single line of code for a new project, perform this ritual. It may initially seem like an extra step, but the time saved in debugging and frustration is immeasurable. Mastering your environment is the first real step in the transition from being a student to being a professional engineer. It is a mark of discipline that separates the code that works from the reliable code.

I hope you liked this article on a guide to set up a virtual environment as an ML Engineer. Feel free to ask valuable questions in the comments section below. You can follow me on Instagram for many more resources.

Aman Kharwal
Aman Kharwal

AI/ML Engineer | Published Author. My aim is to decode data science for the real world in the most simple words.

Articles: 2074

Leave a Reply

Discover more from AmanXai by Aman Kharwal

Subscribe now to keep reading and get access to the full archive.

Continue reading