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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:52:26+00:00 2026-05-15T15:52:26+00:00

In python, if you need a module from a different package you have to

  • 0

In python, if you need a module from a different package you have to import it. Coming from a Java background, that makes sense.

import foo.bar

What doesn’t make sense though, is why do I need to use the full name whenever I want to use bar? If I wanted to use the full name, why do I need to import? Doesn’t using the full name immediately describe which module I’m addressing?

It just seems a little redundant to have from foo import bar when that’s what import foo.bar should be doing. Also a little vague why I had to import when I was going to use the full name.

  • 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-15T15:52:27+00:00Added an answer on May 15, 2026 at 3:52 pm

    The thing is, even though Python’s import statement is designed to look similar to Java’s, they do completely different things under the hood. As you know, in Java an import statement is really little more than a hint to the compiler. It basically sets up an alias for a fully qualified class name. For example, when you write

    import java.util.Set;
    

    it tells the compiler that throughout that file, when you write Set, you mean java.util.Set. And if you write s.add(o) where s is an object of type Set, the compiler (or rather, linker) goes out and finds the add method in Set.class and puts in a reference to it.

    But in Python,

    import util.set
    

    (that is a made-up module, by the way) does something completely different. See, in Python, packages and modules are not just names, they’re actual objects, and when you write util.set in your code, that instructs Python to access an object named util and look for an attribute on it named set. The job of Python’s import statement is to create that object and attribute. The way it works is that the interpreter looks for a file named util/__init__.py, uses the code in it to define properties of an object, and binds that object to the name util. Similarly, the code in util/set.py is used to initialize an object which is bound to util.set. There’s a function called __import__ which takes care of all of this, and in fact the statement import util.set is basically equivalent to

    util = __import__('util.set')
    

    The point is, when you import a Python module, what you get is an object corresponding to the top-level package, util. In order to get access to util.set you need to go through that, and that’s why it seems like you need to use fully qualified names in Python.

    There are ways to get around this, of course. Since all these things are objects, one simple approach is to just bind util.set to a simpler name, i.e. after the import statement, you can have

    set = util.set
    

    and from that point on you can just use set where you otherwise would have written util.set. (Of course this obscures the built-in set class, so I don’t recommend actually using the name set.) Or, as mentioned in at least one other answer, you could write

    from util import set
    

    or

    import util.set as set
    

    This still imports the package util with the module set in it, but instead of creating a variable util in the current scope, it creates a variable set that refers to util.set. Behind the scenes, this works kind of like

    _util = __import__('util', fromlist='set')
    set = _util.set
    del _util
    

    in the former case, or

    _util = __import__('util.set')
    set = _util.set
    del _util
    

    in the latter (although both ways do essentially the same thing). This form is semantically more like what Java’s import statement does: it defines an alias (set) to something that would ordinarily only be accessible by a fully qualified name (util.set).

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

Sidebar

Related Questions

I need to import the multiprocessing module in Python 2.5. I've followed the instructions
I'm studying C++ right now, coming from a background in Python, and I'm having
How can a Python program easily import a Python module from a file with
I need a Python library that supports PEM files and both RSA signing and
I am newbie in Python and I need help: I have a file with
I need to fit some points from different datasets with straight lines. From every
I have two tab delimited csv files (with headers) that I need to merge
A number of people in my organization have different email names from perforce names,
I wonder if it is possible to create an executable module from a Python
I'm wondering if there exists a python module that would allow me to do

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.