Skip to main content

Change Working Directory to the Project Root

Project description

cd2root: Change Working Directory to the Project Root

https://img.shields.io/pypi/v/cd2root.svg

cd2root allows you to elegantly change the working directory of the current file to the project root directory with only 1 line of code. It is useful when you have a project with a deep directory structure and you want to import modules relative to the project root directory.

Getting Started

Install cd2root

pip install cd2root

Usage

The easiest way to use cd2root is to add the following line to the top of your python file:

# normally import packages before custom modules
import ...

############################################################################
# cd2root: change working directory to project root
from cd2root import cd2root
cd2root()
############################################################################

# Then import custom modules that are relative to the project root
from src.utils import ...

If you just want to get the project root directory without changing the working directory

from cd2root import get_project_root

project_root = get_project_root()  # type: pathlib.Path
data_file = project_root / "data" / "data.csv"
...

Documentation

cd2root.cd2root is the grab-and-go function for most use cases. It has the following signature:

def cd2root(
    path_name: str = None,
    dotfile_name: str = None,
    load_dotenv: bool = True,
    use_cwd: bool = False,
    verbose: bool = False,
) -> Path:
    """
    Change working directory to project root directory.

    The rules to find the project root directory:
    1. If path_name is not None, find the directory containing path_name
    2. If dotfile_name is not None, find the directory containing dotfile_name
    3. If PROJECT_ROOT is set in .env, use it
    4. If any of the following files is found, use the directory containing it:
        .idea, .vscode, .editorconfig, .env, pyproject.toml, LICENSE

    :param path_name: the name of the path to find as the project root directory
    :param dotfile_name: the name of the dotfile to find as the project root directory
    :param load_dotenv: whether to load .env file
    :param use_cwd: whether to use current working directory as the starting point
    :param verbose: whether to output verbose messages
    :return: project root directory
    """
    ...

cd2root.get_project_root shares the same signature as cd2root.cd2root except that it does not change the working directory.

def get_project_root(
    path_name: str = None,
    dotfile_name: str = None,
    load_dotenv: bool = True,
    use_cwd: bool = False,
    verbose: bool = False,
) -> Path:
    """
    Get the project root directory. Raise FileNotFoundError if the project root
    directory is not found.

    ...
    """
    ...

There are also other helper functions available with cd2root. cd2root.cd2path change the working directory to given path.

def cd2path(path: Union[Path, str], verbose: bool = False) -> Path:
    """
    Change working directory to path.

    :param path: the path to change working directory to
    :param verbose: whether to output verbose messages
    :return: path
    """
    ...

def find_path(path_name: str, use_cwd: bool = False) -> Path:
    """
    Find the path of the given path_name.

    raise FileNotFoundError if the path is not found.

    :param path_name: the name of the path to find
    :param use_cwd: whether to use current working directory as the starting point
    :return: the path of the given path_name
    """
    ...

History

0.1.0 (2023-10-17)

  • First release on PyPI.

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

cd2root-0.1.6.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

cd2root-0.1.6-py2.py3-none-any.whl (5.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file cd2root-0.1.6.tar.gz.

File metadata

  • Download URL: cd2root-0.1.6.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for cd2root-0.1.6.tar.gz
Algorithm Hash digest
SHA256 76167b4f70a97d72b3bd6b7a676bd39ec8ffdb0c717f12bb261e460709f8e82f
MD5 38f8575b1a583319c496607568efcce0
BLAKE2b-256 22f14be79ef6b1f77a2b8d6087910cd83dc5739e6479fc8e5f1f17a269dcfba1

See more details on using hashes here.

File details

Details for the file cd2root-0.1.6-py2.py3-none-any.whl.

File metadata

  • Download URL: cd2root-0.1.6-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for cd2root-0.1.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6258d48862c453d780c0a39ba6103d87dc722ae7be5bb4cf034f63187604aeff
MD5 cfa7e6f9498fe31417f0d351ba7a4a5b
BLAKE2b-256 6d42757dacd6d61293d54ed1ed6d8e01cefe8db7cf023720bda59494dc0053ed

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