Python Forum
import vs. from module import *
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
import vs. from module import *
#1
I defined my own module <folder1>/<module1> and inside I <module1>, defined <function1>.

Now I import using import statement and call function1, it wont work.
But built-in package/lib import works.

import <module1>
import os
....
os.system()
call <function1()> <----- fails here
If I do following, then it works:

from <module1> import *
call <function1()>

What are difference between these statement such that import does not work for user-defined lib.

looks like i figured out during the post. :)
Reply
#2
Quote:import <module1>
import os
....
os.system()
call <function1()> <----- fails here
you need to do
<module1>.<function1()>
see this tutorial for more info
http://python-forum.io/Thread-Basic-Modules-part-1
you should also avoid star imports
http://python-forum.io/Thread-Namespace-...th-imports
Recommended Tutorials:
Reply
#3
One uses namespacing, and the other pollutes the current namespace. "from _x_ import *" shouldn't ever be used. I'd love to hear some arguments for why people like it, but I'll still probably disagree with you :p
Reply
#4
From David Beazley:

Quote:import always executes the entire file

http://www.dabeaz.com/modulepackage/ModulePackage.pdf

page 22

Not saying I agree, but it's hard not to agree with the Guru
Reply
#5
(Dec-01-2016, 12:52 AM)Larz60+ Wrote: From David Beazley:

Quote:import always executes the entire file

http://www.dabeaz.com/modulepackage/ModulePackage.pdf

page 22

Not saying I agree, but it's hard not to agree with the Guru

I've been reading through it, and by far the most interesting things are the __main__.py files, and python zip executables (https://www.python.org/dev/peps/pep-0441/).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm trying to import a dataset into a Jupyter Notebook Hisilat03 0 700 Mar-24-2025, 09:26 AM
Last Post: Hisilat03
  beginner doesn't understand import cimerio 3 467 Feb-12-2025, 05:02 PM
Last Post: cimerio
  Python: How to import data from txt, instead of running the data from the code? Melcu54 1 600 Dec-13-2024, 06:50 AM
Last Post: Gribouillis
  python 3.13 : import whois HansieB 1 610 Nov-30-2024, 02:58 PM
Last Post: snippsat
Question [SOLVED] Why "import foo" + "from foo import bar"? Winfried 4 1,218 Nov-26-2024, 08:13 AM
Last Post: Winfried
  import data (.csv) into Jupyter notebook oranstprotonme 2 1,145 Aug-14-2024, 07:08 PM
Last Post: oranstprotonme
  'import requests' problem Curbie 2 755 Aug-14-2024, 01:04 AM
Last Post: Curbie
  import a function from another file using relative path paul18fr 6 2,722 Aug-01-2024, 06:40 AM
Last Post: paul18fr
Bug Python 3.12 cannot import local python files as modules sunflowerdog 25 17,412 Jul-31-2024, 01:52 PM
Last Post: sunflowerdog
  ImportError: cannot import name 'scipy_namespace_for' from 'scipy._lib._array_api' AgileAVS 0 1,699 Jul-15-2024, 08:45 AM
Last Post: AgileAVS

Forum Jump:

User Panel Messages

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