Python Forum
Different Ways to Import Modules
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Different Ways to Import Modules
#1
Good morning,

I'm not entirely sure I understand the reason to use the asterix when importing modules, or what its significance might be, or why you would use it.

For example, I think doing something like this would import the entire module

import math

x = math.pi

print(x)
and I think doing something like this would import the specific function from the module?

from math import pi as why

x = why

print(x)
My question is, I don't understand the asterix when its used like this

from math import *

x = pi

print(x)
Reply
#2
It's a wildcard import. In the example, it will import all the math functions and classes. It is also bad practice to do wildcard imports.
buran and RockBlok like this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
And you should never use wildcard imports. Wildcard imports make it more difficult to know what names are imported from which modules, silently import names that might collide with variables/functions in your program or other modules imported using a wildcard
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Import Modules TheBunyip 4 1,253 Mar-10-2023, 04:47 PM
Last Post: buran
  a bunch of modules to import Skaperen 2 902 Nov-07-2022, 07:33 PM
Last Post: Gribouillis
  2 ways to initialize a list with all zero quazirfan 2 1,819 Aug-06-2021, 03:27 PM
Last Post: deanhystad
  Did interpreter 'compile' all import modules(from thrid-party) jamesyuan 10 4,331 Oct-19-2020, 10:49 AM
Last Post: jamesyuan
  Import all items from all modules from package DD169 3 2,124 Jan-25-2020, 12:33 PM
Last Post: buran
  Import Python Modules zowhair 4 2,830 Jun-27-2019, 06:30 AM
Last Post: Gribouillis
  What area unit the opposite ways in which to understand if an inventory in Python is Smhbugra 2 2,494 May-27-2019, 07:55 PM
Last Post: Larz60+
  What are ways of pointing cross-compiled origin source line for python? wyvogew 2 2,835 Feb-02-2019, 03:16 PM
Last Post: wyvogew
  [Jupyter] import notebooks as modules Sirduke1 1 2,679 Aug-20-2018, 08:15 PM
Last Post: perfringo
  Python3: problem to import personals modules PyForIO 2 3,508 Mar-04-2018, 12:42 PM
Last Post: PyForIO

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020