A Guide to Python Virtual Environments
Getting Started with Conda Virtual Environments
Python virtual environments allow you to control software dependencies in Python code. Software dependency means that a given piece of software requires one or more other software package(s)/libraries to run successfully. For example Tensorflow, a deep learning library in Python, requires another Python package called NumPy to run.
Virtual environments are useful ways of ensuring that the correct package/library versions are consistently used every time the software runs. This helps isolate software projects from potentially conflicting libraries and packages that are installed on an operating system. This is helpful because conflicting software packages may result in a piece of software failing.
Further, virtual environments help ensure that the results from running code are reproducible. Reproducibility is an essential part of software development and software testing, because it guarantees that software programs run and work as intended. If a software program’s results can’t be reproduced, it will not be able to reliably perform the task it was developed to complete.
When to Use a Virtual Environment?
Working within a Python virtual environment is generally a good idea when developing Python code. This is because virtual environments provide ways to control the behavior of a software program. If code is written to generate specific output, given a known input, the same output should be reliably generated for that input whether the code is run on my computer, a remote machine, or someone else’s laptop. The Developing software without a virtual environment defined increases the risk of the code failing or producing undesired results. Using virtual environments is even more important when you’re working with a larger python development project that includes many libraries, files, and Python scripts. This is because as code becomes more complex, it is even more important to bookkeep software dependencies as there is an increased risk of package mismanagement and consequently error producing code.
If you’re a developer working on multiple Python projects in parallel, you should use distinct virtual environments for each project. This…