Skip to content

Commit

Permalink
Use local version of package if exists.
Browse files Browse the repository at this point in the history
  • Loading branch information
rohinb2 committed Jul 4, 2024
1 parent 815b5ce commit 549bf29
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions runhouse/resources/packages/package.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
import importlib.metadata as metadata
import re
import sys
from pathlib import Path
Expand Down Expand Up @@ -80,6 +81,13 @@ def __str__(self):
return f"Package: {self.install_target.path}"
return f"Package: {self.install_target}"

@staticmethod
def _find_locally_installed_version(package_name: str):
try:
return metadata.version(package_name)
except metadata.PackageNotFoundError:
return None

@staticmethod
def _prepend_python_executable(
install_cmd: str, env: Union[str, "Env"] = None, cluster: "Cluster" = None
Expand Down Expand Up @@ -477,6 +485,14 @@ def from_string(specifier: str, dryrun=False):
else:
install_method = "pip"

# If we are just defaulting to pip, attempt to install the same version of the package
# that is already installed locally
# Check if the target is only letters, nothing else. This means its a string like 'numpy'.
if install_method == "pip" and target.isalpha():
locally_installed_version = Package._find_locally_installed_version(target)
if locally_installed_version:
target = f"{target}=={locally_installed_version}"

# "Local" install method is a special case where we just copy a local folder and add to path
if install_method == "local":
return Package(
Expand Down

0 comments on commit 549bf29

Please sign in to comment.