杰瑞科技汇

python vritualenv

Of course! Let's dive deep into python virtualenv. It's a fundamental tool for any Python developer.

python vritualenv-图1
(图片来源网络,侵删)

What is a Virtual Environment and Why Do You Need It?

Imagine you're working on two different projects:

  1. Project A: An older web application that requires Django==3.2.
  2. Project B: A new data science project that requires the latest Django==4.2.

If you install both projects' packages globally, you'll have a conflict. Installing Django==4.2 will break Project A, and vice-versa. This is the classic "dependency hell."

A virtual environment is an isolated, self-contained Python environment. Think of it as a lightweight, portable "sandbox" for each of your projects. Inside this sandbox:

  • You have a separate copy of the Python interpreter.
  • You have a separate set of installed packages (site-packages).
  • The packages installed in one environment don't affect any other environment.

This allows you to manage dependencies for each project independently and safely.

python vritualenv-图2
(图片来源网络,侵删)

Key Concepts

  • venv (Python's built-in module): Since Python 3.3, Python comes with a built-in module called venv. It's the standard, recommended way to create virtual environments. virtualenv is a third-party package that was the original popular choice and offers a few extra features, but venv is sufficient for most use cases.
  • pip: The Python Package Installer. It's used to install packages inside an activated virtual environment.
  • requirements.txt: A file that lists all the packages and their exact versions needed for a project. This ensures that anyone else (or you, in the future) can recreate the exact same environment.

Step-by-Step Guide using venv

Here’s how to use the built-in venv module.

Step 1: Create a Virtual Environment

Navigate to your project's root directory in your terminal. Then, run the following command:

# This creates a new folder named 'venv' containing the isolated environment
python -m venv venv
  • python -m venv: This is the command to run the venv module.
  • venv: This is the name of the folder that will be created to hold your environment. You can name it anything you like (e.g., .env, my_project_env), but venv or .venv are common conventions.

After running this, you'll see a new venv folder in your project directory.

Step 2: Activate the Virtual Environment

This is the most crucial step. Activating the environment modifies your shell's path so that python and pip point to the executables inside your venv folder.

On macOS / Linux:

source venv/bin/activate

On Windows (Command Prompt):

venv\Scripts\activate

On Windows (PowerShell):

.\venv\Scripts\Activate.ps1

How do you know it's activated? Your command prompt will change to show the name of the active environment. It looks like this:

(venv) C:\Users\YourUser\your_project>

The (venv) prefix is your confirmation.

Step 3: Install Packages

Now that your environment is active, any pip command will install packages into this isolated environment, not globally.

# Install a package
pip install requests
# Install a specific version
pip install Django==4.2.0
# Install packages from a requirements.txt file (more on this next)
pip install -r requirements.txt

Step 4: Deactivate the Virtual Environment

When you're done working on your project, you can exit the environment by simply running:

deactivate

Your command prompt will return to normal, and python/pip will now refer to your system's global Python again.


The requirements.txt Workflow

This is the standard for sharing projects.

Generate a requirements.txt file

Inside your activated virtual environment, run:

pip freeze > requirements.txt

The pip freeze command lists all installed packages and their exact versions. The > operator redirects this list into a file named requirements.txt.

Your requirements.txt file will look something like this:

asgiref==3.7.2
Django==4.2.0
sqlparse==0.4.4
...

Share Your Project

Now, you can share your project with others. You just need to give them:

  1. Your project's source code.
  2. The requirements.txt file.

Recreate the Environment

The other person can then set up the project like this:

# 1. Navigate to the project directory
cd my_project
# 2. Create a new virtual environment
python -m venv venv
# 3. Activate it
source venv/bin/activate  # (or the appropriate command for their OS)
# 4. Install all dependencies from the file
pip install -r requirements.txt

This ensures they have the exact same versions of the packages you used, preventing bugs and inconsistencies.


venv vs. virtualenv

Feature venv (Built-in) virtualenv (Third-party)
Availability Included with Python 3.3+ Requires separate installation (pip install virtualenv)
Features Basic, standard functionality More features, like creating environments from specific Python versions (virtualenv -p /path/to/python3.9)
Recommendation Recommended for most users. It's the standard, is actively maintained by the Python core team, and is all you need for 99% of projects. Use if you need features not available in venv, or if you're working on older Python 2 projects.

Common Problems and Solutions

  1. Problem: 'venv' is not recognized... (Windows)

    • Solution: Make sure you have added Python to your system's PATH during installation. You can check by running python --version in a new terminal window. If it works, the issue is likely with your current terminal session. Try closing and reopening it.
  2. Problem: ModuleNotFoundError: No module named 'venv'

    • Solution: This means your Python installation doesn't include the venv module. This is common on some Linux distributions (like Ubuntu) where Python is split into multiple packages. You need to install the python3-venv package using your system's package manager.
      • Ubuntu/Debian: sudo apt-get install python3-venv
      • Fedora: sudo dnf install python3-virtualenv
      • macOS: If you installed Python via Homebrew, it should already be included.
  3. Problem: (ModuleNotFoundError: No module named 'pip') inside the venv

    • Solution: Sometimes, the venv creation doesn't include pip. You can fix this by running:
      python -m ensurepip --upgrade

Best Practices

  • Never commit your venv folder to Git. Add it to your .gitignore file.

  • Always use a virtual environment. Make it a habit for every new Python project.

  • Pin your dependencies. Always use pip freeze > requirements.txt to keep track of your project's exact needs.

  • Use a .gitignore file. Here's a simple Python .gitignore template:

    # Virtual Environment
    venv/
    .venv/
    # Python cache
    __pycache__/
    *.py[cod]
    *$py.class
    # Distribution / packaging
    .Python
    build/
    develop-eggs/
    dist/
    downloads/
    eggs/
    .eggs/
    lib/
    lib64/
    parts/
    sdist/
    var/
    wheels/
    pip-wheel-metadata/
    share/python-wheels/
    *.egg-info/
    .installed.cfg
    *.egg

Popular Alternatives to venv

  • conda: Part of the Anaconda distribution. It's a package, environment, and dependency manager that works with any language, not just Python. It's very popular in data science because it can manage non-Python dependencies (like C libraries) that can be tricky for pip.
  • pipenv: Aims to bring the best of all worlds by combining pip and virtualenv into a single tool. It automatically creates and manages a virtual environment and uses a Pipfile (instead of requirements.txt) to manage dependencies.
  • poetry: A modern, comprehensive tool for dependency management and packaging. It also manages virtual environments, publishing packages, and more. It's gaining a lot of traction in the Python community.
分享:
扫描分享到社交APP
上一篇
下一篇