杰瑞科技汇

Python .gitignore文件如何正确配置?

Of course! Here is a comprehensive and well-commented .gitignore file for Python projects, along with explanations for each section and instructions on how to use it.

Python .gitignore文件如何正确配置?-图1
(图片来源网络,侵删)

The Recommended .gitignore File

This file is a great starting point for most Python projects, including web applications (Django, Flask), data science scripts, and standard libraries.

#
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
#
# C extensions
*.so
#
# 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
MANIFEST
#
# PyInstaller
*.manifest
*.spec
#
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
#
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
#
# Virtual environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
#
# Spyder project settings
.spyderproject
.spyproject
#
# Rope project settings
.ropeproject
#
# mkdocs documentation
/site
#
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
#
# Pyre type checker
.pyre/
#
# IDE and Editor Files
# (Optional, uncomment if you use them)
# VS Code
.vscode/
# PyCharm
.idea/
# Sublime Text
*.sublime-project
*.sublime-workspace
# Jupyter Notebook / JupyterLab
.ipynb_checkpoints
# macOS
.DS_Store
# Windows
Thumbs.db
ehthumbs.db
Desktop.ini
# Project-specific files
# (Add your own project-specific ignores here)
# Example: a local configuration file
# local_config.py

Explanation of Each Section

Here’s a breakdown of what each part of the .gitignore file does, so you can understand and customize it for your needs.

Byte-Compiled & Optimized Files

__pycache__/
*.py[cod]
*$py.class
  • What they are: When you run a Python file, Python compiles it into bytecode for faster execution next time. These files are stored in a __pycache__ directory (or with .pyc, .pyo, or .pyd extensions).
  • Why ignore them: They are automatically generated by Python, are not needed to run the code, and can clutter your repository. They will be regenerated on any machine that has Python installed.

C Extensions

*.so
  • What it is: A shared object file, which is a compiled C extension for Python.
  • Why ignore it: Like bytecode, these are compiled artifacts. They are specific to the operating system and architecture, so including them can cause major problems for other collaborators.

Distribution & Packaging Files

.Python
build/
develop-eggs/
dist/
downloads/
eggs/
...
  • What they are: These are directories and files created by tools like setuptools, pip, and build when you are preparing your project for distribution (e.g., running python setup.py sdist or python -m build).
  • Why ignore them: These are build artifacts. Your collaborators should install your package from your source code using a tool like pip install -e . (for development) or pip install . (for an installation), not from these pre-built files.

PyInstaller

*.manifest
*.spec
  • What they are: PyInstaller is a tool that packages Python programs into standalone executables. These are files generated by PyInstaller.
  • Why ignore them: They are build artifacts specific to the PyInstaller process.

Unit Test & Coverage Reports

htmlcov/
.tox/
.nox/
.coverage
...
  • What they are: These are outputs from testing and code coverage tools like pytest, nose, tox, and coverage.py.
  • Why ignore them: They are generated reports that show how much of your code is tested. They are not part of your source code and can be very large. tox and nox create entire virtual environments for testing, which should never be committed.

Virtual Environments

.env
.venv
env/
venv/
ENV/
...
  • What they are: These are the standard directory names for Python virtual environments.
  • Why ignore them: A virtual environment contains all the dependencies for your project. Committing it would be redundant (dependencies are listed in requirements.txt or pyproject.toml) and would bloat your repository to an enormous size. Crucially, you should also add .env to ignore environment variable files, which often contain secrets.

IDE and Editor Files

# VS Code
.vscode/
# PyCharm
.idea/
# Sublime Text
*.sublime-project
...
  • What they are: These are configuration files specific to your Integrated Development Environment (IDE) or code editor.
  • Why ignore them: They contain personal settings like window layouts, installed extensions, and debugger configurations. These settings are specific to you and your machine and will cause unnecessary merge conflicts for your team. Note: You might want to keep a subset of these (e.g., .vscode/settings.json if you have agreed-upon team-wide settings), but it's safer to ignore the whole directory.

OS & System Files

# macOS
.DS_Store
# Windows
Thumbs.db
...
  • What they are: These are hidden system files created by macOS and Windows to store folder view settings and thumbnails.
  • Why ignore them: They have nothing to do with your code and can cause annoying version control noise.

How to Use a .gitignore File

  1. Create the File: In the root directory of your Python project (the same directory as your .git folder), create a new file named .gitignore.

  2. Copy and Paste: Copy the content from the file above and paste it into your new .gitignore file.

    Python .gitignore文件如何正确配置?-图2
    (图片来源网络,侵删)
  3. Add Project-Specific Rules: The most important step. Think about what files are specific to your project that you don't want to commit.

    • Example: If you have a config.py file that contains database passwords, you don't want to commit it. Instead, you might have a config.example.py file with dummy values. You would add config.py to your .gitignore.
    • Add your custom rules to the bottom of the file.
  4. Commit the .gitignore File: Commit the .gitignore file to your repository as soon as you create it. This ensures that all future files you add will be correctly ignored.

    git add .gitignore
    git commit -m "Add comprehensive .gitignore file"

What if I've already committed files I want to ignore?

If you've already added and committed a file (like config.py or __pycache__/), Git will continue to track it even if you add it to .gitignore. You need to tell Git to "untrack" it first.

  1. Stop tracking the file:

    # Replace 'path/to/your/file' with the actual path
    git rm --cached path/to/your/file
  2. If it's a directory:

    git rm -r --cached path/to/your/directory
  3. Commit the change:

    git commit -m "Stop tracking ignored files"

Now, Git will no longer track that file or directory, and any new versions will be ignored by your .gitignore rules.

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