-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
38 lines (29 loc) · 1.05 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sys
from os import path
def is_executable() -> bool:
"""
Determine if the current script is packaged as an executable\n
(EG: If packed into a .exe with PyInstaller)\n
returns : True/False, if the script is an executable
"""
return getattr(sys, "frozen", False)
def script_dir() -> str:
"""
Get the path to the current script's directory, whether running as an executable or in an interpreter.\n
returns : A string containing the path to the script directory.
"""
return (
path.dirname(sys.executable)
if is_executable()
else path.join(path.dirname(path.realpath(sys.argv[0])))
)
def local_path(dir_name: str = "") -> str:
"""
Get the absolute path to a local file/directory __MEIPASS or .), whether running as an executable or in an interpreter.\n
returns : A string containing the path to the local file/directory
"""
return (
path.join(sys._MEIPASS, dir_name) # type: ignore
if is_executable()
else path.join(script_dir(), dir_name)
)