13

I am very new to python, I have a simple question.

For example

If I do

import A

then I can use A.b(). I am wondering, how to omit A in A.b()?

Right leg
  • 16,080
  • 7
  • 48
  • 81
Adam Lee
  • 24,710
  • 51
  • 156
  • 236

3 Answers3

25

If you want to use b from module A:

from A import b

If you want to rename it:

from A import b as my_b

If you need several objects:

from A import b, c, d

If you need everything:

from A import *

About the import * option, please do read this post. It's rather short, but tl;dr: do not import everything from a module by from A import *. If you really need everything from the module, or a very big part of its content, prefer the normal import, possibly with renaming:

import numpy as np
import tkinter as tk
Right leg
  • 16,080
  • 7
  • 48
  • 81
  • 1
    I wouldn't say that you absolutely shouldn't use `from A import *`. It's just that you should be careful when you do so. For example, it was recommended to me in a Tk tutorial that I use `from tkinter import *; from tkinter import ttk` for themed widgets, as `tk` and `ttk` are so easy to confuse. I almost always do so when using `tkinter`. (I only didn't put the two imports on separate lines because I don't know how to do so in a comment). – Isaac Saffold Jul 05 '17 at 09:58
  • 1
    @IsaacSaffold That is rather true, but the care needed is quite painful. Anyway, the post I linked covers this way better than I could. – Right leg Jul 05 '17 at 10:00
  • 8
    Once you do `from os import *` and spend hours debugging because your calls to `open` don't work anymore you'll never use wildcard imports again. – Matthias Jul 05 '17 at 10:12
  • 1
    @IsaacSaffold importing all is like [`using namespace std` in C++](https://stackoverflow.com/q/1452721/995714) – phuclv Jul 05 '17 at 13:46
  • @Matthias I never use wildcard imports for modules like `os` for that very reason. As I said, I am cautious. – Isaac Saffold Jul 05 '17 at 22:31
8

Since A is a module, you can do:

from A import b 

b()
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
3

This question has already been answered, but one thing to note when using imports is that

from module import foo

and

import module

Are absolutely the same, as far as memory and performance is concerned. In either case, the module is compiled and loaded into sys.modules. The only difference is what is brought into your namespace. In the first case, it is foo. In the second case, it is module.

Since the other answers do not explain why using from module import * is bad, this is the reason: * will import everything into your namespace for direct use. This is not a good idea unless you know what you are doing. Name clashes are definitely possible, especially when importing multiple large modules.

This may be a matter of opinion, but I believe import module followed by module.foo() is safer and gives the reader a better idea about that method/attribute, where it came from, and what it is doing. Readability matters!

cs95
  • 379,657
  • 97
  • 704
  • 746