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

  • SEARCH
  • Home
  • 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 9191069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:35:51+00:00 2026-06-17T20:35:51+00:00

So I want to import a module from a python package that is a

  • 0

So I want to import a module from a python package that is a subdirectory of the one where the main script is located. This is the directory structure:

maindir \
  - main.py
  - modules \
    - __init__.py
    - module.py

What I find weird is that I can’t import module.py with a simple import modules.module, because when I then try to call the function module.foo() in main.py it returns NameError: name 'module' is not defined. However, when I do import modules.module as module, everything works fine, the same with from modules import module. And when module.py is located in the same directory as main.py, a simple import module is completely sufficient for calling module.foo().

Now the question is, why is that? Why isn’t a simple import statement enough for importing a module from a package instead of the directory the script is in? Or am I doing something else wrong? An answer would be really appreciated since I am rather confused right now…

  • 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-17T20:35:53+00:00Added an answer on June 17, 2026 at 8:35 pm

    It does import the module, it just doesn’t make its name directly accessible. When you do import foo.bar, the name that is imported is foo, and bar is only accessible as an attribute of that. You use that form of import if that is what you want; that’s what it’s for. If you don’t want that, use a different form of the import statement.

    If you want to be able to type module instead of modules.module, either do import modules.module as module, as you found, or do from modules import module.

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

Sidebar

Related Questions

I want to package a Python script so that it can run as a
Is it possible to import everything except one module from a package? I need
In python, if you need a module from a different package you have to
Possible Duplicate: How to import the src from the tests module in python I
I am wondering why the directory (subpackage) that holds submodules in a python package
i want to import org.apache.commons.io but i'm getting this error: [info] Compiling 1 Java
I want to import 100 million entries from a text file (each row is
I have a file that I want to import into Microsoft Dynamics CRM 2011
I have a method I want to import from a DLL and it has
I have a SimpleXMLElement Object that I want to import with PHP. But it

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.