Let's say you're working on a Project A using pandas version 1.3. You have a script that adds new rows to a DataFrame using the append() method, which was a common way to do this:
df = df.append(new_row, ignore_index=True)
Suppose you upgraded and started using pandas 2.0 for your new Project B. In this version, the append() method was removed. Now if you try to run Project A with pandas 2.0, you'll get an error like this:
AttributeError: 'DataFrame' object has no attribute 'append’
The append() method was convenient in older versions of pandas, but it was removed in later versions (to append rows to a df, we instead need to use concat()). However, it will be painful to change every append() calls to concat(). Is it possible to still use the append() method? Virtual environment allows you to do it.
A Python virtual environment is a tool that helps manage dependencies for different Python projects by isolating them from each other. When working on multiple projects, each might require different versions of the same package or even different versions of Python itself.

By specifying versions of dependencies (packages) in requirements.txt, we effectively install dependencies of particular version, which is separated from dependencies installed in global setting. Therefore, a virtual environment ensures that the dependencies for one project do not interfere with those of another.
Dependency Management: Different projects may need different versions of the same libraries. A virtual environment allows each project to have its own set of dependencies, preventing conflicts.
Avoiding System-Wide Changes: Installing packages globally can lead to version conflicts with system-wide Python packages, potentially breaking other projects or even system utilities.
Portability: Virtual environments can be easily recreated using a requirements.txt file, which lists all the dependencies. This makes it easy to share your project or move it to another machine.
In our earlier anecdote, we add pandas==1.3.0 into requirements.txt in order to install the version that can use append() method in this local setting.
Consistency: Ensures that all developers on a project are using the same versions of packages, reducing "works on my machine" issues.