Return the absolute path to the executable The executable is either Python or, if frozen, then bleachbit.exe. When running under `env -i`, sys.executable is an empty string.
()
| 102 | |
| 103 | |
| 104 | def get_executable(): |
| 105 | """Return the absolute path to the executable |
| 106 | |
| 107 | The executable is either Python or, if frozen, then |
| 108 | bleachbit.exe. |
| 109 | |
| 110 | When running under `env -i`, sys.executable is an empty string. |
| 111 | """ |
| 112 | if sys.executable: |
| 113 | # example: /usr/bin/python3 |
| 114 | return sys.executable |
| 115 | # When running as unittest, sys.argv may look like this: |
| 116 | # [' -m unittest', '-v', 'tests.TestGeneral'] |
| 117 | try: |
| 118 | # example: /usr/bin/python3.12 |
| 119 | # Notice it ends with .12. |
| 120 | return os.readlink('/proc/self/exe') |
| 121 | except Exception: |
| 122 | pass |
| 123 | for py in ['python3', 'python']: |
| 124 | py_which = shutil.which(py) |
| 125 | if py_which: |
| 126 | return py_which |
| 127 | raise RuntimeError('Cannot find Python executable') |
| 128 | |
| 129 | |
| 130 | def get_real_username(): |
no outgoing calls