Login Sign Up
Python Virtual Environments
Courses / python Complete Course / Python Virtual Environments
Chapter 48 🟡 Intermediate

Python Virtual Environments

Apply your skills with a real-world coding challenge. Try to solve it yourself first!

Here is a practical Coding Challenge based on concepts taught in a Python Virtual Environments materials.

Coding Challenge: The High-Performance Environment Architect

Problem Description Imagine you have just been hired as a Senior DevOps Engineer at massive tech company. The company’s codebase relies heavily on a legacy environment manager, Poetry. While Poetry is just fantastic because it securely locks down exact package versions in a poetry.lock file it's written in pure Python. So, installing massive machine learning dependencies takes several minutes, causing severe bottlenecks in your team's CI/CD deployment pipeline, and

your challenge is to architect a hybrid deployment script that combines the strict, safe project management of Poetry with the blazing-fast Rust-based execution speed of uv. You must write bash script that uses uv to automatically download a specific Python version, creates an isolated virtual environment (to avoid "Dependency Hell"), exports Poetry's pre-calculated lockfile, fast installs the dependencies using uv. Then finalizes the local package setup.

Difficulty Level: Advanced

Input & Output Specifications * Input: * project directory containing a standard pyproject.toml and a pre-resolved poetry.lock file. * Output: * fully functioning .venv directory built strictly with Python 3.12.0. * The script must successfully install all dependencies in fractions of second. * The custom local package and its entrypoint scripts must be fully configured and ready to use.


Starter Code Boilerplate

#!/bin/bash
# Hybrid Poetry + uv Environment Setup Script

echo "Starting lightning-fast hybrid environment setup..."

# TODO 1: Use uv to automatically download Python 3.12.0 and create a virtual environment
# Hint: Use the --python flag.
# Your code here:


# TODO 2: Activate the newly created virtual environment
# Your code here:


# TODO 3: Export the strictly resolved poetry.lock dependencies into a requirements.txt file
# Your code here:
poetry export -f requirements.txt --output requirements.txt


# TODO 4: Use uv's blazing-fast pip backend to install the requirements.txt
# Your code here:


# TODO 5: Run a final poetry command to install the custom package itself and its entrypoint scripts
# Your code here:


echo "Environment successfully built and isolated!"

Hints * The Rust-Based Superpower: Standard environment tools force you to download Python installers manually, while with uv, you can simply run uv venv --python 3.12.0 and it will securely fetch the exact CPython 3.12.0 binaries from the internet at the fly. * Activating a Toolbox: Remember that creating the .venv folder isn't enough; you must temporarily rewrite your terminal's internal paths by running source .venv/bin/activate. * A Lockfile Export: Poetry keeps all resolved dependencies in its poetry.lock file, meaning a poetry export command is incredibly fast. * Speeding Up Installation: Instead of running the standard pip install, use uv pip install -r requirements.txt to inject raw speed of the Rust backend into your dependency installation. * A Final Touch: uv handles external dependencies brilliantly but it doesn't configure your local project; you must execute supplementary poetry install at the very end. This final step is quick and solely focuses on installing the custom package itself and wiring up its entrypoint scripts.


Test Cases

  1. Test Case 1 (On-the-Fly Python Provisioning):
  2. Scenario: Execute the script on fresh machine that doesn't have really Python 3.12 installed globally.
  3. Expected Behavior: uv identifies the missing version, securely reaches out to the internet, downloads the CPython 3.12.0 binaries, and builds isolated virtual environment without failing or requiring a manual installer.
  4. Test Case 2 (Execution Speed Validation):
  5. Scenario: Compare the execution time of this hybrid script against a standard poetry install command on project with massive machine learning dependencies.
  6. Expected Behavior: The hybrid script executes in a fraction of the time proving that swapping out pure Python dependency resolution for uv's Rust-based backend successfully eliminates the CI/CD pipeline bottleneck.
  7. Test Case 3 (Local Package Entrypoints Check):
  8. Scenario: After the script finishes a developer attempts to trigger custom command-line tool defined on the project's pyproject.toml.
  9. Expected Behavior: A command executes perfectly because the final poetry install line correctly linked local package and its entrypoint scripts into an isolated .venv toolbox.

Loading sandbox workspace environment...

Verify Your Solution

Write your solution in the compiler, run it to verify output, then click below to verify.

Learn Together
Session active! Discuss with other learners.
No notes yet. Select text in the concept body to add a note.