0

I have a problem with a self-written package. I use Python 3.9. The package was installed correctly.

There is 1 function. This returns a string.

The function is shown here in simplified form. It is located in the user.py file:

def get_user(user):
     print(user)

The package itself is structured as follows.

lichesspy:.
|   account.py
|   user.py
|   vars.py
|   __init__.py

The init.py file is structured as follows:

from lichesspy.user import get_user
# from lichesspy.user import user_tos

If I list all functions there as a directory I can call them normally in a package foreign Python file. But I don't think this is the right way to use an init.py.

Here is a test that often works. So a foreign test.py file:

import lichesspy as lp
print(lp.get_user("Zeyecx"))

How do I have to adapt the init.py to be able to use it sensibly?

Zeyecx
  • 73
  • 1
  • 3
  • By "sensibly", what exactly do you mean here? If everything is working as intended then you don't really have a question here? – metatoaster May 17 '21 at 02:09
  • @metatoaster: That's right. However, I think that the __init__.py is not meant to be a collection of the functions. I'm trying to figure out how to solve this better. – Zeyecx May 17 '21 at 02:16
  • Why not? What you described and using for `lichesspy` is the same technique that Python standard library uses to provide friendlier import statements to classes/functions contained inside submodules of a given package, while maintaining sensible division of different groups of code. As an example, the `unittest` standard library module contains [this `__init__.py`](https://github.com/python/cpython/blob/v3.9.5/Lib/unittest/__init__.py#L59..L67) so `from unittest import TestCase` may be written instead of the more verbose `from unittest.case import TestCase` to import the `TestCase` class. – metatoaster May 17 '21 at 02:23
  • Alternatively, you can just leave `__init__.py` empty when in doubt. For further reading, you may want to read through [what is `__init__.py` for?](https://stackoverflow.com/questions/448271/). – metatoaster May 17 '21 at 02:29

0 Answers0