cloudflare / cloudflare/workers-py

Support package projects with src-layout

Open
#58 8 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
103
Forks
22
Avg merge
2d 1h
Merged PRs (30d)
30

Description

I have an ASGI app project with the following [src-layout](https://packaging.python.org/en/latest/discussions/src-layout-vs-flat-layout/) file structure:

```
project_name
├── config.toml
├── pyproject.toml
├── wrangler.toml
└── src
├── package_name
│ ├── __init__.py
│ ├── server.py
│ ├── ...
│ ├── subpackage_name
│ │ └── __init__.py
│ ├── data
│ │ └── data.json
│ ├── static
│ │ └── styles.css
│ └── templates
│ └── index.html
├── js-stubs
│ └── __init__.pyi
└── worker.py
```

During runtime, it needs to read files in subdirectories of the package (e.g. `data`, `templates`), and files in the current working directory (e.g. `config.toml`).

If I put `worker.py` inside the package and import the ASGI app, it fails because `package_name` is not installed as a package, so Python doesn't know how to import neighbouring files.

If I put `worker.py` at the top level, I need to import from `src`, which defeats the point of the src-layout.

If I put `worker.py` under `src`, the imports work fine, but none of the top-level files are accessible.

Is there a way to make pywrangler build and install the project along with its dependencies (e.g. when a [build system](https://docs.astral.sh/uv/concepts/projects/config/#build-systems) is defined)?
What would be the best place to put `worker.py` in this case?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.