Built with Tutify.io

    AIStudyLabs

    Different Ways to Use Python Virtual Environment

    Different Ways to Use Python Virtual Environment

    There are several ways to create virtual environments in Python, each with different tools and approaches. A virtual environment is an isolated workspace where you can install Python packages separately from your system-wide Python installation.

    Python developers can manage virtual environments in multiple ways, depending on project requirements. The built-in venv module provides lightweight isolation suitable for most projects. virtualenv extends these capabilities with additional features. For environments that require managing multiple Python versions, pyenv offers version control, while pyenv-virtualenv integrates versioning with automatic environment activation. Selecting the appropriate method ensures reproducibility, cleaner dependency management, and greater efficiency in Python development workflows.

    Let me walk you through these options:

    <a id="h-venv-built-in-python-module"></a>

    venv - Built-in Python Module

    The simplest approach uses Python's built-in venv module. venv is built into Python 3.3 and above. So this is the simplest approach if you are using Python version 3.3 and above and if you have just one version of Python installed on your system.

    If you are working on multiple projects each with different Python version then using venv alone may not be suffice. Will talk more about using multiple python versions in next sections.

    
    # Go to your project directory
    cd your-project-directory
    
    # Create virtual environment
    python -m venv .myenv
    
    #### Activate on Linux/Mac
    source .myenv/bin/activate
    
    #### Activate On Windows
    .myenv\Scripts\activate
    
    # Install Packages
    pip install requests
    

    This creates a lightweight virtual environment using the Python version that ran the command.

    When you run pip install requests, this installs packages only into**myenv**, not your global Python.

    <a id="h-virtualenv-package-support-older-python-versions"></a>

    virtualenv Package - Support Older Python Versions

    A more feature-rich option that works with older Python versions. It works almost the same as venv but has more features (e.g., faster environment creation, works with older Python versions, can create environments for multiple interpreters).

    
    # Install virtualenv
    pip install virtualenv
    
    # Create environment
    virtualenv myenv
    
    # Can specify Python version
    virtualenv -p python3.9 myenv
    
    #### Activate on Linux/Mac
    source myenv/bin/activate
    
    #### Activate On Windows
    myenv\Scripts\activate
    
    # Install Packages
    pip install requests
    

    Here, virtualenv myenv does the following

    <a id="h-pipenv-package-combines-pip-amp-virtualenv"></a>

    pipenv Package - Combines pip & virtualenv

    Combines pip and virtualenv with dependency management:

    
    # Install pipenv
    pip install pipenv
    
    # Create environment and Pipfile
    pipenv install
    
    # Install packages
    pipenv install requests
    
    # Activate shell
    pipenv shell
    
    # Run commands in environment
    pipenv run python script.py
    

    <a id="h-pyenv-managing-different-python-versions"></a>

    pyenv - Managing Different Python Versions

    Python developers often run into version conflicts, different projects (or tools) may need different versions of Python. Virtual environments (venv, virtualenv) isolate packages but not the interpreter version (Python versions).

    Imagine you have following scenarios.

    Scenario 1: Imagine****macOS/Linux ship with Python 2.7 or 3.8 as default. But modern frameworks (like Django, FastAPI, PyTorch) may need Python 3.10+.

    Scenario 2: Project A: uses TensorFlow, which works best with Python 3.9. Project B: uses new async features in Python 3.12. Without pyenv, you’d constantly break one project while fixing another.

    pyenv is a Python version manager. It helps you install, switch between, and manage multiple Python versions on the same machine.

    Without it, you’d be stuck with your system Python (often outdated), and switching versions would be messy.

    How pyenv Solves this

    Let's see how to use pyenv

    
    # Install pyenv
    brew install pyenv
    
    # Install Python versions
    pyenv install 3.10.13
    pyenv install 3.12.1
    
    # For ProjectA
    cd projectA
    pyenv local 3.10.13
    python3 --version # Returns 3.10.13
    
    # For ProjectB
    cd projectB
    pyenv local 3.12.1
    python3 --version # Returns 3.12.1
    

    Now, let' see how to use pyenv with virtualenv and pyenv with venv

    <a id="h-pyenv-with-venv-i-use-this-option-frequently"></a>

    pyenv with venv (I use this option frequently)

    If your project required Python version 3.3, you can use pyenv with built-in venv module.

    
    # Install Python versions
    pyenv install 3.10.13
    
    # For ProjectA
    cd projectA
    pyenv local 3.10.13
    python3 --version # Returns 3.10.13
    
    # Create virtual environment
    python -m venv .myenv
    source .myenv/bin/activate
    
    # Install package
    pip install requests
    

    <a id="h-pyenv-virtualenv"></a>

    pyenv-virtualenv

    If your project required Python version before 3.3, you can use pyenv with virtualenv module.

    
    # Install Python versions
    pyenv install 3.10.13
    
    # Create virtual environment
    cd projectA
    pyenv virtualenv 3.10.13 myenv
    
    # Set version to the project
    pyenv local myenv
    

    <a id="h-conclusion"></a>

    Conclusion

    Python offers several ways to manage virtual environments, each suited for different workflows and needs. The built-in venv module is lightweight, reliable, and ideal for project-local environments that keep everything self-contained. Tools like virtualenv add extra features, such as faster environment creation and broader compatibility. When version management is also a concern, pyenv excels at handling multiple Python versions, and combining it with venv or the pyenv-virtualenv plugin provides both flexibility and convenience.

    Ultimately, the best choice depends on your priorities:

    By understanding these different approaches, you can pick the right tool—or combination of tools—that makes your Python development more organized, reproducible, and efficient.