Search in increasingly higher folders for the given file Returns path to the file if found, or an empty string otherwise
(
filename: str = '.env',
raise_error_if_not_found: bool = False,
usecwd: bool = False,
)
| 268 | |
| 269 | |
| 270 | def find_dotenv( |
| 271 | filename: str = '.env', |
| 272 | raise_error_if_not_found: bool = False, |
| 273 | usecwd: bool = False, |
| 274 | ) -> str: |
| 275 | """ |
| 276 | Search in increasingly higher folders for the given file |
| 277 | |
| 278 | Returns path to the file if found, or an empty string otherwise |
| 279 | """ |
| 280 | |
| 281 | def _is_interactive(): |
| 282 | """ Decide whether this is running in a REPL or IPython notebook """ |
| 283 | main = __import__('__main__', None, None, fromlist=['__file__']) |
| 284 | return not hasattr(main, '__file__') |
| 285 | |
| 286 | if usecwd or _is_interactive() or getattr(sys, 'frozen', False): |
| 287 | # Should work without __file__, e.g. in REPL or IPython notebook. |
| 288 | path = os.getcwd() |
| 289 | else: |
| 290 | # will work for .py files |
| 291 | frame = sys._getframe() |
| 292 | current_file = __file__ |
| 293 | |
| 294 | while frame.f_code.co_filename == current_file: |
| 295 | assert frame.f_back is not None |
| 296 | frame = frame.f_back |
| 297 | frame_filename = frame.f_code.co_filename |
| 298 | path = os.path.dirname(os.path.abspath(frame_filename)) |
| 299 | |
| 300 | for dirname in _walk_to_root(path): |
| 301 | check_path = os.path.join(dirname, filename) |
| 302 | if os.path.isfile(check_path): |
| 303 | return check_path |
| 304 | |
| 305 | if raise_error_if_not_found: |
| 306 | raise IOError('File not found') |
| 307 | |
| 308 | return '' |
| 309 | |
| 310 | |
| 311 | def load_dotenv( |
no test coverage detected