Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alias to python module

Tags:

python

I have some python module, that I import as:

from mygraph.draw import pixel

The file structure looks like that:

mygraph/
    __init__.py
    draw.py

and draw.py contains def pixel()

Now, I want to add another function, line(), and I want to import it as

from mygraph.draw import line

I can simply add line to draw.py. But, I would like to have line() in a separate file line.py and not mess with draw.py. But, if I place it in a separate file, it will be imported as

from mygraph.line import line

and that is not what I want...

Is it possible to "alias" somehow the line.py so it is visible in draw module, but is in separete file? I thought about adding a dummy function in draw

def line():
    return real_line.line()

but in this case I won't have a "docstring" from the original line, and I will loose some performance on calling the real line function.

like image 365
Jakub M. Avatar asked Dec 16 '22 05:12

Jakub M.


1 Answers

Try this in your draw.py module:

from line import line

and you should be able to invoke it as mygraph.draw.line, and import it the way you wanted.

I do this a lot in my __init__.py files: expose the primary API this way.

like image 167
Has QUIT--Anony-Mousse Avatar answered Dec 22 '22 00:12

Has QUIT--Anony-Mousse