Python Virtual Environment Setup
Creating isolated Python environments is crucial for managing project dependencies and avoiding conflicts. This challenge focuses on the fundamental skill of creating and activating a Python virtual environment using the built-in venv module.
Problem Description
Your task is to write a series of commands (or a script that executes these commands) to create a new Python virtual environment for a specific project. This environment should be activated and then a simple package should be installed within it to verify its isolation.
What needs to be achieved:
- Create a new, isolated Python virtual environment.
- Activate this virtual environment.
- Install a specified package within the activated environment.
- Demonstrate that the package is installed only in the virtual environment.
Key Requirements:
- Use Python's built-in
venvmodule. - The virtual environment should be created in a designated directory.
- The activation process should be correctly described for a common operating system.
- A specific, lightweight package should be installed.
Expected Behavior: When the commands are executed sequentially, a new directory containing the virtual environment will be created. Upon activation, your shell prompt should indicate that you are inside the virtual environment. After installing the package, running a command to list installed packages should show the newly installed package, and running the same command outside the activated environment should not show the package.
Important Edge Cases:
- What happens if the virtual environment directory already exists? (The
venvmodule handles this gracefully by default.) - Ensuring the correct activation script is used for different operating systems (e.g., Windows vs. macOS/Linux).
Examples
Example 1: Creating and activating on macOS/Linux
Input:
Project Directory: my_python_project
Virtual Environment Name: venv
Package to Install: requests
Commands to Execute:
1. Navigate to the project directory: cd my_python_project
2. Create the virtual environment: python3 -m venv venv
3. Activate the environment: source venv/bin/activate
4. Install the package: pip install requests
5. Verify installation: pip freeze
6. Deactivate the environment: deactivate
7. Verify deactivation: pip freeze
Output:
(After step 3, the prompt changes to something like '(venv) user@hostname:~/my_python_project$')
(After step 5, output includes 'requests==X.Y.Z')
(After step 7, output does NOT include 'requests==X.Y.Z' unless it was globally installed)
Explanation:
The commands first create a virtual environment named 'venv' inside 'my_python_project'.
Activating it modifies the shell's PATH.
'requests' is installed only within this isolated environment.
'pip freeze' confirms its presence when activated and its absence when deactivated.
Example 2: Creating and activating on Windows (Command Prompt)
Input:
Project Directory: my_python_project
Virtual Environment Name: venv
Package to Install: requests
Commands to Execute:
1. Navigate to the project directory: cd my_python_project
2. Create the virtual environment: python -m venv venv
3. Activate the environment: venv\Scripts\activate.bat
4. Install the package: pip install requests
5. Verify installation: pip freeze
6. Deactivate the environment: deactivate
7. Verify deactivation: pip freeze
Output:
(After step 3, the prompt changes to something like '(venv) C:\Users\User\my_python_project>')
(After step 5, output includes 'requests==X.Y.Z')
(After step 7, output does NOT include 'requests==X.Y.Z' unless it was globally installed)
Explanation:
Similar to macOS/Linux, but uses Windows-specific activation script path and command.
Constraints
- The Python version used to create the virtual environment should be Python 3.6 or higher.
- The user is assumed to have Python installed and accessible in their system's PATH.
- The solution should focus on the command-line interface.
- The chosen package for installation (
requests) is guaranteed to be available on PyPI.
Notes
- The
venvmodule is the standard way to create virtual environments in modern Python. - Consider how you would present this as a runnable script or a clear set of instructions for a user.
- Pay close attention to the activation script path, as it differs between operating systems.
- The
pip freezecommand is a good way to list installed packages.