/hotmodern-python

My repo for learning Python based on blog series 'Hypermodern Python' (by Claudio Jolowicz). Repeated with an effort on Windows host (with pyenv and poetry)

Primary LanguagePythonMIT LicenseMIT

PyPI Tests codecov.io Docs Nox

hotmodern-python

My Python learning project by article series 'Hypermodern Python' (by Claudio Jolowicz)

This repo 98% repeats code from these articles with little improvements for Windows environment (see below) and except several components (pre-commit, pytype, typeguard, Release Drafter)

Notes for Windows host

Updated: 2024-08-05

Functions with temp file on Windows

Windows has security limitation for temp files: OS does not allow processes other than the one used to create the NamedTemporaryFile to access the file (from here)

That's why I modified code like this:

# noxfile.py
import pathlib

def install_with_constraints(session: Session, *args: str, **kwargs: Any) -> None:
    """Install packages constrained by Poetry's lock file."""
    with tempfile.NamedTemporaryFile(delete=False) as requirements:
        session.run(
            "poetry",
            "export",
            ...
        )
        session.install("-r", f"{requirements.name}", *args, **kwargs)
    pathlib.Path(requirements.name).unlink()

Run Nox sessions with pyenv's Python versions

On Windows I use pyenv-win for managing Python interpreter versions.

If you set up pyenv-win correctly, it lets you run your session against multiple interpreters by specifying python to @nox.session (and run sessions the same way as on Linux machines).

@nox.session(python=["3.11", "3.12"], reuse_venv=True)
def tests(session: Session) -> None:
...

The main thing you should do is setting paths to Python interpreters in the right order in your PATH environment variable on Windows.
I wrote a detailed tutorial on their wiki page Configure the order in PATH variable
But to avoid many problems related to discover a Python interpreter version, you also have to add paths for all installed versions of Python "below" (after) path to ...\pyenv-win\shims For example your PATH might look as:

D:\python_tools\.pyenv\pyenv-win\bin
D:\python_tools\.pyenv\pyenv-win\shims\
D:\python_tools\.pyenv\pyenv-win\versions\3.8.10
D:\python_tools\.pyenv\pyenv-win\versions\3.9.13
D:\python_tools\.pyenv\pyenv-win\versions\3.10.8
D:\python_tools\.pyenv\pyenv-win\versions\3.11.2
D:\python_tools\.pyenv\pyenv-win\versions\3.12.4

Important

If you encountered with Nox error:

... failed with exit code 1:  
PEP-514 violation in Windows Registry at HKEY_CURRENT_USER/PythonCore/...

for modern Python 3.12+ you must reinstall this version with --register CLI option

pyenv uninstall 3.12.4
pyenv install 3.12.4 --register

Don't forget to restart your Terminal window (or IDE) to apply these changes.