Skip to main content

Nodejs-style requires for python

Project description

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

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

repyrer-0.4.0.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

repyrer-0.4.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file repyrer-0.4.0.tar.gz.

File metadata

  • Download URL: repyrer-0.4.0.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for repyrer-0.4.0.tar.gz
Algorithm Hash digest
SHA256 90ff5c646a232ba2f8e6feaa041b3db04104cc112ec83296e9dc54a486cd1ed8
MD5 aab05425d5b29a100aa3879a89813a81
BLAKE2b-256 38cd63fe173c65596686fc9a03abf1fd03a221bd31ef7f1cb10f47cfd8b47861

See more details on using hashes here.

File details

Details for the file repyrer-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: repyrer-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for repyrer-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f359173b1238755689e6f2268b5e4a72aacfc92c8c098983675663a65291b117
MD5 e6510f2bc2b0b4fe4c6e7d87da64ccc3
BLAKE2b-256 dd0e5a2b522dee9db251d6104052dc50c5e0733843c8b068f850c378dd109af7

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