Benefit
As a Python developer, keeping your project dependencies organized can be a real pain. Luckily, venv is here to save the day!
Using venv has many benefits. It allows you to create a separate virtual environment for each of your projects, with its own set of dependencies. This means that you can work on multiple projects without worrying about version conflicts or cluttering up your global Python environment with unnecessary packages.
Creating a Virtual Environment venv
To get started, you’ll need to have Python installed on your computer. If you’re not sure whether you have Python installed, just type python --version in your terminal to check.
Once you have Python installed, you can create a new virtual environment using the venv module. Simply navigate to the directory where you want to create your environment, and type:
# macOS/UNIX
python3 -m venv myenv# Windows
python -m venv myenvThis will create a new directory called myenv in your current directory, which will contain your virtual environment.
Activation
To activate your virtual environment, navigate to the directory where your environment is located and run
# macOS/UNIX
source myenv/bin/activate# Windows
activate.batYou’ll know your environment is active when you see its name in your terminal prompt.
Deactivation
To deactivate your virtual environment and return to your global Python environment, just type:
deactivateAnd that’s it! With venv, you can keep your Python projects organized and avoid version conflicts.
Reference
This post was originally posted in my Medium page in Mar 2024.
