[Python Basics] Python virtual environment: venv and pipenv

A Python virtual environment is an independent Python workspace that helps you isolate and manage various Python packages and dependencies in different projects. This article will detail two popular Python virtual environment management tools: venv and pipenv. We'll dive into how, why, and their pros and cons, while showing you how to use these tools to create and manage virtual environments with real-world code examples.

1. Introduction to Python virtual environment

A Python virtual environment is an independent Python workspace that helps you isolate and manage various Python packages and dependencies in different projects. Using a virtual environment avoids dependency conflicts between multiple projects, allowing you to manage and switch projects more easily. This article will detail two popular Python virtual environment management tools: venv and pipenv.

2. Introduction to venv

2.1. The principle of venv

venv is a virtual environment tool that comes with the standard library of Python 3.3 and higher. It can create an independent Python runtime environment, including an independent Python interpreter, an independent site-packages directory, and an independent pip tool. By using venv, you can manage your project's dependencies without affecting your global Python installation.

2.2. How to use venv

Create a virtual environment:

python -m venv myenv

Activate the virtual environment:
Windows:

myenv\Scripts\activate

macOS/Linux:

source myenv/bin/activate

Exit the virtual environment:

deactivate

2.3. venv example

Here is an example of using venv to create and manage virtual environments:

# 创建虚拟环境
python -m venv myenv

# 激活虚拟环境
source myenv/bin/activate

# 安装所需的包
pip install requests

# 运行Python脚本
python my_script.py

# 退出虚拟环境
deactivate

3. Introduction to pipenv

3.1. The principle of pipenv

pipenv is a more advanced Python virtual environment and dependency management tool. It combines the functionality of pip and venv to provide a cleaner way to manage a project's Python environment and dependencies. pipenv tracks the dependencies of the project through the Pipfile and Pipfile.lock files to ensure the repeatability and consistency of the project.

3.2. How to use pipenv

Install pipenv:

pip install pipenv

Create a virtual environment and install dependencies:

pipenv install requests

Install development dependencies:

pipenv install --dev pytest

Run the command in the virtual environment:

pipenv run python my_script.py

Activate the shell of the virtual environment:

pipenv shell

Exit the shell of the virtual environment:

exit

3.3. pipenv example

Here is an example of using pipenv to create and manage virtual environments:

# 安装pipenv
pip install pipenv

# 创建虚拟环境并安装依赖项
pipenv install requests

# 安装开发依赖项
pipenv install --dev pytest

# 运行Python脚本
pipenv run python my_script.py

# 激活虚拟环境的shell
pipenv shell

# 退出虚拟环境的shell
exit

4. Comparison between venv and pipenv

  • venv is part of the Python standard library, while pipenv needs to be installed separately.
  • pipenv provides a more concise command line interface, combining the functions of pip and venv.
  • pipenv tracks project dependencies through Pipfile and Pipfile.lock files, improving project repeatability and consistency.
  • pipenv is more suitable for projects with complex dependencies, while venv is more suitable for simple projects and rapid prototyping.

5. Summary

This article introduces the concept of Python virtual environment in detail and how to use venv and pipenv to create and manage virtual environments. While venv and pipenv differ in functionality, they both help you manage dependencies of your Python projects more efficiently. You can choose the right tool based on your project needs and personal preferences.

6. References

  1. Python official documentation: venv
  2. Pipenv official documentation: Pipenv: Python Development Workflow for Humans
  3. Real Python:Pipenv: A Guide to the New Python Packaging Tool

Guess you like

Origin blog.csdn.net/qq_33578950/article/details/130297330