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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:43:56+00:00 2026-05-26T23:43:56+00:00

I have a question which seems to be rather fundamental but I can’t seem

  • 0

I have a question which seems to be rather fundamental but I can’t seem to find any help on this anywhere.

file_a.py >>

from xyz import XYZ
class A:
    .
    .
    .

file_b.py >>

import file_a
from file_a import A
class B(A):
    def __init__(self):
        A.__init__(self)

    def someMethod(self):
        XYZ.doSomething()

XYZ.doSomething() fails saying NameError: name ‘XYZ’ is not defined
Even standard imports like ‘import sys’ done from file_a does not seem to render it usable in file_b. I assumed that should work. Is my understanding wrong? If yes, then is there a way to have common imports and global variables across files? (If it is of nay help, I’ve been a C++ and java programmer and am now starting to use python. )

  • 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-26T23:43:57+00:00Added an answer on May 26, 2026 at 11:43 pm

    Is my understanding wrong?

    Yes, because the line from file_a import A import only class A into the namespace of file_b. The namespace of file_a is left alone. If it were not like this, there would be little sense in having both syntax:

    import modulename
    from modulename import something
    

    as if your thinking was right, then after the second form you would always be able to use modulename.someotherthing.

    If yes, then is there a way to have common imports and global variables across files?

    Yes, with the star * operator:

    from modulename import *
    

    but this brings the issue of namespace pollution, for example from file_a import * will import in file_b also all the imports done in file_a. You will eventually lose control of your imports and this will bite you at some time… trust me on this!

    When for some reason from module import * is needed, a workaround to namespace pollution is to define in module the variable __all__, which whitelists what should be imported with the star operator.

    HTH!

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

Sidebar

Related Questions

Simple question which I can't seem to find an answer of: I have two
I have a very simple question for which I can't seem to find an
I have another Richfaces question which may seem rather weird. I am developing a
i have another question on getting my XML serialization neat, which i can't seem
The question seems pretty well formulated I have a virtual machine which implements only
I have a question which I am sure is simple but I have slight
I have a question which is slightly similar to this question on stackoverflow std::cin.clear()
I have a rather fundamental question about XML here. When declaring an element's xmlns
I have asked this question earlier today but I didn't provide enough information and
This question is not a how do I but rather a why is this

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.