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
  Puzzling import issue that I have no idea how to solvr starseeker 3 432 Feb-21-2024, 05:07 PM
Last Post: deanhystad
  Different Ways to Import Modules RockBlok 2 465 Dec-11-2023, 04:29 PM
Last Post: deanhystad
  How secure is the use of "secret" import? ejwjohn 12 1,154 Nov-09-2023, 08:31 AM
Last Post: ejwjohn
  is import cointegration_analysis a recognized module mitcht33 1 385 Nov-06-2023, 09:29 PM
Last Post: deanhystad
  problem in import module from other folder akbarza 5 1,260 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  can not import anaconda pandas module. PySpark pandas module is imported!! aupres 0 680 Aug-06-2023, 01:09 AM
Last Post: aupres
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 6,166 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  I try to import Vpython and I get a "ZeroDivisionError" Jon2222 16 2,469 Jul-26-2023, 07:37 AM
Last Post: Jon2222
  [WORKED AROUND] Problem installing elitech-datareader, 'cannot import build_py_2to3' NeilUK 4 1,564 Jul-09-2023, 10:01 AM
Last Post: NeilUK
  [solved] What is the best/pytonic way to import with dependency ? SpongeB0B 2 640 Jul-09-2023, 05:05 AM
Last Post: SpongeB0B

Forum Jump:

User Panel Messages

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