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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:55:18+00:00 2026-05-11T18:55:18+00:00

I have something like this: # a.py import os class A: … # b.py

  • 0

I have something like this:

 # a.py  
 import os
 class A:
   ...

 # b.py
 import a
 class B(A):
   ...

In class B (b.py) I’d like to be able to use the modules imported in a.py (os in this case). Is it possible to achieve this behavior in Python or should I import the modules in both files?

Edit: I’m not worried about the import times, my problem is the visual clutter that the block of imports puts on the files. I end up having stuff like this in every controller (RequestHandler):

 from django.utils import simplejson
 from google.appengine.ext import webapp
 from google.appengine.ext.webapp import template
 from google.appengine.ext import db

That’s what I’d like to avoid.

  • 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-11T18:55:18+00:00Added an answer on May 11, 2026 at 6:55 pm

    Yes you can use the imports from the other file by going a.os.

    However, the pythonic way is to just import the exact modules you need without making a chain out of it (which can lead to circular references).

    When you import a module, the code is compiled and inserted into a dictionary of names -> module objects. The dictionary is located at sys.modules.

    import sys
    sys.modules
    
    >>> pprint.pprint(sys.modules)
    {'UserDict': <module 'UserDict' from 'C:\python26\lib\UserDict.pyc'>,
     '__builtin__': <module '__builtin__' (built-in)>,
     '__main__': <module '__main__' (built-in)>,
     '_abcoll': <module '_abcoll' from 'C:\python26\lib\_abcoll.pyc'>,
    # the rest omitted for brevity
    

    When you try to import the module again, Python will check the dictionary to see if its already there. If it is, it will return the already compiled module object to you. Otherwise, it will compile the code, and insert it in sys.modules.

    Since dictionaries are implemented as hash tables, this lookup is very quick and takes up negligible time compared to the risk of creating circular references.

    Edit: I’m not worried about the import
    times, my problem is the visual
    clutter that the block of imports puts
    on the files.

    If you only have about 4 or 5 imports like that, its not too cluttery. Remember, “Explicit is better than implicit”. However if it really bothers you that much, do this:

    <importheaders.py>
    from django.utils import simplejson
    from google.appengine.ext import webapp
    from google.appengine.ext.webapp import template
    from google.appengine.ext import db
    
    
    <mycontroller.py>
    from importheaders import *
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 172k
  • Answers 172k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Check out this project http://code.google.com/p/chibiorm/ It's an Object-Relational Mapping (ORM)… May 12, 2026 at 2:31 pm
  • Editorial Team
    Editorial Team added an answer scala> class X(i : Int){ def method(y : Int) =… May 12, 2026 at 2:31 pm
  • Editorial Team
    Editorial Team added an answer str_replace()? May 12, 2026 at 2:31 pm

Related Questions

I have something like this: # a.py import os class A: ... # b.py
I am copying code from website matplotlib and pasting into the Vim editor in
Is there a simple way to run a Python script on Windows/Linux/OS X? On
I have some code that throws causes syncdb to throw an error (because it
Alright, this is probably a really silly question but I am new to Python/Django

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.