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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:37:22+00:00 2026-05-17T15:37:22+00:00

main.py: import subone import subtwo subone.py: a = ‘abc’ subtwo.py: print subone.a Running python

  • 0

main.py:

import subone
import subtwo

subone.py:

a = 'abc'

subtwo.py:

print subone.a

Running python main.py throws a NameError: name 'subone' is not defined. I expected it to print ‘abc’.

Refactoring it to use from import and classes doesn’t help:

main.py:

from subone import *   # Only using from X import * for example purposes.
from subtwo import *

print 'from main.py:', a.out

subone.py:

class A:
    out = 'def'

a = A()

subtwo.py:

# This throws NameError: name 'a' is not defined
print a.out

# This throws NameError: name 'A' is not defined
b = A()
print b.out

BUT it will print ‘from main.py: def’. (It works when using import too.)

Why does it work this way? It seems like once subone is imported, it should be available to subtwo.

Is it because it’s bad programming to have imported modules depend on each other without going through their ‘parent’ module? Is there another, standard way to do this?

Update:

I now understand that the first example will not work because the line print subone.a doesn’t recognize the name subone, it not being in subtwo‘s namespace (even though it’s in main.py‘s), and it is being called from within the module subtwo. This can be fixed by using import subone at the top of subtwo.py — it will not re-load the module but will add it to subtwo‘s namespace so subtwo can use it.

But what about this:

main.py:

from subone import Nugget
from subtwo import Wrap

wrap = Wrap()
print wrap.nugget.gold

subone.py:

class Nugget:
    gold = 'def'

subtwo.py:

class Wrap:
    nugget = Nugget()

I would think that since Wrap and Nugget are both loaded directly into main‘s namespace, that they would use main‘s namespace and be able to reference each other, but it throws a NameError: name 'Nugget' is not defined. IS IT because Wrap is evaluated/checked from within subtwo‘s namespace BEFORE being loaded into main‘s namespace?

  • 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-05-17T15:37:22+00:00Added an answer on May 17, 2026 at 3:37 pm

    If you modified your subtwo.py this way then it will work

    import subone
    print subone.a
    

    When you do subone.a in subtwo.py, you are trying to access the namespace subone in subtwo.py and in the namespace “subone”, there should be a attribute “a”.

    When you do – import subone in subtwo.py, then subone is added to the namespace and subone namespace has attribute a. so subone.a will work.

    I would also suggest that you play with dir() to see how namespaces are being added.

    In subtwo.py, you can do the following:

    print dir()
    import subone
    print dir()
    print subone.a
    

    Similarly, try adding “print dir()” before and after your import statements and the idea should become clear to you.

    “import x” adds ‘x’ to the current modules
    namespace while “from x import * ” will
    add all the module level attributes
    directly into current module namespace

    So in your above first example of main.py, subone.py and subtwo.py, the namespace in main.py will contain ‘subone’ and ‘subtwo’ while subtwo.py will have an empty namespace and can not access subone.a.

    [Edit: Some more explanations]
    Consider following files:
    main.py

    print "Before importing subone : ", dir()
    import subone
    print "After importing subone and before importing subtwo: ",  dir()
    import subtwo
    print "After importing subone and subtwo: ", dir()
    

    subone.py

    a = 'abc'
    

    subtwo.py

    print dir()
    import subone
    print "module level print: ", subone.a
    print dir()
    def printX():
        print subone.a
    

    And the output of running main.py:

    Before importing subone :  ['__builtins__', '__doc__', '__file__', '__name__', '__package__']
    After importing subone and before importing subtwo:  ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'subone']
    ['__builtins__', '__doc__', '__file__', '__name__', '__package__']
    module level print:  abc
    ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'subone']
    After importing subone and subtwo:  ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'subone', 'subtwo']
    

    Some Observations

    1. You will notice that importing a module subtwo.py, the print statement is executed immediately.
    2. So when subone and subtwo are imported in main.py, the namespace of main.py is augmented.
    3. That does not mean that namespace of subtwo will be augmented. so “a” is available only in main.py via subone.a
    4. When we do import subone in subtwo.py then the namespace of subtwo is augmented with subone and attribute a of module subone is available in subtow.py via subone.a
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.