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
Download my project scripts


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
  Confused by the different ways of extracting data in DataFrame leea2024 1 1,305 Aug-17-2024, 01:34 PM
Last Post: deanhystad
Bug Python 3.12 cannot import local python files as modules sunflowerdog 25 47,285 Jul-31-2024, 01:52 PM
Last Post: sunflowerdog
  Import Modules TheBunyip 4 2,889 Mar-10-2023, 04:47 PM
Last Post: buran
  a bunch of modules to import Skaperen 2 2,058 Nov-07-2022, 07:33 PM
Last Post: Gribouillis
  2 ways to initialize a list with all zero quazirfan 2 3,520 Aug-06-2021, 03:27 PM
Last Post: deanhystad
  Did interpreter 'compile' all import modules(from thrid-party) jamesyuan 10 8,823 Oct-19-2020, 10:49 AM
Last Post: jamesyuan
  Import all items from all modules from package DD169 3 3,645 Jan-25-2020, 12:33 PM
Last Post: buran
  Import Python Modules zowhair 4 4,396 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 3,672 May-27-2019, 07:55 PM
Last Post: Larz60+
  What are ways of pointing cross-compiled origin source line for python? wyvogew 2 4,133 Feb-02-2019, 03:16 PM
Last Post: wyvogew

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.