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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:13:01+00:00 2026-05-17T17:13:01+00:00

I just thought I understood how importing and modules work but obviously I need

  • 0

I just “thought” I understood how importing and modules work but obviously I need more schooling.

Here is an example program (just a test case of somerthing I’m doing that is much bigger in scope and scale) and a module:

quick.py

import gtk
from quick_window import *

w.show_all()
gtk.main()

quick_window.py

w = gtk.Window()
w.connect('destroy', lambda w: gtk.main_quit())
l=gtk.Label('Hello')
w.add(l)

running I get

$ python quick.py
Traceback (most recent call last):
  File "quick.py", line 2, in <module>
    from quick_window import *
  File "/home/woodnt/Dropbox/python/weather_project/plugins/quick_window.py", line 3, in <module>
    w = gtk.Window()
NameError: name 'gtk' is not defined

To get it to work, I have to also import (er, reimport) gtk in the module like so:

import gtk

w = gtk.Window()
w.connect('destroy', lambda w: gtk.main_quit())
l=gtk.Label('Hello')
w.add(l)

Why should I have to import gtk more than once? Does that mean that I have 2 “gtk’s” in memory?

Do I have to import everything within each module that I need within that module?

I know each module has it’s own namespace, but I thought it also inherited the “globals” including imported module from the calling program.

I had been under the impression the from module import * is like cutting and pasting the code right into that location. Is there another way to do that?

Help is greatly appreciated.

Narnie

  • 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-17T17:13:01+00:00Added an answer on May 17, 2026 at 5:13 pm

    The details of importing get very complicated, but conceptually it is very simple.

    When you write:

    import some_module
    

    It is equivalent to this:

    some_module = import_module("some_module")
    

    where import_module is kind of like:

    def import_module(modname):
        if modname in sys.modules:
            module = sys.modules[modname]
        else:
            filename = find_file_for_module(modname)
            python_code = open(filename).read()
            module = create_module_from_code(python_code)
            sys.modules[modname] = module
        return module
    

    Two things to note here: the assignment of some_module is specific: an import statement really does nothing in the current module except assign a module object to the name you specify. That’s why you don’t automatically get the names from the imported module, and why it isn’t like copying the imported code into the current module.

    Also, in the import_module pseudo-function, if the name has already been imported somewhere, it will be in the global list of all modules (sys.modules), and so will simply be reused. The file won’t be opened again, it won’t be executed again, the globals in that modules won’t get new values again, and so on. Importing the same module into many places is not wasteful or extra work, it is very fast.

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

Sidebar

Related Questions

I thought that layout is just a widget that keeps more widgets inside. But
I always thought about this but never understood why. Simple example: public IEnumerator<Effect> GetEnumerator
Just beginning to learn about structs, I thought I understood how they work, using
I thought I had this figured out but it turns out I'm just deleting
Probably I'm just too dump for googling, but I always thought char arrays get
Alright, I thought I understood generics pretty well, but for some reason I can't
Just when I thought I've understood this topic completely, I'm back to basics. I
I may not understand what the return statement does(I thought it just returned a
I just thought about the non-blocking infrastructure of tornado and event-driven programming. Actually I'm
I probably just haven't thought this through, or perhaps I'm simply unaware of an

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.