Skip to content

Commit

Permalink
Function to check if a package is installed.
Browse files Browse the repository at this point in the history
  • Loading branch information
jkanche committed Oct 16, 2023
1 parent 0b1e506 commit 3568984
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/biocutils/package_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
__author__ = "jkanche"
__copyright__ = "jkanche"
__license__ = "MIT"


def is_package_installed(package_name: str) -> bool:
"""Check if the package is installed.
Args:
package_name (str): Package name.
Returns:
bool: True if package is installed, otherwise False.
"""
_installed = False
try:
exec(f"import {package_name}")
_installed = True
except Exception:
pass

return _installed
File renamed without changes.
22 changes: 22 additions & 0 deletions tests/test_package_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from biocutils.package_utils import is_package_installed

__author__ = "jkanche"
__copyright__ = "jkanche"
__license__ = "MIT"


def test_for_pandas():
pkg = is_package_installed("pandas")

assert pkg is False


def test_for_scipy():
pkg = is_package_installed("scipy")

assert pkg is False

def test_for_numpy():
pkg = is_package_installed("numpy")

assert pkg is True

0 comments on commit 3568984

Please sign in to comment.