{"id":4619,"library":"locate","title":"Locate Script Directory","description":"The `locate` library provides simple utilities to find the file location of your current running Python script and manage paths. Currently at version 1.1.1, it receives regular updates, with recent releases adding features like context managers and addressing developer tooling concerns.","status":"active","version":"1.1.1","language":"en","source_language":"en","source_url":"https://github.com/AutoActuary/locate","tags":["file system","utility","script location","path management","context manager"],"install":[{"cmd":"pip install locate","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"While `locate.this_dir()` might work, it's generally best practice to directly import `this_dir` as the primary public interface. Versions prior to 1.1.2 could trigger `Pylance(reportPrivateImportUsage)` warnings if not directly imported due to `this_dir` not being explicitly listed in `__all__`.","wrong":"import locate; locate.this_dir()","symbol":"this_dir","correct":"from locate import this_dir"},{"note":"The `chdir` function is designed to be used as a context manager for temporarily changing the current working directory.","symbol":"chdir","correct":"from locate import chdir"}],"quickstart":{"code":"import os\nfrom locate import this_dir, chdir\n\n# Get the directory of the current script\ncurrent_script_dir = this_dir()\nprint(f\"Current script directory: {current_script_dir}\")\n\n# Using the chdir context manager\noriginal_cwd = os.getcwd()\nprint(f\"Original CWD: {original_cwd}\")\n\nwith chdir(current_script_dir):\n    print(f\"CWD inside context: {os.getcwd()}\")\n    # Perform operations relative to current_script_dir\n\nprint(f\"CWD after context: {os.getcwd()}\") # Should revert to original_cwd","lang":"python","description":"This quickstart demonstrates how to use `this_dir()` to get the path of the executing script and how to use the `chdir` context manager to temporarily change the current working directory. The `chdir` context manager ensures the directory is restored automatically upon exiting the `with` block."},"warnings":[{"fix":"Monitor future major release notes for changes. Pin dependencies to avoid unexpected breaking changes.","message":"The 1.1.1 release notes mention a 'deprecation contract from 4.0.0 to 2.0.0'. While ambiguous, this suggests a revised approach to deprecations, potentially leading to breaking changes or feature removals in future major versions (e.g., 2.0.0). Users should review release notes for versions >= 2.0.0 carefully.","severity":"breaking","affected_versions":"<=1.1.1 (for future compatibility)"},{"fix":"Upgrade to `locate>=1.1.2` or use `from locate import this_dir` for direct import.","message":"When using Pylance in VSCode with `locate` versions prior to 1.1.2, you might encounter a `reportPrivateImportUsage` warning if you import `this_dir` via `import locate` and then use `locate.this_dir`. This was due to `this_dir` not being explicitly listed in `__all__`.","severity":"gotcha","affected_versions":"<1.1.2"},{"fix":"Ensure a solid understanding of Python's import system, especially when dealing with scripts executed directly vs. as part of a package. Use `python -m my_package.my_module` for package-aware execution.","message":"The 1.0.0 release introduced `force_relative_location_imports`. Misunderstanding or misusing Python's relative import mechanisms, especially in scripts not run as part of a package or when manipulating `sys.path`, can lead to `ModuleNotFoundError` or unexpected import behavior. This library aims to simplify location, but core Python import rules still apply.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}