0

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?

2
  • You're mandatory to use the filename somewhere Commented Dec 17, 2020 at 20:01
  • Yeah my question is if there's a way to set attributes in some setup file so the import command wouldn't reveal the filename? Commented Dec 17, 2020 at 20:02

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that's really simple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.