I am new to version 3.x, also new to putting together my own custom library. Here is just a practice library I put together contained in the "example" folder: .../site-packages/example/
Please reference below (toward the bottom) where I've provided the contents of all the files contained in the example library:
- __init__.py
- A.py
- B.py
So in the python command line, the following works just fine
>>> import example as ex
>>> ex.a1()
a1
>>>
'ex.a1()' ouputs a1 as expected.
But I'm looking for a way to access function a1() without having to go through the "ex." like.. so:
>>> from example import a1
>>> a1()
a1
>>>
Is there a way I can enable this in the __init__.py file? ie there are a number of functions id like to be able to access directly without having to import each and every one of them via the example. function
__init__.py:
# from .FOLDER import FUNCTION
from .A import *
from .B import b1
A.py:
def a1(): # simply prints.. "a1"
print('a1')
def a2(): # simply prints.. "a2"
print('a2')
B.py:
def b1(): # simply prints.. "b1"
print('b1')
def b2(): # simply prints.. "b2"
print('b2')
CodePudding user response:
I'm actually a bit surprised this doesn't work.. Anyways: You can add a .
in front of the module imports in __init__.py
to make them relative imports. That worked for me.
P.S.: You can write your module in a folder that's convenient for you, and then install it with a dynamic link: pip install -e .
CodePudding user response:
Not sure if this is the correct general solution but.. I was able to get/enable the features I was looking for with the following import statement:
>>> from example import *
The solution makes sense to me however if this is not the general solution to accomplishing this, someone please speak out. Thanks!