bladexeon
bladexeon

Reputation: 706

Import function from file without child imports

I've created a python module of functions that I have developed. Within this module there are several imports, some of these are not native to python and needed to be installed.

I have one instance where i need a python script to access a function in this module, but i dont want it to try and use all of the other imports that are already in the module. I've created a very basic example of what this setup looks like below.

for example:

#this is the module, named MOD.py
import win32con
def func1():
    data = win32con.function()
    return data
def func2():
    return do_action()


#this is the exterior script
from MOD import func2
data = func2()

Why is it that it will still attempt to import the win32con module within MOD.py even though func2 does not use it? Naturally if the module isn't installed I'll get an ImportError on the win32con line. I don't want to have to install these modules on machines every time i want to run code that does not even use it.

Upvotes: 1

Views: 570

Answers (1)

AllenMoh
AllenMoh

Reputation: 476

If the import is only used in func1, you could import it within func1:

#this is the module, named MOD.py
def func1():
    import win32con
    data = win32con.function()
    return data
def func2():
    return do_action()

Upvotes: 2

Related Questions