https://docs.brew.sh/Python-for-Formula-Authors describes how.
Applications should be installed into a Python virtualenv environment rooted in libexec. This prevents the app’s Python modules from contaminating the system site-packages and vice versa.
It recommends using https://github.com/tdsmith/homebrew-pypi-poet