nebffa
nebffa

Reputation: 1549

Unable to use __init__.py

I must be missing something simple here.

I have a directory structure:

maths/
    __init__.py
    test.py
    ...

    foo/
       __init__.py
       ...

    bar/
       __init__.py
       ...

In multiple files (but not all) I am using the path of the module - on Ubuntu for example - that path is /home/nebffa/Desktop/maths for some tasks. However, I have to compute the path of the maths package in all of those files - so I thought maybe it will be easier to just have it available via __init__.py. At least that's what I thought I could do based on reading up on __init__.py - maybe I'm wrong?

Anyway, attempts to make things available by putting them in the base __init__.py have failed to work so I think I might be misunderstanding this Python concept.

Upvotes: 1

Views: 99

Answers (2)

Adam
Adam

Reputation: 17389

If you run python test.py then __init__.py is not involved at all.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799390

maths and maths.test are two separate modules, each with their own distinct namespace. You will need to explicitly import itertools in maths.test if you want it available there.

Also, python -m maths.test.

Upvotes: 1

Related Questions