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 8477421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:27:51+00:00 2026-06-10T18:27:51+00:00

Having already use flat packages, I was not expecting the issue I encountered with

  • 0

Having already use flat packages, I was not expecting the issue I encountered with nested packages. Here is…

Directory layout

dir
 |
 +-- test.py
 |
 +-- package
      |
      +-- __init__.py
      |
      +-- subpackage
           |
           +-- __init__.py
           |
           +-- module.py

Content of init.py

Both package/__init__.py and package/subpackage/__init__.py are empty.

Content of module.py

# file `package/subpackage/module.py`
attribute1 = "value 1"
attribute2 = "value 2"
attribute3 = "value 3"
# and as many more as you want...

Content of test.py (3 versions)

Version 1

# file test.py
from package.subpackage.module import *
print attribute1 # OK

That’s the bad and unsafe way of importing things (import all in a bulk), but it works.

Version 2

# file test.py
import package.subpackage.module
from package.subpackage import module # Alternative
from module import attribute1

A safer way to import, item by item, but it fails, Python don’t want this: fails with the message: “No module named module”. However …

# file test.py
import package.subpackage.module
from package.subpackage import module # Alternative
print module # Surprise here

… says <module 'package.subpackage.module' from '...'>. So that’s a module, but that’s not a module /-P 😯 … uh

Version 3

# file test.py v3
from package.subpackage.module import attribute1
print attribute1 # OK

This one works. So you are either forced to use the overkill prefix all the time or use the unsafe way as in version #1 and disallowed by Python to use the safe handy way? The better way, which is safe and avoid unecessary long prefix is the only one which Python reject? Is this because it loves import * or because it loves overlong prefixes (which does not help to enforce this practice)?.

Sorry for the hard words, but that’s two days I trying to work around this stupid‑like behavior. Unless I was totally wrong somewhere, this will leave me with a feeling something is really broken in Python’s model of package and sub‑packages.

Notes

  • I don’t want to rely on sys.path, to avoid global side effects, nor on *.pth files, which are just another way to play with sys.path with the same global effets. For the solution to be clean, it has to be local only. Either Python is able to handle subpackage, either it’s not, but it should not require to play with global configuration to be able to handle local stuff.
  • I also tried use imports in package/subpackage/__init__.py, but it solved nothing, it do the same, and complains subpackage is not a known module, while print subpackage says it’s a module (weird behavior, again).

May be I’m entirely wrong tough (the option I would prefer), but this make me feel a lot disappointed about Python.

Any other known way beside of the three I tried? Something I don’t know about?

(sigh)

—– %< —– edit —– >% —–

Conclusion so far (after people’s comments)

There is nothing like real sub‑package in Python, as all package references goes to a global dictionnary, only, which means there’s no local dictionary, which implies there’s is no way to manage local package reference.

You have to either use full prefix or short prefix or alias. As in:

Full prefix version

from package.subpackage.module import attribute1
# An repeat it again an again
# But after that, you can simply:
use_of (attribute1)

Short prefix version (but repeated prefix)

from package.subpackage import module
# Short but then you have to do:
use_of (module.attribute1)
# and repeat the prefix at every use place

Or else, a variation of the above.

from package.subpackage import module as m
use_of (m.attribute1)
# `m` is a shorter prefix, but you could as well
# define a more meaningful name after the context

Factorized version

If you don’t mind about importing multiple entity all at once in a batch, you can:

from package.subpackage.module import attribute1, attribute2
# and etc.

Not in my first favorite taste (I prefer to have one import statement per imported entity), but may be the one I will personally favor.

Update (2012-09-14):

Finally appears to be OK in practice, except with a comment about the layout. Instead of the above, I used:

from package.subpackage.module import (

    attribute1, 
    attribute2,
    attribute3,
    ...)  # and etc.
  • 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-10T18:27:53+00:00Added an answer on June 10, 2026 at 6:27 pm

    You seem to be misunderstanding how import searches for modules. When you use an import statement it always searches the actual module path (and/or sys.modules); it doesn’t make use of module objects in the local namespace that exist because of previous imports. When you do:

    import package.subpackage.module
    from package.subpackage import module
    from module import attribute1
    

    The second line looks for a package called package.subpackage and imports module from that package. This line has no effect on the third line. The third line just looks for a module called module and doesn’t find one. It doesn’t “re-use” the object called module that you got from the line above.

    In other words from someModule import ... doesn’t mean “from the module called someModule that I imported earlier…” it means “from the module named someModule that you find on sys.path…”. There is no way to “incrementally” build up a module’s path by importing the packages that lead to it. You always have to refer to the entire module name when importing.

    It’s not clear what you’re trying to achieve. If you only want to import the particular object attribute1, just do from package.subpackage.module import attribute1 and be done with it. You need never worry about the long package.subpackage.module once you’ve imported the name you want from it.

    If you do want to have access to the module to access other names later, then you can do from package.subpackage import module and, as you’ve seen you can then do module.attribute1 and so on as much as you like.

    If you want both — that is, if you want attribute1 directly accessible and you want module accessible, just do both of the above:

    from package.subpackage import module
    from package.subpackage.module import attribute1
    attribute1 # works
    module.someOtherAttribute # also works
    

    If you don’t like typing package.subpackage even twice, you can just manually create a local reference to attribute1:

    from package.subpackage import module
    attribute1 = module.attribute1
    attribute1 # works
    module.someOtherAttribute #also works
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just started mongo and started having issue with querying already. i have a collection
if I'm already having person name under/over image then should i use same name
using magento 1.7.0.2 here, having already tested lot of recommended solutions (reindexing, clearing cache,
Scenario Having already read a post on this on the same site, which didn't
I'm having content which encoded in base 64 format already. I want to set
I am having a problem with detecting if an interface class is already declared
As many of you may already know I have been having issues with another
I'm having a bit of trouble loading pages into an already-existing colorbox. I have
I've already looked around but couldn't find the exact solution/problem I'm having right now.
I'm having troubles with creating a simple list (some expandable lists are already working).

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.