Skip to main content
added 26 characters in body
Source Link
Sean Mackesey
  • 11k
  • 11
  • 46
  • 74

I have a Python utility library pfacets. This library uses a one-function-per-module structure, with the modules named the same as the function. I want to expose all the functions in this library as attributes of the pfacets module. Currently, pfacets/__init__.py looks like this:

from func1 import func1 from func2 import func2 ... 

Is there a way to automate this so that I don't have to maintain this list? I tried:

for p in glob.glob(os.path.join(os.path.dirname(__file__), '*.py')): if not os.path.basename(p).startswith('__'): name = os.path.basename(x)[:-3] __import__(name, globals(), locals(), [name]) 

I was thinking that having name in the from-list (the 4th arg to __import__) would handle this, but it didn't work-- name was bound to the module, not the function.

I have a Python utility library pfacets. This library uses a one-function-per-module structure, with the modules named the same as the function. I want to expose all the functions in this library as attributes of the pfacets module. Currently, pfacets/__init__.py looks like this:

from func1 import func1 from func2 import func2 ... 

Is there a way to automate this so that I don't have to maintain this list? I tried:

for p in glob.glob(os.path.join(os.path.dirname(__file__), '*.py')): if not p.startswith('__'): name = basename(x)[:-3] __import__(name, globals(), locals(), [name]) 

I was thinking that having name in the from-list (the 4th arg to __import__) would handle this, but it didn't work-- name was bound to the module, not the function.

I have a Python utility library pfacets. This library uses a one-function-per-module structure, with the modules named the same as the function. I want to expose all the functions in this library as attributes of the pfacets module. Currently, pfacets/__init__.py looks like this:

from func1 import func1 from func2 import func2 ... 

Is there a way to automate this so that I don't have to maintain this list? I tried:

for p in glob.glob(os.path.join(os.path.dirname(__file__), '*.py')): if not os.path.basename(p).startswith('__'): name = os.path.basename(x)[:-3] __import__(name, globals(), locals(), [name]) 

I was thinking that having name in the from-list (the 4th arg to __import__) would handle this, but it didn't work-- name was bound to the module, not the function.

Source Link
Sean Mackesey
  • 11k
  • 11
  • 46
  • 74

Automating Import of a function from all modules in a package in python

I have a Python utility library pfacets. This library uses a one-function-per-module structure, with the modules named the same as the function. I want to expose all the functions in this library as attributes of the pfacets module. Currently, pfacets/__init__.py looks like this:

from func1 import func1 from func2 import func2 ... 

Is there a way to automate this so that I don't have to maintain this list? I tried:

for p in glob.glob(os.path.join(os.path.dirname(__file__), '*.py')): if not p.startswith('__'): name = basename(x)[:-3] __import__(name, globals(), locals(), [name]) 

I was thinking that having name in the from-list (the 4th arg to __import__) would handle this, but it didn't work-- name was bound to the module, not the function.