Skip to main content

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

Project description

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

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

insider_scripts-1.1.0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

insider_scripts-1.1.0-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: insider_scripts-1.1.0.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for insider_scripts-1.1.0.tar.gz
Algorithm Hash digest
SHA256 a67288f3141c0599247ef74f4311eb7689e489394cb009d44b2a7f29f1d16dc9
MD5 13cc739b5735ea1f8a76bd0ab3d2bd83
BLAKE2b-256 04b9b5bac68901bbc71f0931656a34d08a154c7a814b1d1cb80cbfcb08c79f3f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for insider_scripts-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b53c3f524716aea511ce4024226817c91f374c362c934f05e557d8107a12fc14
MD5 a81503a4f1169d33da89b1f25c7e7f8f
BLAKE2b-256 8b2677e4c72dc472a86e77cedbe4833d8f2af3d595e343e71a48b34d39a6db5f

See more details on using hashes here.

Supported by

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