Skip to main content

python-require (repyrer)

Provides nodejs-style requiring of modules for python

This module allows imports of python modules using syntax similar to the node.js require function. In particular, it takes string arguments comprised of (optionally) a UNIX-style directory path and a module name, and returns a reference to the loaded module. Specifying no directory path will have the module import attempted first in the current working directory, then on the python path variable (sys.path), allowing both local-to-the-folder imports and standard library imports (as well as anything else you've configured into sys.path). If an (absolute) directory path is included (i.e. a leading / in the directory path), the module will first be attempted to be loaded from that directory first, then the rest of the sys.path as a fallback. If a relative directory path is provided (like lib/module_name or ../other_lib/module_name) the current working directory is used to resolve the relative path.

NOTE: the leading ./ characters on a directory path (as in ./lib/module_name) indicate that the directory path should be resolved relative to the current file NOT the current working directory. For example, suppose you have the following file structure:

top_folder
|-----main.py
|-----lib
  |-----A.py
  |-----sub_lib
    |-----B.py

Suppose main.py imports A.py as a module, and A.py in tern imports B.py as a module. Inside of A.py, it should use B = repyre('./sublib/B'), NOT B = repyre('sublib/B') since the latter will fail when main.py imports A.py (because the current working directory will be top_folder NOT lib). However, main.py is free to import A.py as A = repyre('lib/A') or A = repyre('./lib/A') provided main.py will only ever be run directly as a script from top_folder.

Example Usage

You can use repyrer exactly as you would import to access the standard library, and anything else you might have pip installed:

>>> from repyrer import repyre
>>> os = repyre('os') # import standard library
>>> np = repyre('numpy') # import other libraries installed with pip, etc.
>>> plt = repyre('matplotlib.pyplot') # import sub-modules using '.' notation

You can also specify a directory path (relative to the current working directory, or absolute) preceding the module name to access local libraries, suppose you have a file structure like:

folder
|----CWD
  |----main.py
  |----adjacent_file.py
  |----my_dev_lib
    |-----module_A.py
|----my_other_lib
  |-----module_B.py

Then, in the python shell

>>> adjacent = repyre('adjacent_file') # import other files relative to the CWD
>>> A = repyre('my_dev_lib/module_A') # import modules from a relative directory path (relative to CWD)
>>> B = repyre('../my_other_lib/module_B') # can use '..' in directory paths

And in a script like main.py:

from repyrer import repyre

adjacent = repyre('adjacent_file') # only works if script is run in this folder
adjacent = repyre('./adjacent_file') # safe to run from anywhere

Metadata

Release files for repyrer 0.4.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 repyrer 0.4.0
File Size Uploaded
repyrer-0.4.0.tar.gz 3.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for repyrer 0.4.0
File Interpreter ABI Platform
repyrer-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.5 kB

Release files / repyrer-0.4.0.tar.gz

Download URL repyrer-0.4.0.tar.gz
Size 3.3 kB
Tags Source
SHA-256 checksum
How to use checksums
90ff5c646a232ba2f8e6feaa041b3db04104cc112ec83296e9dc54a486cd1ed8
BLAKE2b-256 checksum
How to use checksums
38cd63fe173c65596686fc9a03abf1fd03a221bd31ef7f1cb10f47cfd8b47861
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.0.post20201006 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.7.5

Release files / repyrer-0.4.0-py3-none-any.whl

Download URL repyrer-0.4.0-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f359173b1238755689e6f2268b5e4a72aacfc92c8c098983675663a65291b117
BLAKE2b-256 checksum
How to use checksums
dd0e5a2b522dee9db251d6104052dc50c5e0733843c8b068f850c378dd109af7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.0.post20201006 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.7.5

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.2.0

1 release file

0.1.0

1 release file

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