Welcome to pythonfinder’s documentation!¶
PythonFinder: Cross Platform Search Tool for Finding Pythons¶
Installation¶
Install from PyPI:
$ pipenv install pythonfinder
Install from Github:
$ pipenv install -e git+https://github.com/sarugaku/pythonfinder.git#egg=pythonfinder
Usage¶
Using PythonFinder is easy. Simply import it and ask for a python:
>>> from pythonfinder.pythonfinder import PythonFinder
>>> PythonFinder.from_line('python3')
'/home/techalchemy/.pyenv/versions/3.6.5/python3'
>>> from pythonfinder import Finder
>>> f = Finder()
>>> f.find_python_version(3, minor=6)
PathEntry(path=PosixPath('/home/hawk/.pyenv/versions/3.6.5/bin/python'), _children={}, is_root=False, only_python=False, py_version=PythonVersion(major=3, minor=6, patch=5, is_prerelease=False, is_postrelease=False, is_devrelease=False, version=<Version('3.6.5')>, architecture='64bit', comes_from=PathEntry(path=PosixPath('/home/hawk/.pyenv/versions/3.6.5/bin/python'), _children={}, is_root=True, only_python=False, py_version=None, pythons=None), executable=None), pythons=None)
>>> f.find_python_version(2)
PathEntry(path=PosixPath('/home/hawk/.pyenv/shims/python2'), ...py_version=PythonVersion(major=2, minor=7, patch=15, is_prerelease=False, is_postrelease=False, is_devrelease=False, version=<Version('2.7.15')>, architecture='64bit', comes_from=PathEntry(path=PosixPath('/home/hawk/.pyenv/shims/python2'), _children={}, is_root=True, only_python=False, py_version=None, pythons=None), executable=None), pythons=None)
>>> f.find_python_version("anaconda3-5.3.0")
Find a named distribution, such as anaconda3-5.3.0
:
PathEntry(path=PosixPath('/home/hawk/.pyenv/versions/anaconda3-5.3.0/bin/python3.7m'), _children={'/home/hawk/.pyenv/versions/anaconda3-5.3.0/bin/python3.7m': ...}, only_python=False, name='anaconda3-5.3.0', _py_version=PythonVersion(major=3, minor=7, patch=0, is_prerelease=False, is_postrelease=False, is_devrelease=False,...))
PythonFinder can even find beta releases:
>>> f.find_python_version(3, minor=7)
PathEntry(path=PosixPath('/home/hawk/.pyenv/versions/3.7.0b1/bin/python'), _children={}, is_root=False, only_python=False, py_version=PythonVersion(major=3, minor=7, patch=0, is_prerelease=True, is_postrelease=False, is_devrelease=False, version=<Version('3.7.0b1')>, architecture='64bit', comes_from=PathEntry(path=PosixPath('/home/hawk/.pyenv/versions/3.7.0b1/bin/python'), _children={}, is_root=True, only_python=False, py_version=None, pythons=None), executable=None), pythons=None)
>>> f.which('python')
PathEntry(path=PosixPath('/home/hawk/.pyenv/versions/3.6.5/bin/python'), _children={}, is_root=False, only_python=False, py_version=PythonVersion(major=3, minor=6, patch=5, is_prerelease=False, is_postrelease=False, is_devrelease=False, version=<Version('3.6.5')>, architecture='64bit', comes_from=PathEntry(path=PosixPath('/home/hawk/.pyenv/versions/3.6.5/bin/python'), _children={}, is_root=True, only_python=False, py_version=None, pythons=None), executable=None), pythons=None)
Windows Support¶
PythonFinder natively supports windows via both the PATH environment variable and PEP-514 compliant finder which comes by default with python 3. Usage on windows becomes:
>>> from pythonfinder import Finder
>>> f = Finder()
>>> f.find_python_version(3, minor=6)
PythonVersion(major=3, minor=6, patch=4, is_prerelease=False, is_postrelease=False, is_devrelease=False, version=<Version('3.6.4')>, architecture='64bit', comes_from=PathEntry(path=WindowsPath('C:/Program Files/Python36/python.exe'), _children={}, is_root=False, only_python=True, py_version=None, pythons=None), executable=WindowsPath('C:/Program Files/Python36/python.exe'))
>>> f.find_python_version(3, minor=7, pre=True)
PythonVersion(major=3, minor=7, patch=0, is_prerelease=True, is_postrelease=False, is_devrelease=False, version=<Version('3.7.0b5')>, architecture='64bit', comes_from=PathEntry(path=WindowsPath('C:/Program Files/Python37/python.exe'), _children={}, is_root=False, only_python=True, py_version=None, pythons=None), executable=WindowsPath('C:/Program Files/Python37/python.exe'))
>>> f.which('python')
PathEntry(path=WindowsPath('C:/Python27/python.exe'), _children={}, is_root=False, only_python=False, py_version=None, pythons=None)
Finding Executables¶
PythonFinder also provides which functionality across platforms, and it uses lazy loading and fast-returns to be performant at this task.
>>> f.which('cmd')
PathEntry(path=WindowsPath('C:/windows/system32/cmd.exe'), _children={}, is_root=False, only_python=False, py_version=None, pythons=None)
>>> f.which('code')
PathEntry(path=WindowsPath('C:/Program Files/Microsoft VS Code/bin/code'), _children={}, is_root=False, only_python=False, py_version=None, pythons=None)
>>> f.which('vim')
PathEntry(path=PosixPath('/usr/bin/vim'), _children={}, is_root=False, only_python=False, py_version=None, pythons=None)
>>> f.which('inv')
PathEntry(path=PosixPath('/home/hawk/.pyenv/versions/3.6.5/bin/inv'), _children={}, is_root=False, only_python=False, py_version=None, pythons=None)
Architecture support¶
PythonFinder supports architecture specific lookups on all platforms:
>>> f.find_python_version(3, minor=6, arch="64")
PathEntry(path=PosixPath('/usr/bin/python3'), _children={'/usr/bin/python3': ...}, only_python=False, name='python3', _py_version=PythonVersion(major=3, minor=6, patch=7, is_prerelease=False, is_postrelease=False, is_devrelease=False, is_debug=False, version=<Version('3.6.7')>, architecture='64bit', comes_from=..., executable='/usr/bin/python3', name='python3'), _pythons=defaultdict(None, {}), is_root=False)
Integrations¶
pythonfinder package¶
- class pythonfinder.Finder(**data)[source]¶
Bases:
FinderBaseModel
- find_all_python_versions(major=None, minor=None, patch=None, pre=None, dev=None, arch=None, name=None)[source]¶
- find_python_version(major=None, minor=None, patch=None, pre=None, dev=None, arch=None, name=None, sort_by_path=False)[source]¶
Find the python version which corresponds most closely to the version requested.
- Parameters
major (str | int | None) – The major version to look for, or the full version, or the name of the target version.
minor (int | None) – The minor version. If provided, disables string-based lookups from the major version field.
patch (int | None) – The patch version.
pre (bool | None) – If provided, specifies whether to search pre-releases.
dev (bool | None) – If provided, whether to search dev-releases.
arch (str | None) – If provided, which architecture to search.
name (str | None) – Name of the target python, e.g.
anaconda3-5.3.0
sort_by_path (bool) – Whether to sort by path – default sort is by version(default: False)
- Return type
PathEntry | None
- Returns
A new PathEntry pointer at a matching python version, if one can be located.
- global_search: bool¶
- ignore_unsupported: bool¶
- path_prepend: Optional[str]¶
- sort_by_path: bool¶
- system: bool¶
- system_path: Optional[SystemPath]¶
- exception pythonfinder.InvalidPythonVersion[source]¶
Bases:
Exception
Raised when parsing an invalid python version
- class pythonfinder.SystemPath(*, global_search: bool = True, paths: Dict[str, Union[PythonFinder, PathEntry]] = None, executables_tracking: List[PathEntry] = None, python_executables_tracking: Dict[str, PathEntry] = None, path_order: List[str] = None, python_version_dict: Dict[Tuple, Any] = None, version_dict_tracking: Dict[Tuple, List[PathEntry]] = None, only_python: bool = False, pyenv_finder: Optional[PythonFinder] = None, asdf_finder: Optional[PythonFinder] = None, system: bool = False, ignore_unsupported: bool = False, finders_dict: Dict[str, PythonFinder] = None)[source]¶
Bases:
FinderBaseModel
- class Config[source]¶
Bases:
object
- allow_mutation = True¶
- arbitrary_types_allowed = True¶
- include_private_attributes = True¶
- keep_untouched = (<class 'cached_property.cached_property'>,)¶
- validate_assignment = True¶
- asdf_finder: Optional[PythonFinder]¶
- classmethod create(path=None, system=False, only_python=False, global_search=True, ignore_unsupported=True)[source]¶
Create a new
pythonfinder.models.SystemPath
instance.- Parameters
path (str | None) – Search path to prepend when searching, defaults to None
path – str, optional
system (bool) – Whether to use the running python by default instead of searching, defaults to False
only_python (bool) – Whether to search only for python executables, defaults to False
ignore_unsupported (bool) – Whether to ignore unsupported python versions, if False, an error is raised, defaults to True
- Return type
- Returns
A new
pythonfinder.models.SystemPath
instance.
- executables_tracking: List[PathEntry]¶
- find_all(executable)[source]¶
Search the path for an executable. Return all copies.
- Parameters
executable (str) – Name of the executable
- Return type
- Returns
List[PathEntry]
- find_all_python_versions(major=None, minor=None, patch=None, pre=None, dev=None, arch=None, name=None)[source]¶
- find_python_version(major=None, minor=None, patch=None, pre=None, dev=None, arch=None, name=None, sort_by_path=False)[source]¶
- Return type
- finders_dict: Dict[str, PythonFinder]¶
- global_search: bool¶
- ignore_unsupported: bool¶
- only_python: bool¶
- path_entries¶
- path_order: List[str]¶
- paths: Dict[str, Union[PythonFinder, PathEntry]]¶
- pyenv_finder: Optional[PythonFinder]¶
- python_executables¶
- python_executables_tracking: Dict[str, PathEntry]¶
- python_version_dict: Dict[Tuple, Any]¶
- system: bool¶
- version_dict¶
- version_dict_tracking: Dict[Tuple, List[PathEntry]]¶
Subpackages¶
pythonfinder.models package¶
Submodules¶
pythonfinder.models.mixins module¶
- class pythonfinder.models.mixins.PathEntry(**data)[source]¶
Bases:
BaseModel
- class Config[source]¶
Bases:
object
- allow_mutation = True¶
- arbitrary_types_allowed = True¶
- include_private_attributes = True¶
- validate_assignment = True¶
- property as_python: PythonVersion¶
- children_ref: Optional[Any]¶
- classmethod create(path, is_root=False, only_python=False, pythons=None, name=None)[source]¶
Helper method for creating new
pythonfinder.models.PathEntry
instances.- Parameters
path (str) – Path to the specified location.
is_root (bool) – Whether this is a root from the environment PATH variable, defaults to False
only_python (bool) – Whether to search only for python executables, defaults to False
pythons (dict) – A dictionary of existing python objects (usually from a finder), defaults to None
name (str) – Name of the python version, e.g.
anaconda3-5.3.0
- Return type
- Returns
A new instance of the class.
- find_all_python_versions(major=None, minor=None, patch=None, pre=None, dev=None, arch=None, name=None)[source]¶
Search for a specific python version on the path. Return all copies
- Parameters
major (int) – Major python version to search for.
minor (int) – Minor python version to search for, defaults to None
patch (int) – Patch python version to search for, defaults to None
pre (bool) – Search for prereleases (default None) - prioritize releases if None
dev (bool) – Search for devreleases (default None) - prioritize releases if None
arch (str) – Architecture to include, e.g. ‘64bit’, defaults to None
name (str) – The name of a python version, e.g.
anaconda3-5.3.0
- Return type
- Returns
A list of
PathEntry
instances matching the version requested.
- find_python_version(major=None, minor=None, patch=None, pre=None, dev=None, arch=None, name=None)[source]¶
Search or self for the specified Python version and return the first match.
- Parameters
major (int) – Major version number.
minor (int) – Minor python version to search for, defaults to None
patch (int) – Patch python version to search for, defaults to None
pre (bool) – Search for prereleases (default None) - prioritize releases if None
dev (bool) – Search for devreleases (default None) - prioritize releases if None
arch (str) – Architecture to include, e.g. ‘64bit’, defaults to None
name (str) – The name of a python version, e.g.
anaconda3-5.3.0
- Return type
PathEntry | None
- Returns
A
PathEntry
instance matching the version requested.
- is_dir_ref: Optional[bool]¶
- is_executable_ref: Optional[bool]¶
- is_python_ref: Optional[bool]¶
- is_root: bool¶
- name: Optional[str]¶
- only_python: Optional[bool]¶
- path: Optional[Path]¶
- property py_version: PythonVersion | None¶
- py_version_ref: Optional[Any]¶
- pythons_ref: Optional[Dict[Any, Any]]¶
pythonfinder.models.path module¶
- class pythonfinder.models.path.SystemPath(*, global_search: bool = True, paths: Dict[str, Union[PythonFinder, PathEntry]] = None, executables_tracking: List[PathEntry] = None, python_executables_tracking: Dict[str, PathEntry] = None, path_order: List[str] = None, python_version_dict: Dict[Tuple, Any] = None, version_dict_tracking: Dict[Tuple, List[PathEntry]] = None, only_python: bool = False, pyenv_finder: Optional[PythonFinder] = None, asdf_finder: Optional[PythonFinder] = None, system: bool = False, ignore_unsupported: bool = False, finders_dict: Dict[str, PythonFinder] = None)[source]¶
Bases:
FinderBaseModel
- class Config[source]¶
Bases:
object
- allow_mutation = True¶
- arbitrary_types_allowed = True¶
- include_private_attributes = True¶
- keep_untouched = (<class 'cached_property.cached_property'>,)¶
- validate_assignment = True¶
- asdf_finder: Optional[PythonFinder]¶
- classmethod create(path=None, system=False, only_python=False, global_search=True, ignore_unsupported=True)[source]¶
Create a new
pythonfinder.models.SystemPath
instance.- Parameters
path (str | None) – Search path to prepend when searching, defaults to None
path – str, optional
system (bool) – Whether to use the running python by default instead of searching, defaults to False
only_python (bool) – Whether to search only for python executables, defaults to False
ignore_unsupported (bool) – Whether to ignore unsupported python versions, if False, an error is raised, defaults to True
- Return type
- Returns
A new
pythonfinder.models.SystemPath
instance.
- property executables¶
- executables_tracking: List[PathEntry]¶
- find_all(executable)[source]¶
Search the path for an executable. Return all copies.
- Parameters
executable (str) – Name of the executable
- Return type
- Returns
List[PathEntry]
- find_all_python_versions(major=None, minor=None, patch=None, pre=None, dev=None, arch=None, name=None)[source]¶
- find_python_version(major=None, minor=None, patch=None, pre=None, dev=None, arch=None, name=None, sort_by_path=False)[source]¶
- Return type
- property finders¶
- finders_dict: Dict[str, PythonFinder]¶
- global_search: bool¶
- ignore_unsupported: bool¶
- only_python: bool¶
- path_entries¶
- path_order: List[str]¶
- paths: Dict[str, Union[PythonFinder, PathEntry]]¶
- pyenv_finder: Optional[PythonFinder]¶
- python_executables¶
- python_executables_tracking: Dict[str, PathEntry]¶
- python_version_dict: Dict[Tuple, Any]¶
- system: bool¶
- version_dict¶
- version_dict_tracking: Dict[Tuple, List[PathEntry]]¶
pythonfinder.models.python module¶
- class pythonfinder.models.python.PythonFinder(**data)[source]¶
Bases:
PathEntry
- class Config[source]¶
Bases:
object
- allow_mutation = True¶
- arbitrary_types_allowed = True¶
- include_private_attributes = True¶
- validate_assignment = True¶
- classmethod create(root, sort_function, version_glob_path=None, ignore_unsupported=True)[source]¶
Helper method for creating new
pythonfinder.models.PathEntry
instances.- Parameters
path (str) – Path to the specified location.
is_root (bool) – Whether this is a root from the environment PATH variable, defaults to False
only_python (bool) – Whether to search only for python executables, defaults to False
pythons (dict) – A dictionary of existing python objects (usually from a finder), defaults to None
name (str) – Name of the python version, e.g.
anaconda3-5.3.0
- Return type
- Returns
A new instance of the class.
- find_all_python_versions(major=None, minor=None, patch=None, pre=None, dev=None, arch=None, name=None)[source]¶
Search for a specific python version on the path. Return all copies
- Parameters
major (int) – Major python version to search for.
minor (int) – Minor python version to search for, defaults to None
patch (int) – Patch python version to search for, defaults to None
pre (bool) – Search for prereleases (default None) - prioritize releases if None
dev (bool) – Search for devreleases (default None) - prioritize releases if None
arch (str) – Architecture to include, e.g. ‘64bit’, defaults to None
name (str) – The name of a python version, e.g.
anaconda3-5.3.0
- Return type
- Returns
A list of
PathEntry
instances matching the version requested.
- find_python_version(major=None, minor=None, patch=None, pre=None, dev=None, arch=None, name=None)[source]¶
Search or self for the specified Python version and return the first match.
- Parameters
major (int) – Major version number.
minor (int) – Minor python version to search for, defaults to None
patch (int) – Patch python version to search for, defaults to None
pre (bool) – Search for prereleases (default None) - prioritize releases if None
dev (bool) – Search for devreleases (default None) - prioritize releases if None
arch (str) – Architecture to include, e.g. ‘64bit’, defaults to None
name (str) – The name of a python version, e.g.
anaconda3-5.3.0
- Return type
PathEntry | None
- Returns
A
PathEntry
instance matching the version requested.
- ignore_unsupported: bool¶
Whether to ignore any paths which raise exceptions and are not actually python
- paths: List¶
List of paths discovered during search
- pythons_ref: Dict¶
- root: Path¶
- roots: Dict¶
The root locations used for discovery
- sort_function: Optional[Callable]¶
The function to use to sort version order when returning an ordered version set
- version_glob_path: str¶
Glob path for python versions off of the root directory
- property versions: DefaultDict[tuple, PathEntry]¶
- class pythonfinder.models.python.PythonVersion(**data)[source]¶
Bases:
FinderBaseModel
- class Config[source]¶
Bases:
object
- allow_mutation = True¶
- arbitrary_types_allowed = True¶
- include_private_attributes = True¶
- validate_assignment = True¶
- architecture: Optional[str]¶
- comes_from: Optional['PathEntry']¶
- company: Optional[str]¶
- executable: Optional[Union[str, WindowsPath, Path]]¶
- classmethod from_path(path, name=None, ignore_unsupported=True, company=None)[source]¶
Parses a python version from a system path.
- Raises:
ValueError – Not a valid python path
- Parameters
- Return type
- Returns
An instance of a PythonVersion.
- classmethod from_windows_launcher(launcher_entry, name=None, company=None)[source]¶
Create a new PythonVersion instance from a Windows Launcher Entry
- Parameters
- Return type
- Returns
An instance of a PythonVersion.
- is_debug: bool¶
- is_devrelease: bool¶
- is_postrelease: bool¶
- is_prerelease: bool¶
- major: int¶
- matches(major=None, minor=None, patch=None, pre=False, dev=False, arch=None, debug=False, python_name=None)[source]¶
- Return type
- minor: Optional[int]¶
- name: Optional[str]¶
- classmethod parse(version)[source]¶
Parse a valid version string into a dictionary
- Raises:
ValueError – Unable to parse version string ValueError – Not a valid python version TypeError – NoneType or unparsable type passed in
- patch: Optional[int]¶
- update_metadata(metadata)[source]¶
Update the metadata on the current
pythonfinder.models.python.PythonVersion
Given a parsed version dictionary from
pythonfinder.utils.parse_python_version()
, update the instance variables of the current version instance to reflect the newly supplied values.- Return type
- version: Optional[Version]¶
- property version_sort: tuple[int, int, int | None, int, int]¶
A tuple for sorting against other instances of the same class.
Returns a tuple of the python version but includes points for core python, non-dev, and non-prerelease versions. So released versions will have 2 points for this value. E.g.
(1, 3, 6, 6, 2)
is a release,(1, 3, 6, 6, 1)
is a prerelease,(1, 3, 6, 6, 0)
is a dev release, and(1, 3, 6, 6, 3)
is a postrelease.(0, 3, 7, 3, 2)
represents a non-core python release, e.g. by a repackager of python like Continuum.
- class pythonfinder.models.python.VersionMap(**data)[source]¶
Bases:
FinderBaseModel
- class Config[source]¶
Bases:
object
- allow_mutation = True¶
- arbitrary_types_allowed = True¶
- include_private_attributes = True¶
- validate_assignment = True¶
- versions: DefaultDict[Tuple[int, Optional[int], Optional[int], bool, bool, bool], List[PathEntry]]¶
pythonfinder.models.windows module¶
Submodules¶
pythonfinder.cli module¶
pythonfinder.environment module¶
- pythonfinder.environment.SUBPROCESS_TIMEOUT = 5¶
The default subprocess timeout for determining python versions
Set to 5 by default.
pythonfinder.exceptions module¶
pythonfinder.pythonfinder module¶
- class pythonfinder.pythonfinder.Finder(**data)[source]¶
Bases:
FinderBaseModel
- find_all_python_versions(major=None, minor=None, patch=None, pre=None, dev=None, arch=None, name=None)[source]¶
- find_python_version(major=None, minor=None, patch=None, pre=None, dev=None, arch=None, name=None, sort_by_path=False)[source]¶
Find the python version which corresponds most closely to the version requested.
- Parameters
major (str | int | None) – The major version to look for, or the full version, or the name of the target version.
minor (int | None) – The minor version. If provided, disables string-based lookups from the major version field.
patch (int | None) – The patch version.
pre (bool | None) – If provided, specifies whether to search pre-releases.
dev (bool | None) – If provided, whether to search dev-releases.
arch (str | None) – If provided, which architecture to search.
name (str | None) – Name of the target python, e.g.
anaconda3-5.3.0
sort_by_path (bool) – Whether to sort by path – default sort is by version(default: False)
- Return type
PathEntry | None
- Returns
A new PathEntry pointer at a matching python version, if one can be located.
- global_search: bool¶
- ignore_unsupported: bool¶
- path_prepend: Optional[str]¶
- sort_by_path: bool¶
- system: bool¶
- system_path: Optional[SystemPath]¶
pythonfinder.utils module¶
- pythonfinder.utils.dedup(iterable)[source]¶
Deduplicate an iterable object like iter(set(iterable)) but order-reserved.
- Return type
- pythonfinder.utils.ensure_path(path)[source]¶
Given a path (either a string or a Path object), expand variables and return a Path object.
- pythonfinder.utils.expand_paths(path, only_python=True)[source]¶
Recursively expand a list or
PathEntry
instance
- pythonfinder.utils.filter_pythons(path)[source]¶
Return all valid pythons in a given path
- Return type
Iterable | Path
- pythonfinder.utils.get_python_version(path)[source]¶
Get python version string using subprocess from a given path.
- Return type
- pythonfinder.utils.guess_company(path)[source]¶
Given a path to python, guess the company who created it
- pythonfinder.utils.looks_like_python(name)[source]¶
Determine whether the supplied filename looks like a possible name of python.
- pythonfinder.utils.path_is_executable(path)[source]¶
Determine whether the supplied path is executable.
- Return type
- Returns
Whether the provided path is executable.
- pythonfinder.utils.path_is_known_executable(path)[source]¶
Returns whether a given path is a known executable from known executable extensions or has the executable bit toggled.
- pythonfinder.utils.path_is_python(path)[source]¶
Determine whether the supplied path is executable and looks like a possible path to python.
- Parameters
path (
Path
) – The path to an executable.- Return type
- Returns
Whether the provided path is an executable path to python.
- pythonfinder.utils.path_is_pythoncore(path)[source]¶
Given a path, determine whether it appears to be pythoncore.
Does not verify whether the path is in fact a path to python, but simply does an exclusionary check on the possible known python implementations to see if their names are present in the path (fairly dumb check).