Python 3+ import package in a function call?
up vote
2
down vote
favorite
Overtime I have built up a collection of utility functions for various things.
I would like to put them all in package, with a bit more structure than just a single file containing all the functions.
Some of these functions are written assuming certain packages have been imported e.g. I have several numpy
and pandas
utility functions that assume something like import numpy as np
Obviously I will not use this hypothetical package like from <pkg> import *
but I do not want to hinder performance either.
So if I have a numpy
utility function, should I add this to every function
# mypkg.np.utils
import sys
def np_util_fn(...):
if 'np' not in sys.modules: import numpy as np
# rest of func
or
# mypkg.np.utils
import sys
if 'np' not in sys.modules: import numpy as np
def np_util_fn(...):
# rest of func
which is more performant if I use a different part of this package? e.g. from pkg.other.utils import fn
python
add a comment |
up vote
2
down vote
favorite
Overtime I have built up a collection of utility functions for various things.
I would like to put them all in package, with a bit more structure than just a single file containing all the functions.
Some of these functions are written assuming certain packages have been imported e.g. I have several numpy
and pandas
utility functions that assume something like import numpy as np
Obviously I will not use this hypothetical package like from <pkg> import *
but I do not want to hinder performance either.
So if I have a numpy
utility function, should I add this to every function
# mypkg.np.utils
import sys
def np_util_fn(...):
if 'np' not in sys.modules: import numpy as np
# rest of func
or
# mypkg.np.utils
import sys
if 'np' not in sys.modules: import numpy as np
def np_util_fn(...):
# rest of func
which is more performant if I use a different part of this package? e.g. from pkg.other.utils import fn
python
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Overtime I have built up a collection of utility functions for various things.
I would like to put them all in package, with a bit more structure than just a single file containing all the functions.
Some of these functions are written assuming certain packages have been imported e.g. I have several numpy
and pandas
utility functions that assume something like import numpy as np
Obviously I will not use this hypothetical package like from <pkg> import *
but I do not want to hinder performance either.
So if I have a numpy
utility function, should I add this to every function
# mypkg.np.utils
import sys
def np_util_fn(...):
if 'np' not in sys.modules: import numpy as np
# rest of func
or
# mypkg.np.utils
import sys
if 'np' not in sys.modules: import numpy as np
def np_util_fn(...):
# rest of func
which is more performant if I use a different part of this package? e.g. from pkg.other.utils import fn
python
Overtime I have built up a collection of utility functions for various things.
I would like to put them all in package, with a bit more structure than just a single file containing all the functions.
Some of these functions are written assuming certain packages have been imported e.g. I have several numpy
and pandas
utility functions that assume something like import numpy as np
Obviously I will not use this hypothetical package like from <pkg> import *
but I do not want to hinder performance either.
So if I have a numpy
utility function, should I add this to every function
# mypkg.np.utils
import sys
def np_util_fn(...):
if 'np' not in sys.modules: import numpy as np
# rest of func
or
# mypkg.np.utils
import sys
if 'np' not in sys.modules: import numpy as np
def np_util_fn(...):
# rest of func
which is more performant if I use a different part of this package? e.g. from pkg.other.utils import fn
python
python
asked Nov 18 at 8:21
SumNeuron
1,075723
1,075723
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Ok, let's analyze your issue. Assume you have a file module.py
:
print("Module got imported")
and a file test.py
with:
import module
import module
. If you now execute test.py
you will get
Module got imported
. Please note that this line is not outputted two times. This means that python already checks whether a module was already imported (before reimporting it). So your check if 'np' not in sys.modules: import numpy as np
is not needed. This check only delays things as it may result in a double check.
In case you want to reimport a module you need reload(module)
. So if you have
import module
import module
reload(module)
in code.py
you will see the line Module got imported
two times.
This means that
import numpy as np
is sufficient. There is no need to check whether it already got imported via:
if 'np' not in sys.modules: import numpy as np
It depends whether it is advantageous to do import numpy as np
at the very beginning of your script or in a function. If the function is executed multiple times, it is advantageous to do so only at the very beginning. Otherwise you are rechecking whether 'np' is not in sys.modules all the time. In contrast if you can argue that your function is not called to often / is not necessarily executed in your program (e.g. because it depends on user input) then it may be advantageous (seen from the "point vu" of speed) to import this module in a function only.
I normally don't use any import
statements in functions as I always have the feeling that they blow up the function body and thus reduce readability.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Ok, let's analyze your issue. Assume you have a file module.py
:
print("Module got imported")
and a file test.py
with:
import module
import module
. If you now execute test.py
you will get
Module got imported
. Please note that this line is not outputted two times. This means that python already checks whether a module was already imported (before reimporting it). So your check if 'np' not in sys.modules: import numpy as np
is not needed. This check only delays things as it may result in a double check.
In case you want to reimport a module you need reload(module)
. So if you have
import module
import module
reload(module)
in code.py
you will see the line Module got imported
two times.
This means that
import numpy as np
is sufficient. There is no need to check whether it already got imported via:
if 'np' not in sys.modules: import numpy as np
It depends whether it is advantageous to do import numpy as np
at the very beginning of your script or in a function. If the function is executed multiple times, it is advantageous to do so only at the very beginning. Otherwise you are rechecking whether 'np' is not in sys.modules all the time. In contrast if you can argue that your function is not called to often / is not necessarily executed in your program (e.g. because it depends on user input) then it may be advantageous (seen from the "point vu" of speed) to import this module in a function only.
I normally don't use any import
statements in functions as I always have the feeling that they blow up the function body and thus reduce readability.
add a comment |
up vote
3
down vote
accepted
Ok, let's analyze your issue. Assume you have a file module.py
:
print("Module got imported")
and a file test.py
with:
import module
import module
. If you now execute test.py
you will get
Module got imported
. Please note that this line is not outputted two times. This means that python already checks whether a module was already imported (before reimporting it). So your check if 'np' not in sys.modules: import numpy as np
is not needed. This check only delays things as it may result in a double check.
In case you want to reimport a module you need reload(module)
. So if you have
import module
import module
reload(module)
in code.py
you will see the line Module got imported
two times.
This means that
import numpy as np
is sufficient. There is no need to check whether it already got imported via:
if 'np' not in sys.modules: import numpy as np
It depends whether it is advantageous to do import numpy as np
at the very beginning of your script or in a function. If the function is executed multiple times, it is advantageous to do so only at the very beginning. Otherwise you are rechecking whether 'np' is not in sys.modules all the time. In contrast if you can argue that your function is not called to often / is not necessarily executed in your program (e.g. because it depends on user input) then it may be advantageous (seen from the "point vu" of speed) to import this module in a function only.
I normally don't use any import
statements in functions as I always have the feeling that they blow up the function body and thus reduce readability.
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Ok, let's analyze your issue. Assume you have a file module.py
:
print("Module got imported")
and a file test.py
with:
import module
import module
. If you now execute test.py
you will get
Module got imported
. Please note that this line is not outputted two times. This means that python already checks whether a module was already imported (before reimporting it). So your check if 'np' not in sys.modules: import numpy as np
is not needed. This check only delays things as it may result in a double check.
In case you want to reimport a module you need reload(module)
. So if you have
import module
import module
reload(module)
in code.py
you will see the line Module got imported
two times.
This means that
import numpy as np
is sufficient. There is no need to check whether it already got imported via:
if 'np' not in sys.modules: import numpy as np
It depends whether it is advantageous to do import numpy as np
at the very beginning of your script or in a function. If the function is executed multiple times, it is advantageous to do so only at the very beginning. Otherwise you are rechecking whether 'np' is not in sys.modules all the time. In contrast if you can argue that your function is not called to often / is not necessarily executed in your program (e.g. because it depends on user input) then it may be advantageous (seen from the "point vu" of speed) to import this module in a function only.
I normally don't use any import
statements in functions as I always have the feeling that they blow up the function body and thus reduce readability.
Ok, let's analyze your issue. Assume you have a file module.py
:
print("Module got imported")
and a file test.py
with:
import module
import module
. If you now execute test.py
you will get
Module got imported
. Please note that this line is not outputted two times. This means that python already checks whether a module was already imported (before reimporting it). So your check if 'np' not in sys.modules: import numpy as np
is not needed. This check only delays things as it may result in a double check.
In case you want to reimport a module you need reload(module)
. So if you have
import module
import module
reload(module)
in code.py
you will see the line Module got imported
two times.
This means that
import numpy as np
is sufficient. There is no need to check whether it already got imported via:
if 'np' not in sys.modules: import numpy as np
It depends whether it is advantageous to do import numpy as np
at the very beginning of your script or in a function. If the function is executed multiple times, it is advantageous to do so only at the very beginning. Otherwise you are rechecking whether 'np' is not in sys.modules all the time. In contrast if you can argue that your function is not called to often / is not necessarily executed in your program (e.g. because it depends on user input) then it may be advantageous (seen from the "point vu" of speed) to import this module in a function only.
I normally don't use any import
statements in functions as I always have the feeling that they blow up the function body and thus reduce readability.
edited Nov 18 at 8:44
answered Nov 18 at 8:37
quant
1,54911526
1,54911526
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53359054%2fpython-3-import-package-in-a-function-call%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown