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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:51:27+00:00 2026-05-12T20:51:27+00:00

Out of the various ways to import code, are there some ways that are

  • 0

Out of the various ways to import code, are there some ways that are preferable to use, compared to others? This link http://effbot.org/zone/import-confusion.htm in short
states that

from foo.bar import MyClass

is not the preferred way to import MyClass under normal circumstances or unless you know what you are doing. Rather, a better way would like:

import foo.bar as foobaralias

and then in the code, to access MyClass use

foobaralias.MyClass

In short, it seems that the above-referenced link is saying it is usually better to import everything from a module, rather than just parts of the module.

However, that article I linked is really old.

I’ve also heard that it is better, at least in the context of Django projects, to instead only import the classes you want to use, rather than the whole module. It has been said that this form helps avoid circular import errors or at least makes the django import system less fragile. It was pointed out that Django’s own code seems to prefer "from x import y" over "import x".

Assuming the project I am working on doesn’t use any special features of __init__.py … (all of our __init__.py files are empty), what import method should I favor, and why?

  • 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-12T20:51:27+00:00Added an answer on May 12, 2026 at 8:51 pm

    For me, it’s dependent on the situation. If it’s a uniquely named method/class (i.e., not process() or something like that), and you’re going to use it a lot, then save typing and just do from foo import MyClass.

    If you’re importing multiple things from one module, it’s probably better to just import the module, and do module.bar, module.foo, module.baz, etc., to keep the namespace clean.

    You also said

    It has been said that this form helps avoid circular import errors or at least makes the django import system less fragile. It was pointed out that Django’s own code seems to prefer “from x import y” over “import x”.

    I don’t see how one way or the other would help prevent circular imports. The reason is that even when you do from x import y, ALL of x is imported. Only y is brought into the current namespace, but the entire module x is processed. Try out this example:

    In test.py, put the following:

    def a():
        print "a"
    
    print "hi"
    
    def b():
        print "b"
    
    print "bye"
    

    Then in ‘runme.py’, put:

    from test import b
    
    b()
    

    Then just do python runme.py

    You’ll see the following output:

    hi
    bye
    b
    

    So everything in test.py was run, even though you only imported b

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

Sidebar

Related Questions

I have been browsing SO and various other sites to find out whether this
Out of curiousity, is there any way to edit an existing synonym? That is,
There are various ways to handle session timeouts, like meta refreshes javascript on load
I've been trying out various ways of finding the last row in a range
What are the various ways through which I can find out the source from
You can compile Python in various ways. I'd like to find out with which
Please lay out the various ways for a .NET application to become a JMS
I'm trying various ways to implement pretty graphs in my app. I followed http://pullmonkey.com/projects/open_flash_chart2
I know this has been asked alot and in various ways, and I have
Please check out http://jsbin.com/omuqo for a demo of this issue. When you open a

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.