How to Create a Python Virtual Environment with uv
How to Create a Python Virtual Environment with uv

How to Create a Python Virtual Environment with uv

Ever get the feeling that your Python projects are like siblings squabbling over the same toys? One needs a shiny new package while the other is content with the old favorite. Before you know it, dependency issues arise and your projects are in chaos. Fear not, virtual environments are here to save the day and uv is the hero we need but don’t deserve.

Why Virtual Environments?

Imagine this: You have a Python 3.10 backend application with packages a2.1, b2.2, and c2.3 installed system-wide. Everything is hunky-dory until you start a new project that also uses Python 3.10 but needs a1.2, b2.2, and c2.1. Chaos ensues! Virtual environments solve these problems by isolating dependencies for each project. This means you can have different packages and Python versions without causing a sibling rivalry.

Installing uv

First things first, let’s install uv. Depending on your operating system, you can use one of the following commands:

  • Linux or Mac:
curl -LsSf https://astral.sh/uv/install.sh | sh
  • Mac (via Homebrew):
brew install uv
  • Windows:
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
  • Any OS (via pip):
pip install uv

After installing uv, verify the installation with:

uv --version

You should see something like uv 0.1.44. Congrats! uv is now your new best friend.

Creating a Project and a Virtual Environment

Let’s get down to business. Create a new project directory:

mkdir my_uv_project
cd my_uv_project

Next, create a virtual environment within this directory:

uv venv my_env

You should see something like:

Using Python 3.9.6 interpreter at: /Library/Developer/CommandLineTools/usr/bin/python3
Creating virtualenv at: my_env
Activate with: source my_env/bin/activate

Activating Your Environment

Time to bring your environment to life:

source my_env/bin/activate

To verify it’s a clean slate, check the installed packages:

uv pip list

Your output should be blissfully empty.

Installing Packages

Let’s install pandas:

uv pip install pandas

You should see:

Resolved 6 packages in 143ms
Installed 6 packages in 76ms
 + numpy==1.26.4
 + pandas==2.2.2
 + python-dateutil==2.9.0.post0
 + pytz==2024.1
 + six==1.16.0

Creating a Second Virtual Environment

To test isolation, create another environment:

uv venv my_second_env
source my_second_env/bin/activate
uv pip install pandas==2.1.0 flask

Verify the installed packages:

uv pip list

You’ll see a different set of packages, proving that my_env and my_second_env are blissfully unaware of each other.

Different Python Versions

Want to play with Python 3.11? Create a new environment:

uv venv 3rd_env --python 3.11
source 3rd_env/bin/activate
uv pip install pandas

Switching and Removing Environments

Switch between environments with:

deactivate
source my_second_env/bin/activate

List all environments in your project directory:

ls

Remove an environment with:

rm -rf my_second_env

Overcoming uv Limitations

While uv is fantastic, it has its limits. It only manages Python dependencies and lacks containerization capabilities. For larger projects or those with many dependencies, consider using Earthly. It simplifies dependency management and ensures consistency across environments.

Conclusion

Virtual environments are a lifesaver for managing dependencies and avoiding conflicts. uv makes creating and managing these environments a breeze. For more complex projects, consider using tools like Earthly to handle both Python and system-wide dependencies.

Happy coding!

Leave a response to this article by providing your insights, comments, or requests for future articles.

Share the articles with your friends and colleagues on social media.

Let’s Get in Touch! Follow me on :

>GitHub: @gajanan0707

>LinkedIn: Gajanan Rajput

>Website: https://mrcoder701.com

>YouTube: mrcoder701

> Instagram: mr_coder_701

Show 1 Comment

1 Comment

  1. When I originally commented I clicked the -Notify me when new comments are added- checkbox and now each time a comment is added I get four emails with the same comment. Is there any way you can remove me from that service? Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *