Python package that allows a user to easily import local functions from parent folders.
Project description
import-parent
https://import-parent.readthedocs.io/
import_parent is a tiny helper for Python projects that need to import code
from a parent directory.
It adds a parent folder to sys.path relative to the caller, which is useful
for local tooling, test helpers, and small scripts that are not installed as
packages.
It works when used in .py files or .ipynb files, which typically have different default behaviour about local imports otherwise.
Why this package exists
If your project layout is:
project/
├── helper/
├── tests/
└── scripts/
and a file in scripts/ (or any of its subfolders) needs to import a function from helpers/ (or any of its subfolders), this package makes that import possible.
The relative import may be possible on some computers by running from ..helper.file import function, but that .. notation is not possible in cloud-based IDEs like Posit Workbench, Posit Cloud, JupyterHub, GitHub Codespaces, etc., especailly when they are accessed from an enterprise account.
Using add_parent_to_path avoids the need for the following janky modification of sys.path otherwise needed to add a parent directory:
import os
import sys
up_one_level = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if up_one_level not in sys.path:
sys.path.append(up_one_level)
Heaven forbid you need to go up two levels, in which case that janky code gets even jankier.
Installation
pip install import-parent
Quick start
from import_parent import add_parent_to_path
add_parent_to_path("..")
This resolves the parent directory of the current caller and prepends it to
sys.path, so sibling modules can be imported immediately.
When to use it
Use this package when:
- you are working on local scripts or tests that should import a sibling package;
- you want a small, explicit alternative to repeated
sys.pathmanipulation; - you prefer not to install the project in editable mode for every experiment.
Documentation
- Read the Docs: https://import-parent.readthedocs.io/
- Tutorials: see the getting-started guide in the docs.
- How-to recipes: use the task-focused examples in the docs.
- Explanation: learn why this helper exists and when it is appropriate.
- Reference: API details for
add_parent_to_path.
Notes
- The package name on PyPI is
import-parent(with a hyphen). - The Python import name is
import_parent(with an underscore).
Questions and feedback: https://github.com/michaelboerman/import_parent/issues
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file import_parent-1.1.0.tar.gz.
File metadata
- Download URL: import_parent-1.1.0.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0a7734486b53f0c4acb2cd736d7eef8e1a2a71ceb9285b77aee68392f24c237
|
|
| MD5 |
3d353444d7b04f985c52f7e64dc3e938
|
|
| BLAKE2b-256 |
b28c000ef15321dee30eab420a6428ebbdcfc0859e4363f9b4e14e3cc02dfefd
|
Provenance
The following attestation bundles were made for import_parent-1.1.0.tar.gz:
Publisher:
pypi-publish.yml on michaelboerman/import_parent
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
import_parent-1.1.0.tar.gz -
Subject digest:
c0a7734486b53f0c4acb2cd736d7eef8e1a2a71ceb9285b77aee68392f24c237 - Sigstore transparency entry: 1710339084
- Sigstore integration time:
-
Permalink:
michaelboerman/import_parent@ff10eee900c0f6f46fbcaa57a66e104b165ca57f -
Branch / Tag:
refs/heads/main - Owner: https://github.com/michaelboerman
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@ff10eee900c0f6f46fbcaa57a66e104b165ca57f -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file import_parent-1.1.0-py3-none-any.whl.
File metadata
- Download URL: import_parent-1.1.0-py3-none-any.whl
- Upload date:
- Size: 3.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ee040686a13e2a59992dbd63d6efd6ec2e4c0c1f82dedf26f043d796ada66e0
|
|
| MD5 |
bb7184e0399223f5907a3fba714b161a
|
|
| BLAKE2b-256 |
9ab12d843bd211a3e9f823c64fe10e6eeb947ae25122b13dfb27e4a1a612fd15
|
Provenance
The following attestation bundles were made for import_parent-1.1.0-py3-none-any.whl:
Publisher:
pypi-publish.yml on michaelboerman/import_parent
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
import_parent-1.1.0-py3-none-any.whl -
Subject digest:
2ee040686a13e2a59992dbd63d6efd6ec2e4c0c1f82dedf26f043d796ada66e0 - Sigstore transparency entry: 1710339121
- Sigstore integration time:
-
Permalink:
michaelboerman/import_parent@ff10eee900c0f6f46fbcaa57a66e104b165ca57f -
Branch / Tag:
refs/heads/main - Owner: https://github.com/michaelboerman
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@ff10eee900c0f6f46fbcaa57a66e104b165ca57f -
Trigger Event:
workflow_dispatch
-
Statement type: