Performance and lightness of an application: import the whole library or just the necessary modules?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I've been asking myself questions for some time now. If I have to distribute an application that uses large libraries, or frameworks, can I afford to import the entire library or framework, and call the modules when I need them, for example:
import os
import nltk
import codecs
import json
import collections
import flask
#e.g.
json.load(json_data)
collection.Counter(aList)
nltk.word_tokenize(sentence)
codecs.open(aFile)
os.listdir(aPath)
or otherwise it is better to import only the necessary modules, in order to promote lightness and performance, for example:
from collections import Counter
from flask import request, Flask, render_template, redirect, url_for
from nltk.data import load
from nltk import word_tokenize
from nltk.corpus import stopwords.words as stopW
from os import listdir
It makes me very curious to know which is the best solution when we aim to distribute an application on any platform (whether desktop, mobile, or web based).
Thank you very much in advance.
python
add a comment |
I've been asking myself questions for some time now. If I have to distribute an application that uses large libraries, or frameworks, can I afford to import the entire library or framework, and call the modules when I need them, for example:
import os
import nltk
import codecs
import json
import collections
import flask
#e.g.
json.load(json_data)
collection.Counter(aList)
nltk.word_tokenize(sentence)
codecs.open(aFile)
os.listdir(aPath)
or otherwise it is better to import only the necessary modules, in order to promote lightness and performance, for example:
from collections import Counter
from flask import request, Flask, render_template, redirect, url_for
from nltk.data import load
from nltk import word_tokenize
from nltk.corpus import stopwords.words as stopW
from os import listdir
It makes me very curious to know which is the best solution when we aim to distribute an application on any platform (whether desktop, mobile, or web based).
Thank you very much in advance.
python
1
Performance-wise, usingimport x
versusfrom x import y
doesn't matter; in both cases, the entire modulex
needs to be imported. The only difference is whether symbols from the module are made available in your module's scope. It could make a difference to import only e.g.nltk.data
and not all ofnltk
, but this also depends on how that individual package is set up (i.e. it might still end up loading all its modules).
– Thomas
Nov 22 '18 at 11:26
Thank you for your answer @Thomas. Yes, I think most of the doubts are on nltk. For now I usednltk.corpus.stopwords.words()
,nltk.data.load()
,nltk.word_tokenize()
and maybe I will usenltk.stem.SnowballStemmer()
– Guglielmo Lischi
Nov 22 '18 at 13:32
add a comment |
I've been asking myself questions for some time now. If I have to distribute an application that uses large libraries, or frameworks, can I afford to import the entire library or framework, and call the modules when I need them, for example:
import os
import nltk
import codecs
import json
import collections
import flask
#e.g.
json.load(json_data)
collection.Counter(aList)
nltk.word_tokenize(sentence)
codecs.open(aFile)
os.listdir(aPath)
or otherwise it is better to import only the necessary modules, in order to promote lightness and performance, for example:
from collections import Counter
from flask import request, Flask, render_template, redirect, url_for
from nltk.data import load
from nltk import word_tokenize
from nltk.corpus import stopwords.words as stopW
from os import listdir
It makes me very curious to know which is the best solution when we aim to distribute an application on any platform (whether desktop, mobile, or web based).
Thank you very much in advance.
python
I've been asking myself questions for some time now. If I have to distribute an application that uses large libraries, or frameworks, can I afford to import the entire library or framework, and call the modules when I need them, for example:
import os
import nltk
import codecs
import json
import collections
import flask
#e.g.
json.load(json_data)
collection.Counter(aList)
nltk.word_tokenize(sentence)
codecs.open(aFile)
os.listdir(aPath)
or otherwise it is better to import only the necessary modules, in order to promote lightness and performance, for example:
from collections import Counter
from flask import request, Flask, render_template, redirect, url_for
from nltk.data import load
from nltk import word_tokenize
from nltk.corpus import stopwords.words as stopW
from os import listdir
It makes me very curious to know which is the best solution when we aim to distribute an application on any platform (whether desktop, mobile, or web based).
Thank you very much in advance.
python
python
edited Nov 22 '18 at 18:00
davidism
66.4k12184194
66.4k12184194
asked Nov 22 '18 at 11:24
Guglielmo LischiGuglielmo Lischi
30110
30110
1
Performance-wise, usingimport x
versusfrom x import y
doesn't matter; in both cases, the entire modulex
needs to be imported. The only difference is whether symbols from the module are made available in your module's scope. It could make a difference to import only e.g.nltk.data
and not all ofnltk
, but this also depends on how that individual package is set up (i.e. it might still end up loading all its modules).
– Thomas
Nov 22 '18 at 11:26
Thank you for your answer @Thomas. Yes, I think most of the doubts are on nltk. For now I usednltk.corpus.stopwords.words()
,nltk.data.load()
,nltk.word_tokenize()
and maybe I will usenltk.stem.SnowballStemmer()
– Guglielmo Lischi
Nov 22 '18 at 13:32
add a comment |
1
Performance-wise, usingimport x
versusfrom x import y
doesn't matter; in both cases, the entire modulex
needs to be imported. The only difference is whether symbols from the module are made available in your module's scope. It could make a difference to import only e.g.nltk.data
and not all ofnltk
, but this also depends on how that individual package is set up (i.e. it might still end up loading all its modules).
– Thomas
Nov 22 '18 at 11:26
Thank you for your answer @Thomas. Yes, I think most of the doubts are on nltk. For now I usednltk.corpus.stopwords.words()
,nltk.data.load()
,nltk.word_tokenize()
and maybe I will usenltk.stem.SnowballStemmer()
– Guglielmo Lischi
Nov 22 '18 at 13:32
1
1
Performance-wise, using
import x
versus from x import y
doesn't matter; in both cases, the entire module x
needs to be imported. The only difference is whether symbols from the module are made available in your module's scope. It could make a difference to import only e.g. nltk.data
and not all of nltk
, but this also depends on how that individual package is set up (i.e. it might still end up loading all its modules).– Thomas
Nov 22 '18 at 11:26
Performance-wise, using
import x
versus from x import y
doesn't matter; in both cases, the entire module x
needs to be imported. The only difference is whether symbols from the module are made available in your module's scope. It could make a difference to import only e.g. nltk.data
and not all of nltk
, but this also depends on how that individual package is set up (i.e. it might still end up loading all its modules).– Thomas
Nov 22 '18 at 11:26
Thank you for your answer @Thomas. Yes, I think most of the doubts are on nltk. For now I used
nltk.corpus.stopwords.words()
, nltk.data.load()
, nltk.word_tokenize()
and maybe I will use nltk.stem.SnowballStemmer()
– Guglielmo Lischi
Nov 22 '18 at 13:32
Thank you for your answer @Thomas. Yes, I think most of the doubts are on nltk. For now I used
nltk.corpus.stopwords.words()
, nltk.data.load()
, nltk.word_tokenize()
and maybe I will use nltk.stem.SnowballStemmer()
– Guglielmo Lischi
Nov 22 '18 at 13:32
add a comment |
1 Answer
1
active
oldest
votes
First wonder
It depends what you want to achieve. Take an example: If you have a big pile of code like 500 lines it's easier for readability if you import "module" for instance
import myscript
and 400 lines later when you need to use the function from myscript you need to write
myscript.somefunction()
now it's easier to read your code because you already know from where is this function come from.
Because in the second scenario if you import
from myscript import somefunction
you would write
somefunction()
Which is harder to diagnose from where this function came from if you have 5 and more imports like that
But it depends on you which style to choose
Another discuss about first wonder
Second wonder
In your example you show something like
from flask import request, Flask, render_template, redirect, url_for
and I would stick with that because of one big advantage, if you import the whole module like that
import flask
you could forget or by accident create function that already exists in the flask for instance:
def request():
pass
and this would overwrite your imported function and give you a nice headache because now this function returns nothing.
Performance
In both situation as Thomas said entire module needs to be imported so in both situations performance should be the same.
Summary
For libraries, I would choose to from module import function most of the times. Except for some libraries like numpy because I never saw this library imports in a different way than:
import numpy as np
And it's really convenient because it's easier to read your code for somebody else and vice versa.
But if I import many own scripts I would choose to
import myscript
because it's easier to understand from where this function comes from and function can have the same name in different scripts. But as I said it's my opinion and I think it's quite a convenient way. But if you would like to do it differently go ahead :)
Thank you very much for your answers. At this point I think that for the most inflated libraries (e.g. os, json, and codecs) I will not import specific modules. But I still have doubts about what import from nltk. I am currently usingnltk.corpus.stopwords.words()
nltk.data.load()
nltk.word_tokenize()
and maybe I will usenltk.stem.SnowballStemmer()
. I don't think that selecting the modules changes something.
– Guglielmo Lischi
Nov 22 '18 at 13:20
If I were you I would choose to import specific modules just to get a better understanding of what I'm using. Importing specific modules likefrom nltk.corpus import stopwords
forstopwords.words()
in the sake of clarity of importing modules and easier debugging.
– K. Klik
Nov 22 '18 at 14:00
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%2f53429898%2fperformance-and-lightness-of-an-application-import-the-whole-library-or-just-th%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
First wonder
It depends what you want to achieve. Take an example: If you have a big pile of code like 500 lines it's easier for readability if you import "module" for instance
import myscript
and 400 lines later when you need to use the function from myscript you need to write
myscript.somefunction()
now it's easier to read your code because you already know from where is this function come from.
Because in the second scenario if you import
from myscript import somefunction
you would write
somefunction()
Which is harder to diagnose from where this function came from if you have 5 and more imports like that
But it depends on you which style to choose
Another discuss about first wonder
Second wonder
In your example you show something like
from flask import request, Flask, render_template, redirect, url_for
and I would stick with that because of one big advantage, if you import the whole module like that
import flask
you could forget or by accident create function that already exists in the flask for instance:
def request():
pass
and this would overwrite your imported function and give you a nice headache because now this function returns nothing.
Performance
In both situation as Thomas said entire module needs to be imported so in both situations performance should be the same.
Summary
For libraries, I would choose to from module import function most of the times. Except for some libraries like numpy because I never saw this library imports in a different way than:
import numpy as np
And it's really convenient because it's easier to read your code for somebody else and vice versa.
But if I import many own scripts I would choose to
import myscript
because it's easier to understand from where this function comes from and function can have the same name in different scripts. But as I said it's my opinion and I think it's quite a convenient way. But if you would like to do it differently go ahead :)
Thank you very much for your answers. At this point I think that for the most inflated libraries (e.g. os, json, and codecs) I will not import specific modules. But I still have doubts about what import from nltk. I am currently usingnltk.corpus.stopwords.words()
nltk.data.load()
nltk.word_tokenize()
and maybe I will usenltk.stem.SnowballStemmer()
. I don't think that selecting the modules changes something.
– Guglielmo Lischi
Nov 22 '18 at 13:20
If I were you I would choose to import specific modules just to get a better understanding of what I'm using. Importing specific modules likefrom nltk.corpus import stopwords
forstopwords.words()
in the sake of clarity of importing modules and easier debugging.
– K. Klik
Nov 22 '18 at 14:00
add a comment |
First wonder
It depends what you want to achieve. Take an example: If you have a big pile of code like 500 lines it's easier for readability if you import "module" for instance
import myscript
and 400 lines later when you need to use the function from myscript you need to write
myscript.somefunction()
now it's easier to read your code because you already know from where is this function come from.
Because in the second scenario if you import
from myscript import somefunction
you would write
somefunction()
Which is harder to diagnose from where this function came from if you have 5 and more imports like that
But it depends on you which style to choose
Another discuss about first wonder
Second wonder
In your example you show something like
from flask import request, Flask, render_template, redirect, url_for
and I would stick with that because of one big advantage, if you import the whole module like that
import flask
you could forget or by accident create function that already exists in the flask for instance:
def request():
pass
and this would overwrite your imported function and give you a nice headache because now this function returns nothing.
Performance
In both situation as Thomas said entire module needs to be imported so in both situations performance should be the same.
Summary
For libraries, I would choose to from module import function most of the times. Except for some libraries like numpy because I never saw this library imports in a different way than:
import numpy as np
And it's really convenient because it's easier to read your code for somebody else and vice versa.
But if I import many own scripts I would choose to
import myscript
because it's easier to understand from where this function comes from and function can have the same name in different scripts. But as I said it's my opinion and I think it's quite a convenient way. But if you would like to do it differently go ahead :)
Thank you very much for your answers. At this point I think that for the most inflated libraries (e.g. os, json, and codecs) I will not import specific modules. But I still have doubts about what import from nltk. I am currently usingnltk.corpus.stopwords.words()
nltk.data.load()
nltk.word_tokenize()
and maybe I will usenltk.stem.SnowballStemmer()
. I don't think that selecting the modules changes something.
– Guglielmo Lischi
Nov 22 '18 at 13:20
If I were you I would choose to import specific modules just to get a better understanding of what I'm using. Importing specific modules likefrom nltk.corpus import stopwords
forstopwords.words()
in the sake of clarity of importing modules and easier debugging.
– K. Klik
Nov 22 '18 at 14:00
add a comment |
First wonder
It depends what you want to achieve. Take an example: If you have a big pile of code like 500 lines it's easier for readability if you import "module" for instance
import myscript
and 400 lines later when you need to use the function from myscript you need to write
myscript.somefunction()
now it's easier to read your code because you already know from where is this function come from.
Because in the second scenario if you import
from myscript import somefunction
you would write
somefunction()
Which is harder to diagnose from where this function came from if you have 5 and more imports like that
But it depends on you which style to choose
Another discuss about first wonder
Second wonder
In your example you show something like
from flask import request, Flask, render_template, redirect, url_for
and I would stick with that because of one big advantage, if you import the whole module like that
import flask
you could forget or by accident create function that already exists in the flask for instance:
def request():
pass
and this would overwrite your imported function and give you a nice headache because now this function returns nothing.
Performance
In both situation as Thomas said entire module needs to be imported so in both situations performance should be the same.
Summary
For libraries, I would choose to from module import function most of the times. Except for some libraries like numpy because I never saw this library imports in a different way than:
import numpy as np
And it's really convenient because it's easier to read your code for somebody else and vice versa.
But if I import many own scripts I would choose to
import myscript
because it's easier to understand from where this function comes from and function can have the same name in different scripts. But as I said it's my opinion and I think it's quite a convenient way. But if you would like to do it differently go ahead :)
First wonder
It depends what you want to achieve. Take an example: If you have a big pile of code like 500 lines it's easier for readability if you import "module" for instance
import myscript
and 400 lines later when you need to use the function from myscript you need to write
myscript.somefunction()
now it's easier to read your code because you already know from where is this function come from.
Because in the second scenario if you import
from myscript import somefunction
you would write
somefunction()
Which is harder to diagnose from where this function came from if you have 5 and more imports like that
But it depends on you which style to choose
Another discuss about first wonder
Second wonder
In your example you show something like
from flask import request, Flask, render_template, redirect, url_for
and I would stick with that because of one big advantage, if you import the whole module like that
import flask
you could forget or by accident create function that already exists in the flask for instance:
def request():
pass
and this would overwrite your imported function and give you a nice headache because now this function returns nothing.
Performance
In both situation as Thomas said entire module needs to be imported so in both situations performance should be the same.
Summary
For libraries, I would choose to from module import function most of the times. Except for some libraries like numpy because I never saw this library imports in a different way than:
import numpy as np
And it's really convenient because it's easier to read your code for somebody else and vice versa.
But if I import many own scripts I would choose to
import myscript
because it's easier to understand from where this function comes from and function can have the same name in different scripts. But as I said it's my opinion and I think it's quite a convenient way. But if you would like to do it differently go ahead :)
answered Nov 22 '18 at 12:11
K. KlikK. Klik
356
356
Thank you very much for your answers. At this point I think that for the most inflated libraries (e.g. os, json, and codecs) I will not import specific modules. But I still have doubts about what import from nltk. I am currently usingnltk.corpus.stopwords.words()
nltk.data.load()
nltk.word_tokenize()
and maybe I will usenltk.stem.SnowballStemmer()
. I don't think that selecting the modules changes something.
– Guglielmo Lischi
Nov 22 '18 at 13:20
If I were you I would choose to import specific modules just to get a better understanding of what I'm using. Importing specific modules likefrom nltk.corpus import stopwords
forstopwords.words()
in the sake of clarity of importing modules and easier debugging.
– K. Klik
Nov 22 '18 at 14:00
add a comment |
Thank you very much for your answers. At this point I think that for the most inflated libraries (e.g. os, json, and codecs) I will not import specific modules. But I still have doubts about what import from nltk. I am currently usingnltk.corpus.stopwords.words()
nltk.data.load()
nltk.word_tokenize()
and maybe I will usenltk.stem.SnowballStemmer()
. I don't think that selecting the modules changes something.
– Guglielmo Lischi
Nov 22 '18 at 13:20
If I were you I would choose to import specific modules just to get a better understanding of what I'm using. Importing specific modules likefrom nltk.corpus import stopwords
forstopwords.words()
in the sake of clarity of importing modules and easier debugging.
– K. Klik
Nov 22 '18 at 14:00
Thank you very much for your answers. At this point I think that for the most inflated libraries (e.g. os, json, and codecs) I will not import specific modules. But I still have doubts about what import from nltk. I am currently using
nltk.corpus.stopwords.words()
nltk.data.load()
nltk.word_tokenize()
and maybe I will use nltk.stem.SnowballStemmer()
. I don't think that selecting the modules changes something.– Guglielmo Lischi
Nov 22 '18 at 13:20
Thank you very much for your answers. At this point I think that for the most inflated libraries (e.g. os, json, and codecs) I will not import specific modules. But I still have doubts about what import from nltk. I am currently using
nltk.corpus.stopwords.words()
nltk.data.load()
nltk.word_tokenize()
and maybe I will use nltk.stem.SnowballStemmer()
. I don't think that selecting the modules changes something.– Guglielmo Lischi
Nov 22 '18 at 13:20
If I were you I would choose to import specific modules just to get a better understanding of what I'm using. Importing specific modules like
from nltk.corpus import stopwords
for stopwords.words()
in the sake of clarity of importing modules and easier debugging.– K. Klik
Nov 22 '18 at 14:00
If I were you I would choose to import specific modules just to get a better understanding of what I'm using. Importing specific modules like
from nltk.corpus import stopwords
for stopwords.words()
in the sake of clarity of importing modules and easier debugging.– K. Klik
Nov 22 '18 at 14:00
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.
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%2f53429898%2fperformance-and-lightness-of-an-application-import-the-whole-library-or-just-th%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
1
Performance-wise, using
import x
versusfrom x import y
doesn't matter; in both cases, the entire modulex
needs to be imported. The only difference is whether symbols from the module are made available in your module's scope. It could make a difference to import only e.g.nltk.data
and not all ofnltk
, but this also depends on how that individual package is set up (i.e. it might still end up loading all its modules).– Thomas
Nov 22 '18 at 11:26
Thank you for your answer @Thomas. Yes, I think most of the doubts are on nltk. For now I used
nltk.corpus.stopwords.words()
,nltk.data.load()
,nltk.word_tokenize()
and maybe I will usenltk.stem.SnowballStemmer()
– Guglielmo Lischi
Nov 22 '18 at 13:32