cant't use package internals
Peter Otten
__peter__ at web.de
Wed Sep 6 05:12:34 EDT 2017
Andrej Viktorovich wrote:
> Hello,
>
> I have Python package tst in my workspace.
>
> tst has files:
> __init__.py
> tst.py
>
>
> content of __init__.py:
> print("importing Tst")
>
>
> content of tst.py:
> class Tst:
> def __init__(self):
> print("init Tst")
>
>
> I run python console in workspace directory. I do
>>>>import tst
> Run without errors. I was expected "importing Tst" to be printed, but
> nothing happened. Why?
You may have a second tst.py, e. g. in your working directory, that is found
first and thus shadows the tst package. A variant of that explanation would
be that the tst folder is your working directory and you are importing the
tst submodule directly instead as part of a package.
In the interactive interpreter you can use the tst.__file__ attribute to
find out what is actually going on.
To make things a bit clearer I recommend that you give package and submodule
different names, at least while you are experimenting.
> I run
> t=tst.tst.Tst()
>
> and got error:
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> AttributeError: module 'tst' has no attribute 'tst'
>
> Wh it not finds Tst class?
Submodules of a package are not imported automatically.
You need
import tst.tst
t = tst.tst.Tst()
More information about the Python-list
mailing list