I am trying to understand python packages, to eventually generate my own packages.
I read some tutorial and try to code some examples, but I am not stuck with some understanding of the init.py file.
I try to follow this tutorial, https://timothybramlett.com/How_to_create_a_Python_Package_with___init__py.html
but I get the following error at example3.py
AttributeError: module 'string_func' has no attribute 'stringLength'
This is the tree of files:
.
├── example2.py
├── example3.py
└── string_func
├── __init.py__
├── stringLength.py
├── stringToLower.py
└── stringToUpper.py
and here the init file
$ cat string_func/__init.py__
from .stringLength import stringLength
from .stringToLower import stringToLower
from .stringToUpper import stringToUpper
and the two shortened examples: example2:
import string_func.stringLength
some_string = "Hello, Universe!"
print(string_func.stringLength.stringLength(some_string))
Returns 16 as expected.
And example3:
import string_func
some_string = "Hello, Universe!"
print(string_func.stringLength(some_string))
I am using:
python3 --version
Python 3.9.9
But got the same error on a python 3.6.x
It feels like the init file is ignored, but I cannot understand why.
CodePudding user response:
it should be
__init__.py
not
__init.py__
CodePudding user response:
Your file name in init.py is not correct. You should rename it as __init__.py
The reason for this is after the last "dot" then it should be the file extension, hence your init fill would be a file with "__"
extension. Therefore file should be renamed as "__init__.py"