Python 3+ import package in a function call?
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 |
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 |
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 '18 at 8:21
SumNeuronSumNeuron
1,125824
1,125824
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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 |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 8:44
answered Nov 18 '18 at 8:37
quantquant
1,58211526
1,58211526
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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