ChenNingCong's Blog

website·Indexed 30 days ago

https://chenningcong.github.io

A personal blog featuring technical notes on Python directory structures and path imports for deep learning projects, hosted via Hexo.

Tokens
437
Snippets
6
Records
6
Agent score
25%

What's inside ChenNingCong

  1. Organize Python project using Flat Layout for deep learning

    A Flat Layout simplifies project structure and is well-supported by package builders and IDEs like VS Code. The core requirement is that the main package directory must contain an __init__.py file to be importable after installation. If using a Flat Layout, you must configure the include parameter in pyproject.toml so the package builder knows which directory to use.
    deeplearning/
    ├── deeplearning/          # Model code
    │   ├── __init__.py        # Required for imports
    │   ├── net/
    │   │   ├── net1.py
    │   │   └── net2.py
    │   └── data/
    │       ├── dataloader.py
    │       └── dataset.py
    ├── script/                 # Training scripts
    ├── test/                   # Test code
    └── pyproject.toml          # Configuration for packaging
  2. Manage Python imports and PYTHONPATH for scripts

    To avoid import errors and ensure IDE support (such as VS Code's Pylance), always use absolute imports (e.g., import deeplearning.XX) instead of relative imports (e.g., from . import xxx).

    When running scripts located in a separate directory (like /script), the Python interpreter may not find the package. You must set the PYTHONPATH environment variable to the project root before execution.

    # Run a script by setting PYTHONPATH to the current directory
    PYTHONPATH="." python script/xxx.py