Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7846623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:38:42+00:00 2026-06-02T17:38:42+00:00

The import function of Python still confuses me sometimes. Here’s an example: My project

  • 0

The import function of Python still confuses me sometimes. Here’s an example:

My project has the following package structure:

Project/
    src/
        example/
            __init__.py
            an_example.py
        top/
            __init__.py
            lin/
                __init__.py
                factory.py

In an_example.py, I’d like to write

from top import lin
if __name__ == '__main__':
    a = lin.factory.AClass()

However, this fails with:

    a = lin.factory.AClass()
AttributeError: 'module' object has no attribute 'factory'

an_example.py works when written like this:

from top.lin import factory
if __name__ == '__main__':
    a = factory.AClass()

Can you explain to me why it is wrong to write the import statement like the first version? I’d prefer a fully qualified name like lin.factory.AClass to factory.AClass.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:38:43+00:00Added an answer on June 2, 2026 at 5:38 pm

    This is because, unless you tell lin to import factory in __init__.py, then factory is not in the lin namespace.

    E.g:

    Presume your existing project structure, with an_example.py containing:

    from top import lin
    
    lin.factory.AClass()
    

    With top/lin/__init__.py blank, we get ImportError: No module named factory.

    With top/lin/__init__.py containing import top.lin.factory, we get no error.

    When you ask to use factory.AClass(), it works because it is defined there. Likewise, you need to define factory in lin if you want to use it from there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is canonical example of a program extending embedded Python 3.x in C/C++: #include
I need to import a 3rd party module inside my pl/python function. It seems
I'm trying to interop with C in Python, and I have the following function
first question here. I am trying to learn python by stepping through project euler,
I'm trying to write a function in Python (still a noob!) which returns indices
Python has a few ways of printing trace output. print , import logging ,
I'm trying to write an import function for getting data out of an excel
I'm horrible with regex but i'm trying to figure out how an import function
When you use DllImport to import a function you can specify a CharSet to
Is it possible to use a function import in a where clause in entity

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.