Cat Mai
Cat Mai

Reputation: 470

Setting attributes for Python module instead of file name

I'm new to packaging up Python so not sure what search term to use.

Inside my package, there is a _checksum.py

# _checksum.py

class Add():
     def __init__(self, x, y):
           self.x = x
           self.y = y
    
     def answer(self):
           return self.x + self.y

So to use them, I'd have to import the file name

import MYPACKAGE
import MYPACKAGE._checksum as checksum

test = checksum.Add(3, 4)
test.answer()    #7

So my question is is there a way to set alias to MYPACKAGE._checksum, maybe something like from MYPACKAGE import checksum?

Upvotes: 0

Views: 98

Answers (1)

minker
minker

Reputation: 680

Python relies on filename a lot for module importing, just to make it more intuitive. However, if it's for the package users, you can probably do

# MYPACKAGE/__init__.py
import ._checksum as checksum

So when your users using your package, they can do

# Application code
from MYPACKAGE import checksum

Upvotes: 1

Related Questions