Python Forum
importing same python library in multiple custom module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
importing same python library in multiple custom module
#1
Hi All,

Scenario 01:

module:

import time


def printtime():
    print(time.time())
master program:

import module


module.printtime()

print(list(module.time.gmtime()))
Output will be:
Output:
1589049932.1866198 [2020, 5, 9, 19, 7, 41, 5, 130, 0]
Scenario 02:

module:

def printtime():
    print(time.time())
master program:

import time

import module


module.printtime()
Output will be:
Output:
NameError: name 'time' is not defined
I can import library in module and use it in master program but importing library in master program and use it in module functions won't work. I want to import python library in my master script and use it in python module functions directly. In other words, Scenario 02 to work.

I found a long way as below:

Scenario 03:

module:

def printtime(time):
    print(time.time())
master program:

import time

import module


module.printtime(time)
Output will be:
Output:
1589049932.1866198
But I want some more direct way like:

Scenario 04:

module:

def printtime():
    print(SOMETHING.time.time())
master program:

import time

import module


module.printtime()
Output will be:
Output:
1589049932.1866198
Is there any way? Please help me out.

Linux Mint
python3.6
IDLE3
Reply
#2
Python does not have include (which is what you are looking for). Each python module has to be valid. If scenario 2 was possible it would be really hard to use modules in python.
Reply
#3
What problem are you having that this causes (or why is something like the first example to be avoided)? Are you just wanting to avoid the include in the module file? You can have the includes in both the master and the module.
Reply
#4
(May-09-2020, 07:40 PM)deanhystad Wrote: Python does not have include (which is what you are looking for). Each python module has to be valid. If scenario 2 was possible it would be really hard to use modules in python.

Thank you for your quick response. Okay I do understand python modules should be valid by it's own. Thanks.

(May-09-2020, 08:55 PM)bowlofred Wrote: What problem are you having that this causes (or why is something like the first example to be avoided)? Are you just wanting to avoid the include in the module file? You can have the includes in both the master and the module.

Thank you for the reply. Actually this is a multimodule program and multiple modules needs some common fairly large libraries. I thought It may be memory (RAM) consuming so, I wanted to import those libraries in the master program once and use them in the module functions (please refer to Scenario 04, module, line 2). I've found an workaround as Scenario 03. But It's a bit longer process, that's it. Thanks.
Reply
#5
I don't think this is a concern. As a test I import PySide2 which is a fairly large package
import PySide2
input()
When I run this in Windows the task manager reports memory usage is 15.5 MB. I imported a second file that also imports PySide2
test.py

import PySide2
import test2.py
input()

test2.py
import PySide2
x = 2
With two modules importing PySide2 the memory usage is still 15.5 MB.
Reply
#6
(May-10-2020, 06:42 AM)escape_freedom13 Wrote: 've found an workaround as Scenario 03. But It's a bit longer process, that's it.
Absolutely no need of such "workarounds"

Each module is imported only once, unless you explicitly reload it.
You can see:
module1.py
print('Importing module1')
module2.py
import module1
module3.py
import module1
module4.py
import module2
import module3
now if you run module 4, it will print Importing module1 only once.

You need to read

The import system

and particularly The module cache:
Quote:The first place checked during import search is sys.modules. This mapping serves as a cache of all modules that have been previously imported, including the intermediate paths. So if foo.bar.baz was previously imported, sys.modules will contain entries for foo, foo.bar, and foo.bar.baz. Each key will have as its value the corresponding module object.

During import, the module name is looked up in sys.modules and if present, the associated value is the module satisfying the import, and the process completes.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(May-10-2020, 03:53 PM)deanhystad Wrote: I don't think this is a concern. As a test I import PySide2 which is a fairly large package

Thank you bro. This nice test is very helpful for me to understand.

(May-10-2020, 04:22 PM)buran Wrote: Absolutely no need of such "workarounds" as in scenario 03

Thank you very much bro. I've red the module cache part and will read the import statement thoroughly. But, you have explained very nicely with example that modules are imported once even if it's imported in multiple module. Thanks again and I'm going to mark it as solved, so others can search and visit this valuable discussion.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  importing variables from module 8376459 1 245 Feb-18-2024, 02:24 PM
Last Post: deanhystad
  no module named 'docx' when importing docx MaartenRo 1 710 Dec-31-2023, 11:21 AM
Last Post: deanhystad
  python convert multiple files to multiple lists MCL169 6 1,432 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  I want to create custom charts in Python. js1152410 1 501 Nov-13-2023, 05:45 PM
Last Post: gulshan212
  python standard way of importing library mg24 1 872 Nov-15-2022, 01:41 AM
Last Post: deanhystad
  Problem with importing python-telegram library into the project gandonio 1 1,515 Nov-01-2022, 02:19 AM
Last Post: deanhystad
  My code displays too much output when importing class from a module lil_e 4 1,102 Oct-22-2022, 12:56 AM
Last Post: Larz60+
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 5 1,493 Aug-28-2022, 07:11 AM
Last Post: Melcu54
Question How to move a class to a custom module? python300 4 1,511 Mar-08-2022, 09:19 PM
Last Post: python300
  Importing module in jupyter Noteboook ajitnayak1987 0 1,725 Jun-04-2021, 12:26 PM
Last Post: ajitnayak1987

Forum Jump:

User Panel Messages

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