杰瑞科技汇

python dev package

Of course! The term "Python dev package" is a bit of a colloquialism, but it almost always refers to a package or tool that is essential for developing Python software, as opposed to one that is just for running it.

Let's break it down into two main categories:

  1. Core Tools: The absolute essentials for any Python developer.
  2. Development Packages (Libraries): Libraries that are used specifically for building applications, not for running them.

Core Development Tools (The "Must-Haves")

These are the tools you install to create, manage, and run your Python projects. They are not part of your application's final code but are required to build it.

A. The Python Interpreter & Virtual Environment

  • Python Interpreter: The core program that runs your Python code.

    • Installation: Download from the official Python website. Crucially, check the box "Add Python to PATH" during installation on Windows.
    • How to check: Open your terminal or command prompt and type python --version or python3 --version.
  • pip: The standard package manager for Python. It comes bundled with Python. You use it to install libraries.

    • How to check: pip --version or pip3 --version.
  • venv (Virtual Environment): This is arguably the most important concept for Python development. It creates an isolated environment for each project, preventing package version conflicts.

    • Why use it? Project A might need requests==2.25.0, while Project B needs requests==2.28.0. A virtual environment keeps these separate.
    • How to use it:
      1. Navigate to your project folder: cd my-project
      2. Create the environment: python -m venv .venv (or python3 -m venv .venv)
      3. Activate it:
        • Windows: .venv\Scripts\activate
        • macOS/Linux: source .venv/bin/activate
      4. You'll see (.venv) at the start of your command prompt, indicating it's active. Now, pip install will only install packages into this isolated environment.

B. Build Tools & Packaging

These tools are used to prepare your code for distribution (e.g., uploading to PyPI) or for deployment.

  • build: The modern, recommended tool for building your Python package.

    • Installation: pip install build
    • Usage: In your project's root directory (which has a pyproject.toml file), run python -m build. This will create a dist/ folder with your package files ready to be uploaded.
  • setuptools: The classic library for packaging Python projects. It's often used under the hood by build and other tools. You define your project's metadata (name, version, dependencies) in a setup.py or pyproject.toml file.

  • wheel: A built format for Python packages that allows for faster installation. build will automatically create a wheel file (.whl) for you.

C. Linters & Formatters (Code Quality)

These tools automatically check your code for style and syntax errors and can even fix them for you.

  • black: An opinionated code formatter. It enforces a consistent style, so you and your team never have to argue about formatting again.

    • Installation: pip install black
    • Usage: black . (to format all files in the current directory).
  • flake8: A linter that checks your Python code for style errors (according to PEP 8) and programming errors.

    • Installation: pip install flake8
    • Usage: flake8 .
  • mypy: A static type checker for Python. It helps you catch bugs before you even run your code by checking if your functions use the correct types of variables.

    • Installation: pip install mypy
    • Usage: mypy .

Development Packages (Libraries)

These are libraries that you install into your virtual environment to build your application. They are not typically dependencies for the final application itself, but for the development process.

A. Testing Frameworks

  • pytest: The de-facto standard for testing in Python. It's powerful, easy to use, and has a rich ecosystem of plugins.

    • Installation: pip install pytest
    • Usage: Create a file named test_my_app.py and run pytest in your terminal.
  • unittest: Python's built-in testing framework. It's more rigid and less "Pythonic" than pytest but is always available.

B. Mocking Libraries

Used in testing to create "fake" objects that mimic the behavior of real objects, allowing you to test components in isolation.

  • unittest.mock: Python's built-in mocking library, part of the unittest framework.
  • pytest-mock: A very popular plugin for pytest that provides a more powerful and easier-to-use mocker.

C. Documentation Generators

  • Sphinx: The most popular tool for creating beautiful documentation from your docstrings. It's what powers Read the Docs.

    • Installation: pip install sphinx
    • Usage: sphinx-quickstart to initialize a project.
  • MkDocs: A simpler, more modern alternative that uses Markdown files. It's very fast and easy to set up.

    • Installation: pip install mkdocs
    • Usage: mkdocs new my-project and mkdocs serve.

Putting It All Together: A Typical Workflow

  1. Setup Project:

    mkdir my-awesome-app
    cd my-awesome-app
    python -m venv .venv
    source .venv/bin/activate  # On Windows: .venv\Scripts\activate
  2. Install Core Dev Tools:

    pip install black flake8 mypy pytest build
  3. Write Code: Write your application code in files like app.py.

  4. Format and Lint Code (Before committing):

    black .
    flake8 .
    mypy .
  5. Write Tests: Create a tests/ directory and write test files.

  6. Run Tests:

    pytest
  7. Build Package for Distribution:

    # Create a pyproject.toml file first!
    python -m build

Summary Table

Category Purpose Example Tools/Packages
Core Tools The foundation for development python, pip, venv
Build & Packaging Prepare code for distribution build, setuptools, wheel
Code Quality Enforce style and catch errors black, flake8, mypy
Testing Verify code works correctly pytest, unittest, pytest-mock
Documentation Generate project documentation Sphinx, MkDocs

When someone says "install the dev package," they are usually referring to a tool like black, flake8, or pytest, which are installed to aid in the development process and are not dependencies of the final application.

分享:
扫描分享到社交APP
上一篇
下一篇