Python Forum
to import just part of a modulr
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
to import just part of a modulr
#2
Can use from my_module import d
my_module.py:
a = 100
b = 200
c = 300
d = 400
bar.py:
from my_module import d

print(d)
Output:
400
So now have access to only d in bar.py.
A module is always fully imported,so doing import my_module and from my_module import d make no difference in bottom.
So sys.modules['my_module'] has reference to whole file.
>>> import sys
>>> sys.modules['my_module']
<module 'my_module' from 'E:\\div_code\\new\\my_module.py'> 
from my_module import d point straight to d.
But whether you use anything else from the module or not,it's always fully imported.

import my_module binds the name my_module to sys.modules['my_module'].
Then can access all other names from it.
>>> import my_module

>>> my_module.a
100
>>> my_module.b
200
>>> my_module.d
400
Reply


Messages In This Thread
to import just part of a modulr - by Skaperen - Oct-10-2018, 05:18 AM
RE: to import just part of a modulr - by snippsat - Oct-10-2018, 08:57 AM
RE: to import just part of a modulr - by nilamo - Oct-10-2018, 04:08 PM
RE: to import just part of a modulr - by Skaperen - Oct-11-2018, 12:52 AM
RE: to import just part of a modulr - by Larz60+ - Oct-11-2018, 02:58 AM
RE: to import just part of a modulr - by ichabod801 - Oct-11-2018, 03:25 AM
RE: to import just part of a modulr - by Skaperen - Oct-13-2018, 05:21 AM
RE: to import just part of a modulr - by Larz60+ - Oct-13-2018, 11:58 AM
RE: to import just part of a modulr - by Skaperen - Oct-13-2018, 08:22 PM
RE: to import just part of a modulr - by nilamo - Oct-14-2018, 04:21 AM
RE: to import just part of a modulr - by wavic - Oct-14-2018, 04:25 AM
RE: to import just part of a modulr - by Skaperen - Oct-15-2018, 02:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Can you please explain what the part after import sys is doing? Pedroski55 3 2,146 Dec-09-2020, 07:02 AM
Last Post: bowlofred
  Import error when trying to import DDE (part of PyWin32) fbicalho 0 3,863 Apr-21-2018, 07:26 PM
Last Post: fbicalho

Forum Jump:

User Panel Messages

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