Skip to main content

importing a module by physical file path

Project description

https://travis-ci.org/podhmo/magicalimport.svg?branch=master

Importing a module from physical file path.

examples

these files are existed, then..

$ tree
.
├── a
   └── b
       └── c
           └── foo.py
└── main.py

4 directories, 3 files

a/b/c/foo.py

name = "foo"
_age = "*secret*"
from magicalimport import import_from_physical_path

# importing foo.py as the module named foo2
foo = import_from_physical_path("./a/b/c/foo.py", as_="foo2")
print(foo.name)

# cached in sys.modules, so it is ok.
import foo2
print(foo2.name)

here option

from magicalimport import import_from_physical_path

import_from_physical_path("bar.py", here="/tmp/foo", as_="bar")

star import

from magicalimport import import_from_physical_path
from magicalimport import expose_all_members

# something of like a `from foo import *`
expose_all_members(import_from_physical_path("./a/b/c/foo.py"))
print(name)  # "foo"
# print(_age)  # NameError.. because expose_all_members() doesn't expose the symbols started by "_"

# or
from magicalimport import expose_members
expose_members(import_from_physical_path("./a/b/c/foo.py"), members=["_age"])
print(_age)  # "*secret*"

0.9.1

  • import_module() and import_symbol()’s cwd is True

0.9.0

0.8.1

  • cwd option

0.8.0

  • default behaviour is changed, from now, if here option is omitted, treated as here=__file__ on caller module.

  • ( previous behaviour is here=os.getcwd() )

0.7.3

  • skip needless __init__.py creation, in some cases

0.7.2

  • fix directly import module handling is not enough, now, treated as a same module

0.7.1

  • fix permission error, and same file, treated as a same module

0.7.0

  • fix same file, treated as a same module

0.6.0

  • fix module_id is not stored in sys.modules, on exec_module()

0.5.0

  • raised exception adjustment

0.3.0

  • ns option in import_symbol

0.2.1

  • here option is supported

0.2

  • python2 support

0.1

  • first release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

magicalimport-0.9.1-py2.py3-none-any.whl (7.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file magicalimport-0.9.1-py2.py3-none-any.whl.

File metadata

  • Download URL: magicalimport-0.9.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.1

File hashes

Hashes for magicalimport-0.9.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3fae46743f008df7c6cb26ee9cbd3eddd985e7b7e366a25af68e530ca64f168c
MD5 bfaa5e1136f8b2e7174b5102c6c6d4f1
BLAKE2b-256 3f64723500d99bc226b00d385d49aa05a953fbf9fe678c26ba4e383869940c8b

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