Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to omit module prefix?

Tags:

python

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()?

like image 348
Adam Lee Avatar asked Jul 05 '17 09:07

Adam Lee


People also ask

How do you stop a Python module?

One of the most appropriate functions that we can use to exit from a Python program is the sys. exit() function. This function is available in the sys module and when called it raises the SystemException in Python that then triggers the interpreter to stop further execution of the current python script.

Does Python support aliases for module names?

Aliasing ModulesIt is possible to modify the names of modules and their functions within Python by using the as keyword.


1 Answers

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
like image 143
Right leg Avatar answered Oct 12 '22 02:10

Right leg