Skip to content

Suggestion for disambiguation re import expectations and __init__.py #1929

@malcolm-heath

Description

@malcolm-heath

Issue Description

Having just gone through the tutorial, I found myself confused because once the project is packaged and installed, and then imported, the add_one() function seemed unavailable.

  • "import example_package" was without error, but attempting to use example_package.add_one(3) gave AttributeError: module 'example_package' has no attribute 'add_one'

  • "from example_package import add_one" gives ImportError: cannot import name 'add_one' from 'example_package'

  • "from example_package import *" runs without error, but then attempting to use add_one gives NameError: name 'add_one' is not defined

The only way I have been able to run "add_one()" has been as
"from example_package.example import add_one".

While this is understandable as the function is defined in the top level example.py file, it definitely violated my expectations as almost every package I use would allow me to use "import example_package" and then "example_package.add_one()" or "from example_package import add_one" and then run "add_one()"

I am suggesting either an example of loading the package and using the function, or the addition of modifying the init.py to add "from .example import add_one"

Code of Conduct

  • I am aware that participants in this repository must follow the PSF Code of Conduct.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions