Skip to content

[setup.py] get_version: replace importlib with raw text scanning #213

@zuoxingdong

Description

@zuoxingdong

e.g.

# only read through the raw file without importing any modules
def _get_version(dirpath):
    var_name = '__version__'
    file_path = Path(dirpath)/'__init__.py'
    with open(file_path, 'r') as fp:
        for line in fp:
            if line.startswith(var_name):
                g = {}
                exec(line, g)
                return g[var_name]
        raise ValueError(f'`{var_name}` must be defined within `{file_path.as_posix()}`')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions