按物理文件路径导入模块
magicalimport的Python项目详细描述
从物理文件路径导入模块。
示例
<>这些文件是存在的,然后…$ tree . ├── a │ └── b │ └── c │ └── foo.py └── main.py 4 directories, 3 files
A/B/C/FOO.PY
name="foo"_age="*secret*"
frommagicalimportimportimport_from_physical_path# importing foo.py as the module named foo2foo=import_from_physical_path("./a/b/c/foo.py",as_="foo2")print(foo.name)# cached in sys.modules, so it is ok.importfoo2print(foo2.name)
此处选项
frommagicalimportimportimport_from_physical_pathimport_from_physical_path("bar.py",here="/tmp/foo",as_="bar")
星型导入
frommagicalimportimportimport_from_physical_pathfrommagicalimportimportexpose_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 "_"# orfrommagicalimportimportexpose_membersexpose_members(import_from_physical_path("./a/b/c/foo.py"),members=["_age"])print(_age)# "*secret*"
0.5
- 引发异常调整
0.3
- 导入符号中的ns选项
0.2.1
- 此处支持选项
0.2
- python2支持
0.1
- 首次发布