Skip to main content

Locate the file location of your current running script.

Project description

Locate

This library provides three 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"). This also works 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 rather than the last.

Safety

The *_sys_path destructors (when exiting the with block) are safe from any side effect your package imports may have on sys.path. It's not a naive implementation such as removing the first/last element or removing the element by value; it employs a string subclass with an additional id property to keep tags of sys.path insertions. It is, therefore, completely safe and allows for any further nesting of with *_sys_path within the import tree.

Examples

Input

import locate
from pathlib import Path

this_dir = locate.this_dir()

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

# Create files to demonstrate importing from a directory
foo = Path(locate.this_dir(), "foo")
foo.mkdir(exist_ok=True)

Path(foo, "bar1.py").write_text("print('Importing bar1')")
Path(foo, "bar2.py").write_text("print('Importing bar2')")
Path(foo, "bar3.py").write_text("print('Importing bar3')")
Path(foo, "bar4.py").write_text("print('Importing bar4')")

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

    print()

print(f"I can no longer import from: {Path(this_dir, 'foo')}")
try:
    import bar2
except ImportError:
    print("Cannot import bar2")
print()

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

print()

Output

This file is located in: C:\Users\simon

I can temporarily import from: C:\Users\simon\foo
Importing bar1

I can no longer import from: C:\Users\simon\foo
Cannot import bar2

I can now always import from: C:\Users\simon\foo
Importing bar3
Importing bar4

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.1.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

locate-1.1.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: locate-1.1.1.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for locate-1.1.1.tar.gz
Algorithm Hash digest
SHA256 432750f5b7e89f8c99942ca7d8722ccd1e7954b20e6a973027fccb6cc00af857
MD5 88c54e14e9fe62c1215c6bea1b502419
BLAKE2b-256 26b06b303d4a2a20046dc396de914a6c1840253ff874630f00864ffe623acb68

See more details on using hashes here.

File details

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

File metadata

  • Download URL: locate-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for locate-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9e5e2f3516639240f4d975c08e95ae6a24ff4dd63d228f927541cdec30105755
MD5 42e86bb4e381b75e81b827492e27676f
BLAKE2b-256 ab0d9b6f11382b2f9080b5a366d20e90b4c08e547b6cd08c2a206729e6bad47a

See more details on using hashes here.

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