Thursday, December 12, 2024

Using Python Virtual Environments

tl;dr:

Setup:

mkdir my-new-project
cd my-new-project
python -m venv env

Linux Usage:

source env/bin/activate

Powershell Usage:

./env/Scripts/Activate.ps1

Either usage

pip install requests # and anything else you need

Exit

deactivate

Imagine you have a bunch of different school projects, and each one needs its own set of supplies. If you mix all the supplies together, it can get really messy and confusing. Python virtual environments help keep things organized, just like having separate boxes for each project.

Here are some benefits of using Python virtual environments:
  • Organization: Each project gets its own “box” (virtual environment) with all the tools and libraries it needs. This way, different projects don’t mess with each other.
  • Avoiding Conflicts: Sometimes, different projects need different versions of the same tool. Virtual environments make sure each project uses the right version without causing problems.
  • Easy Sharing: If you want to share your project with a friend, you can include the virtual environment. This way, your friend gets all the right tools and versions to run your project without any issues.
  • Safety: You can try out new tools or updates in a virtual environment without worrying about breaking other projects. It’s like having a safe space to experiment.
  • Clean Up: When you’re done with a project, you can easily delete its virtual environment. This keeps your computer neat and tidy.

In short, Python virtual environments help you stay organized, avoid problems, and make it easier to share and manage your projects.

No comments:

Post a Comment