Skip to main content

Locate the file location of your current running script.

Project description

Locate

This library exposes three functions/callables

  • this_dir() returns the directory of the current Python file, or when using from an interactive session, it returns the current working directory.
  • append_sys_path(relative_path) allows importing from a location relative to the running Python file by resolving relative_path relative to this_dir() and appending it to sys.path (e.g. relative_path="../foo/bar"); note the functionality is also available as a context manager to allow temporary effect (e.g. with append_sys_path("foo"): ...).
  • prepend_sys_path(relative_path) is the same as append_sys_path, but prepending sys.path in order to be the first import location for Python to search for modules/files.

Example use of this package

import locate

print(f"This file is located in: {locate.this_dir()}")

# Changing sys.path temporarily
with locate.prepend_sys_path("foo"):
    print(f"I can temporarily import from: {locate.this_dir().joinpath('foo')}")
print(f"I can no longer import from: {locate.this_dir().joinpath('foo')}")

# Changing sys.path permanently
locate.prepend_sys_path("foo")
print(f"I can now always import from: {locate.this_dir().joinpath('foo')}")

Motivation

This package is for people who frequently use the directory of their scripts for storing files and custom modules and do not want their pipeline to break from an interactive shell. This is based on how Julia thinks about the immediate directory through its @__DIR__ macro.

locate.this_dir() is defined as:

  • When running a .py file, this is the file's base directory.
  • When running an .ipyn notebook, this is the current working directory. This is the desired/expected result since Jupyter sets the working directory as the .ipynb base directory by default.
  • When running in a REPL, this is also the current working directory. This is similar to Julia's @__DIR__ macro.

Other considerations

For a good discussion on retrieving the current Python path, see https://stackoverflow.com/questions/3718657

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

locate-1.1.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

locate-1.1.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file locate-1.1.0.tar.gz.

File metadata

  • Download URL: locate-1.1.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.5

File hashes

Hashes for locate-1.1.0.tar.gz
Algorithm Hash digest
SHA256 a55ebfb3526c8d1dadd7e7f6eb998e7501a8f7731bd390465e450aac946eb784
MD5 e2c3f439a40911ad5191ec663e1d429b
BLAKE2b-256 b2a44037b42acf0b3e1d10597ec77d177a3f1c4eddcfd4d36aef16cd591c83fc

See more details on using hashes here.

Provenance

File details

Details for the file locate-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: locate-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.5

File hashes

Hashes for locate-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7a1db7affc2df0c615cf50bd3544cc819b781d3c1772e6e6c65173ac9db74a77
MD5 635f51051175239f1f8268d4245ee67c
BLAKE2b-256 ac301276a24b0544998468a8b5105b1eee69edd291c3da286bfb46ee3b6e825a

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page