From c06ddeb62a89c955048f7f25169dca616ac0adbe Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Sun, 30 May 2021 20:52:42 +0200 Subject: [PATCH] Create `is_pip()` function (#3391) Returns `True` if file is part of pip package. Useful for contextual behavior modification. ```python def is_pip(): # Is file in a pip package? return 'site-packages' in Path(__file__).absolute().parts ``` --- utils/general.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/utils/general.py b/utils/general.py index 006e64859f32..1f805c56af29 100755 --- a/utils/general.py +++ b/utils/general.py @@ -53,12 +53,12 @@ def get_latest_run(search_dir='.'): def is_docker(): - # Is environment a Docker container + # Is environment a Docker container? return Path('/workspace').exists() # or Path('/.dockerenv').exists() def is_colab(): - # Is environment a Google Colab instance + # Is environment a Google Colab instance? try: import google.colab return True @@ -66,6 +66,11 @@ def is_colab(): return False +def is_pip(): + # Is file in a pip package? + return 'site-packages' in Path(__file__).absolute().parts + + def emojis(str=''): # Return platform-dependent emoji-safe version of string return str.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else str