Skip to main content

Insider Scripts

Purpose

Run python scripts from inside a package without relative import error.

Often when working on a project, one might want scripts to be callable from anywhere within the tree of the project.

For instance with the following layout:

your_package/
|── scriptX.py
|── A_folder/
|   ├── A.py
|   ├── C.py
|   └── scriptA.py
|── B_folder/
|   └── B.py

The following import in C.py will fail if the scriptA.py imports C and is run from inside the package:

from .A import A # relative import with no known parent package.
from ..B_folder.B import B # relative import with no known parent package.

Absolute import of A would work:

import A.A

but it would the fail if scriptX.py imports C.

Using the trick of adding a path to sys.path will work most of the time, however if you have the following situation:

C.py

import sys
from importlib import Path
sys.path.append(Path(__file__).parent.parent)
from B import B

def make_B() -> B:
  return B()

scriptX.py

from A_folder.C import make_B
from B_folder.B import B
b = make_B()
print(isinstance(b, B)) # prints False

scriptX.py will print False because make_B produces an instance of B.B while the imported B is an instance of B_folder.B.B.

The best solution I found to solve those issue resulted in the creation of the insider_scripts package.

Installation

pip install insider_scripts

Usage

Import the function define script from the insider_scripts and call it with either the depth of the script in the package, or the path of the package root.

Example layout

your_package/
|── A_folder/
|   ├── A.py
|   └── script.py
|── B_folder/
|   └── B.py

Example script:\n"

from pathlib import Path
from insider_scripts import define_script
define_script(1) # or define_script(Path(__file__).parent.parent) or define_script(-1)

from .A import A
from ..B_folder.B import B

Metadata

Release files for insider-scripts 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for insider-scripts 1.1.0
File Size Uploaded Provenance Actions
insider_scripts-1.1.0.tar.gz 3.5 kB Without provenance Details for insider_scripts-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for insider-scripts 1.1.0
File Interpreter ABI Platform Actions
insider_scripts-1.1.0-py3-none-any.whl Python 3 none any Details for insider_scripts-1.1.0-py3-none-any.whl

Total release size: 7.0 kB

Release files / insider_scripts-1.1.0.tar.gz

Download URL insider_scripts-1.1.0.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a67288f3141c0599247ef74f4311eb7689e489394cb009d44b2a7f29f1d16dc9
BLAKE2b-256 checksum
How to use checksums
04b9b5bac68901bbc71f0931656a34d08a154c7a814b1d1cb80cbfcb08c79f3f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.1

Release files / insider_scripts-1.1.0-py3-none-any.whl

Download URL insider_scripts-1.1.0-py3-none-any.whl
Size 3.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b53c3f524716aea511ce4024226817c91f374c362c934f05e557d8107a12fc14
BLAKE2b-256 checksum
How to use checksums
8b2677e4c72dc472a86e77cedbe4833d8f2af3d595e343e71a48b34d39a6db5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.1

Security for insider-scripts 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page