Linked Questions
17 questions linked to/from Callable modules
12
votes
2
answers
69k
views
How do I export a single function as a module in Python? [duplicate]
I'm writing a module called foo that has many internal functions, but ultimately boils down to a single external function foo(). When using this module/function, I'd like to do
import foo
foo(...
user avatar
user3064538
0
votes
0
answers
250
views
How to pass arguments to __init__.py? [duplicate]
Considering a namespace package in python 3.7, how would I pass an argument to my package when I initialise it?
my_package
__init__.py
I wish to be able to call my_package(arg).
Thank you,
0
votes
2
answers
111
views
Import and call Python module directly [duplicate]
Apologies if this has been asked before, I'm not sure how to search for this.
I'm trying to structure my module in a way that allows me to do this:
import module
module()
How can I do this?
My ...
151
votes
9
answers
78k
views
__getattr__ on a module
How can implement the equivalent of a __getattr__ on a class, on a module?
Example
When calling a function that does not exist in a module's statically defined attributes, I wish to create an ...
139
votes
8
answers
46k
views
Can modules have properties the same way that objects can?
With python properties, I can make it such that
obj.y
calls a function rather than just returning a value.
Is there a way to do this with modules? I have a case where I want
module.y
to call a ...
8
votes
5
answers
2k
views
Can a python module have a __repr__?
Can a python module have a __repr__? The idea would be to do something like:
import mymodule
print mymodule
EDIT: precision: I mean a user-defined repr!
13
votes
2
answers
5k
views
Retrieve module object from stack frame
Given a frame object, I need to get the corresponding module object. In other words, implement callers_module so this works:
import sys
from some_other_module import callers_module
assert sys....
user avatar
Roger Pate
10
votes
3
answers
350
views
Altering traceback of a non-callable module
I'm a minor contributor to a package where people are meant to do this (Foo.Bar.Bar is a class):
>>> from Foo.Bar import Bar
>>> s = Bar('a')
Sometimes people do this by mistake (...
1
vote
1
answer
2k
views
Can I make a Python Module __init__.py callable?
The documentation for __init__.py is quite hard to find. I can't find a place that explains all the things you can do in this file. The Python module documentation barely even mentions __init__.py nor ...
0
votes
2
answers
1k
views
short import paths in Python
I'm coming back to python and having trouble with import paths. Can someone explain the logic, or a better way of doing this?
If I have a class called Sentence in structure like
base.py
models/...
-1
votes
1
answer
369
views
python chaining modules execution
I do have the situation as follows:
main.py
moduleA.py
moduleB.py
moduleA contains main() function
moduleB contains main() function
I do in main:
import moduleA, moduleB
def main():
moduleA....
1
vote
3
answers
213
views
Why do imports not work when creating custom module classes?
I have this module, wat.py
import re
import types
import sys
hello = "Hello World"
class MyModule(types.ModuleType):
def get_re(self):
return re
def get_hello(self):
return ...
Eric's user avatar
- 98.1k
1
vote
1
answer
378
views
Callable modules with parameters
Is it possible to make a module callable with parameters?
I am trying to make a callable module on Python, following the question and its answers Callable modules, like so:
foo.py
import sys
class ...
1
vote
1
answer
182
views
Make a Python module's name its root class instantiated
I have a module with the structure:
- mymodule
- mymodule
__init__.py
core.py
setup.py
__init__.py contents:
from .core import MyClass
So now when I import my module I have to do:
...
-1
votes
1
answer
119
views
Imports and name bindings best practice
This is closely related to: Importing modules in Python - best practice
, but the following case is not really discussed in details.
If we have a module coordinates that defines one class ...