How can I use the same imports from a different script?
I have script1.py
which has
from itertools import izip_longest, islice
and somewhere in script1.py
, islice
is used.
I have another script, script2.py
, which needs to be automatically executed within script1.py
because script1
's output file is script2
's input file. Problem is, script2
also needs to use the islice
. Is there a way to use it without importing islice
again in script2
?
python itertools
add a comment |
I have script1.py
which has
from itertools import izip_longest, islice
and somewhere in script1.py
, islice
is used.
I have another script, script2.py
, which needs to be automatically executed within script1.py
because script1
's output file is script2
's input file. Problem is, script2
also needs to use the islice
. Is there a way to use it without importing islice
again in script2
?
python itertools
1
Why do you not want to import it twice? As a reader, if I see someone import or use islice from script1, I'll assume that's a different function from the one in itertools. This just makes your code confusing, and introduces an unnecessary dependency on script1 from script2, that there must be that itertools import line.
– Melvin
Nov 15 '18 at 4:48
1
Actual imports only happen once. Successive imports do not reload anything, just fetch the reference.
– Mad Physicist
Nov 15 '18 at 4:49
@MadPhysicist it's because my professor told me to not import the same thing twice. Can you please elaborate on what you mean by not reloading anything so I can tell it to him
– sneedshelp
Nov 15 '18 at 4:52
1
@sneedshelp. Your professor seems to be missing some key information, to put it mildly. I'll post an answer momentarily (or find a duplicate if I can).
– Mad Physicist
Nov 15 '18 at 12:27
add a comment |
I have script1.py
which has
from itertools import izip_longest, islice
and somewhere in script1.py
, islice
is used.
I have another script, script2.py
, which needs to be automatically executed within script1.py
because script1
's output file is script2
's input file. Problem is, script2
also needs to use the islice
. Is there a way to use it without importing islice
again in script2
?
python itertools
I have script1.py
which has
from itertools import izip_longest, islice
and somewhere in script1.py
, islice
is used.
I have another script, script2.py
, which needs to be automatically executed within script1.py
because script1
's output file is script2
's input file. Problem is, script2
also needs to use the islice
. Is there a way to use it without importing islice
again in script2
?
python itertools
python itertools
edited Nov 15 '18 at 13:12
Mad Physicist
38k1674107
38k1674107
asked Nov 15 '18 at 4:40
sneedshelpsneedshelp
14
14
1
Why do you not want to import it twice? As a reader, if I see someone import or use islice from script1, I'll assume that's a different function from the one in itertools. This just makes your code confusing, and introduces an unnecessary dependency on script1 from script2, that there must be that itertools import line.
– Melvin
Nov 15 '18 at 4:48
1
Actual imports only happen once. Successive imports do not reload anything, just fetch the reference.
– Mad Physicist
Nov 15 '18 at 4:49
@MadPhysicist it's because my professor told me to not import the same thing twice. Can you please elaborate on what you mean by not reloading anything so I can tell it to him
– sneedshelp
Nov 15 '18 at 4:52
1
@sneedshelp. Your professor seems to be missing some key information, to put it mildly. I'll post an answer momentarily (or find a duplicate if I can).
– Mad Physicist
Nov 15 '18 at 12:27
add a comment |
1
Why do you not want to import it twice? As a reader, if I see someone import or use islice from script1, I'll assume that's a different function from the one in itertools. This just makes your code confusing, and introduces an unnecessary dependency on script1 from script2, that there must be that itertools import line.
– Melvin
Nov 15 '18 at 4:48
1
Actual imports only happen once. Successive imports do not reload anything, just fetch the reference.
– Mad Physicist
Nov 15 '18 at 4:49
@MadPhysicist it's because my professor told me to not import the same thing twice. Can you please elaborate on what you mean by not reloading anything so I can tell it to him
– sneedshelp
Nov 15 '18 at 4:52
1
@sneedshelp. Your professor seems to be missing some key information, to put it mildly. I'll post an answer momentarily (or find a duplicate if I can).
– Mad Physicist
Nov 15 '18 at 12:27
1
1
Why do you not want to import it twice? As a reader, if I see someone import or use islice from script1, I'll assume that's a different function from the one in itertools. This just makes your code confusing, and introduces an unnecessary dependency on script1 from script2, that there must be that itertools import line.
– Melvin
Nov 15 '18 at 4:48
Why do you not want to import it twice? As a reader, if I see someone import or use islice from script1, I'll assume that's a different function from the one in itertools. This just makes your code confusing, and introduces an unnecessary dependency on script1 from script2, that there must be that itertools import line.
– Melvin
Nov 15 '18 at 4:48
1
1
Actual imports only happen once. Successive imports do not reload anything, just fetch the reference.
– Mad Physicist
Nov 15 '18 at 4:49
Actual imports only happen once. Successive imports do not reload anything, just fetch the reference.
– Mad Physicist
Nov 15 '18 at 4:49
@MadPhysicist it's because my professor told me to not import the same thing twice. Can you please elaborate on what you mean by not reloading anything so I can tell it to him
– sneedshelp
Nov 15 '18 at 4:52
@MadPhysicist it's because my professor told me to not import the same thing twice. Can you please elaborate on what you mean by not reloading anything so I can tell it to him
– sneedshelp
Nov 15 '18 at 4:52
1
1
@sneedshelp. Your professor seems to be missing some key information, to put it mildly. I'll post an answer momentarily (or find a duplicate if I can).
– Mad Physicist
Nov 15 '18 at 12:27
@sneedshelp. Your professor seems to be missing some key information, to put it mildly. I'll post an answer momentarily (or find a duplicate if I can).
– Mad Physicist
Nov 15 '18 at 12:27
add a comment |
2 Answers
2
active
oldest
votes
The pythonic way to use islice
in script2
is to do
from itertools import islice
The import machinery in Python is pretty smart. Importing a module does not automatically load the module every time. The first step is always to check if the module is already in sys.modules
. If it is (as it would be in your case), the appropriate existing reference(s) are bound to name(s) in your namespace.
Let's look at a concrete example, with the two modules below:
mod1.py
from itertools import izip_longest, islice
from mod2 import some_function
# code that uses islice and some_function
mod2.py
from itertools import islice
def some_function():
# do the thing
If you run mod1
, the following (edited) sequence of events will happen:
- Create an empty module object and register/cache it under
sys.modules['mod1']
orsys.modules['__main__']
, depending on how you run the script. - Begin executing the code in
mod1.py
. - Check if
itertools
is insys.modules
: it's not
- Do some magic to find, register and load
itertools
intosys.modules['itertools']
- Add the attributes
izip_longest
andislice
to your global namespace in the incompletemod1
- Do some magic to find, register and load
- Check if
mod2
is insys.modules
: it's not
- Do some magic to find
mod2
and create an emptysys.modules['mod2']
- Begin executing the code in
mod2.py
. - Check if
itertools
is insys.modules
: it is - Add the attribute
islice
to your global namespace in the incompletemod2
. This is basically equivalent to doingislice = sys.modules['itertools'].islice
at this point. Nothing gets loaded here. - Execute the
def
statement to create a function object, and add it to themod2
global namespace with the namesome_func
.
mod2
is now fully loaded
- Do some magic to find
- Execute the remaining code in
mod1
So as you can see, while you can say that the itertools
module is technically imported twice, it is only loaded once. The second time, the name islice
is bound to a local name.
You can read the official documentation about the whole procedure here: https://docs.python.org/2/reference/simple_stmts.html#the-import-statement. This sequence of events has been around, in one implementation or another, since at least version 2.0: https://docs.python.org/2.0/ref/import.html. You may also want to read more on how modules work in general here: https://docs.python.org/2/tutorial/modules.html. As a rule, I have found the official docs to be the best teacher of Python outside maybe Stack Overflow.
The other thing to remember is that modules do not share namespaces. Globals within a module are just attributes of that particular module object. However, you could potentially do something like this:
mod2.py
from itertools import islice
def some_function(): pass
mod1.py
import mod2
#use mod2.some_function and mod2.islice as attributes of mod2
This way, you only have one explicit import of islice
. I would not consider this to be a very pythonic approach though. One of the key principles of Python is "explicit is better than implicit", and readability is more important than succinctness. When you use mod2.islice
, the implication to the reader is that mod2
defines a version of the function that is different from the built-in one.
As an aside, consider moving to Python 3.6+ soon. There are many improvements and new features. Python 2.x will not be supported for much longer, and many popular third party libraries such as numpy will be dropping Python 2 support entirely in coming releases.
add a comment |
What you could do here is import *
from script1, which would also include all its imports as well. Something like this:
# in script2.py
from script1 import *
This way you only have to do the imports once. Is this what you mean?
This is not a very good idea. You may clobber some unexpected names.
– Mad Physicist
Nov 15 '18 at 4:52
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%2f53312535%2fhow-can-i-use-the-same-imports-from-a-different-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The pythonic way to use islice
in script2
is to do
from itertools import islice
The import machinery in Python is pretty smart. Importing a module does not automatically load the module every time. The first step is always to check if the module is already in sys.modules
. If it is (as it would be in your case), the appropriate existing reference(s) are bound to name(s) in your namespace.
Let's look at a concrete example, with the two modules below:
mod1.py
from itertools import izip_longest, islice
from mod2 import some_function
# code that uses islice and some_function
mod2.py
from itertools import islice
def some_function():
# do the thing
If you run mod1
, the following (edited) sequence of events will happen:
- Create an empty module object and register/cache it under
sys.modules['mod1']
orsys.modules['__main__']
, depending on how you run the script. - Begin executing the code in
mod1.py
. - Check if
itertools
is insys.modules
: it's not
- Do some magic to find, register and load
itertools
intosys.modules['itertools']
- Add the attributes
izip_longest
andislice
to your global namespace in the incompletemod1
- Do some magic to find, register and load
- Check if
mod2
is insys.modules
: it's not
- Do some magic to find
mod2
and create an emptysys.modules['mod2']
- Begin executing the code in
mod2.py
. - Check if
itertools
is insys.modules
: it is - Add the attribute
islice
to your global namespace in the incompletemod2
. This is basically equivalent to doingislice = sys.modules['itertools'].islice
at this point. Nothing gets loaded here. - Execute the
def
statement to create a function object, and add it to themod2
global namespace with the namesome_func
.
mod2
is now fully loaded
- Do some magic to find
- Execute the remaining code in
mod1
So as you can see, while you can say that the itertools
module is technically imported twice, it is only loaded once. The second time, the name islice
is bound to a local name.
You can read the official documentation about the whole procedure here: https://docs.python.org/2/reference/simple_stmts.html#the-import-statement. This sequence of events has been around, in one implementation or another, since at least version 2.0: https://docs.python.org/2.0/ref/import.html. You may also want to read more on how modules work in general here: https://docs.python.org/2/tutorial/modules.html. As a rule, I have found the official docs to be the best teacher of Python outside maybe Stack Overflow.
The other thing to remember is that modules do not share namespaces. Globals within a module are just attributes of that particular module object. However, you could potentially do something like this:
mod2.py
from itertools import islice
def some_function(): pass
mod1.py
import mod2
#use mod2.some_function and mod2.islice as attributes of mod2
This way, you only have one explicit import of islice
. I would not consider this to be a very pythonic approach though. One of the key principles of Python is "explicit is better than implicit", and readability is more important than succinctness. When you use mod2.islice
, the implication to the reader is that mod2
defines a version of the function that is different from the built-in one.
As an aside, consider moving to Python 3.6+ soon. There are many improvements and new features. Python 2.x will not be supported for much longer, and many popular third party libraries such as numpy will be dropping Python 2 support entirely in coming releases.
add a comment |
The pythonic way to use islice
in script2
is to do
from itertools import islice
The import machinery in Python is pretty smart. Importing a module does not automatically load the module every time. The first step is always to check if the module is already in sys.modules
. If it is (as it would be in your case), the appropriate existing reference(s) are bound to name(s) in your namespace.
Let's look at a concrete example, with the two modules below:
mod1.py
from itertools import izip_longest, islice
from mod2 import some_function
# code that uses islice and some_function
mod2.py
from itertools import islice
def some_function():
# do the thing
If you run mod1
, the following (edited) sequence of events will happen:
- Create an empty module object and register/cache it under
sys.modules['mod1']
orsys.modules['__main__']
, depending on how you run the script. - Begin executing the code in
mod1.py
. - Check if
itertools
is insys.modules
: it's not
- Do some magic to find, register and load
itertools
intosys.modules['itertools']
- Add the attributes
izip_longest
andislice
to your global namespace in the incompletemod1
- Do some magic to find, register and load
- Check if
mod2
is insys.modules
: it's not
- Do some magic to find
mod2
and create an emptysys.modules['mod2']
- Begin executing the code in
mod2.py
. - Check if
itertools
is insys.modules
: it is - Add the attribute
islice
to your global namespace in the incompletemod2
. This is basically equivalent to doingislice = sys.modules['itertools'].islice
at this point. Nothing gets loaded here. - Execute the
def
statement to create a function object, and add it to themod2
global namespace with the namesome_func
.
mod2
is now fully loaded
- Do some magic to find
- Execute the remaining code in
mod1
So as you can see, while you can say that the itertools
module is technically imported twice, it is only loaded once. The second time, the name islice
is bound to a local name.
You can read the official documentation about the whole procedure here: https://docs.python.org/2/reference/simple_stmts.html#the-import-statement. This sequence of events has been around, in one implementation or another, since at least version 2.0: https://docs.python.org/2.0/ref/import.html. You may also want to read more on how modules work in general here: https://docs.python.org/2/tutorial/modules.html. As a rule, I have found the official docs to be the best teacher of Python outside maybe Stack Overflow.
The other thing to remember is that modules do not share namespaces. Globals within a module are just attributes of that particular module object. However, you could potentially do something like this:
mod2.py
from itertools import islice
def some_function(): pass
mod1.py
import mod2
#use mod2.some_function and mod2.islice as attributes of mod2
This way, you only have one explicit import of islice
. I would not consider this to be a very pythonic approach though. One of the key principles of Python is "explicit is better than implicit", and readability is more important than succinctness. When you use mod2.islice
, the implication to the reader is that mod2
defines a version of the function that is different from the built-in one.
As an aside, consider moving to Python 3.6+ soon. There are many improvements and new features. Python 2.x will not be supported for much longer, and many popular third party libraries such as numpy will be dropping Python 2 support entirely in coming releases.
add a comment |
The pythonic way to use islice
in script2
is to do
from itertools import islice
The import machinery in Python is pretty smart. Importing a module does not automatically load the module every time. The first step is always to check if the module is already in sys.modules
. If it is (as it would be in your case), the appropriate existing reference(s) are bound to name(s) in your namespace.
Let's look at a concrete example, with the two modules below:
mod1.py
from itertools import izip_longest, islice
from mod2 import some_function
# code that uses islice and some_function
mod2.py
from itertools import islice
def some_function():
# do the thing
If you run mod1
, the following (edited) sequence of events will happen:
- Create an empty module object and register/cache it under
sys.modules['mod1']
orsys.modules['__main__']
, depending on how you run the script. - Begin executing the code in
mod1.py
. - Check if
itertools
is insys.modules
: it's not
- Do some magic to find, register and load
itertools
intosys.modules['itertools']
- Add the attributes
izip_longest
andislice
to your global namespace in the incompletemod1
- Do some magic to find, register and load
- Check if
mod2
is insys.modules
: it's not
- Do some magic to find
mod2
and create an emptysys.modules['mod2']
- Begin executing the code in
mod2.py
. - Check if
itertools
is insys.modules
: it is - Add the attribute
islice
to your global namespace in the incompletemod2
. This is basically equivalent to doingislice = sys.modules['itertools'].islice
at this point. Nothing gets loaded here. - Execute the
def
statement to create a function object, and add it to themod2
global namespace with the namesome_func
.
mod2
is now fully loaded
- Do some magic to find
- Execute the remaining code in
mod1
So as you can see, while you can say that the itertools
module is technically imported twice, it is only loaded once. The second time, the name islice
is bound to a local name.
You can read the official documentation about the whole procedure here: https://docs.python.org/2/reference/simple_stmts.html#the-import-statement. This sequence of events has been around, in one implementation or another, since at least version 2.0: https://docs.python.org/2.0/ref/import.html. You may also want to read more on how modules work in general here: https://docs.python.org/2/tutorial/modules.html. As a rule, I have found the official docs to be the best teacher of Python outside maybe Stack Overflow.
The other thing to remember is that modules do not share namespaces. Globals within a module are just attributes of that particular module object. However, you could potentially do something like this:
mod2.py
from itertools import islice
def some_function(): pass
mod1.py
import mod2
#use mod2.some_function and mod2.islice as attributes of mod2
This way, you only have one explicit import of islice
. I would not consider this to be a very pythonic approach though. One of the key principles of Python is "explicit is better than implicit", and readability is more important than succinctness. When you use mod2.islice
, the implication to the reader is that mod2
defines a version of the function that is different from the built-in one.
As an aside, consider moving to Python 3.6+ soon. There are many improvements and new features. Python 2.x will not be supported for much longer, and many popular third party libraries such as numpy will be dropping Python 2 support entirely in coming releases.
The pythonic way to use islice
in script2
is to do
from itertools import islice
The import machinery in Python is pretty smart. Importing a module does not automatically load the module every time. The first step is always to check if the module is already in sys.modules
. If it is (as it would be in your case), the appropriate existing reference(s) are bound to name(s) in your namespace.
Let's look at a concrete example, with the two modules below:
mod1.py
from itertools import izip_longest, islice
from mod2 import some_function
# code that uses islice and some_function
mod2.py
from itertools import islice
def some_function():
# do the thing
If you run mod1
, the following (edited) sequence of events will happen:
- Create an empty module object and register/cache it under
sys.modules['mod1']
orsys.modules['__main__']
, depending on how you run the script. - Begin executing the code in
mod1.py
. - Check if
itertools
is insys.modules
: it's not
- Do some magic to find, register and load
itertools
intosys.modules['itertools']
- Add the attributes
izip_longest
andislice
to your global namespace in the incompletemod1
- Do some magic to find, register and load
- Check if
mod2
is insys.modules
: it's not
- Do some magic to find
mod2
and create an emptysys.modules['mod2']
- Begin executing the code in
mod2.py
. - Check if
itertools
is insys.modules
: it is - Add the attribute
islice
to your global namespace in the incompletemod2
. This is basically equivalent to doingislice = sys.modules['itertools'].islice
at this point. Nothing gets loaded here. - Execute the
def
statement to create a function object, and add it to themod2
global namespace with the namesome_func
.
mod2
is now fully loaded
- Do some magic to find
- Execute the remaining code in
mod1
So as you can see, while you can say that the itertools
module is technically imported twice, it is only loaded once. The second time, the name islice
is bound to a local name.
You can read the official documentation about the whole procedure here: https://docs.python.org/2/reference/simple_stmts.html#the-import-statement. This sequence of events has been around, in one implementation or another, since at least version 2.0: https://docs.python.org/2.0/ref/import.html. You may also want to read more on how modules work in general here: https://docs.python.org/2/tutorial/modules.html. As a rule, I have found the official docs to be the best teacher of Python outside maybe Stack Overflow.
The other thing to remember is that modules do not share namespaces. Globals within a module are just attributes of that particular module object. However, you could potentially do something like this:
mod2.py
from itertools import islice
def some_function(): pass
mod1.py
import mod2
#use mod2.some_function and mod2.islice as attributes of mod2
This way, you only have one explicit import of islice
. I would not consider this to be a very pythonic approach though. One of the key principles of Python is "explicit is better than implicit", and readability is more important than succinctness. When you use mod2.islice
, the implication to the reader is that mod2
defines a version of the function that is different from the built-in one.
As an aside, consider moving to Python 3.6+ soon. There are many improvements and new features. Python 2.x will not be supported for much longer, and many popular third party libraries such as numpy will be dropping Python 2 support entirely in coming releases.
edited Nov 15 '18 at 13:20
answered Nov 15 '18 at 13:08
Mad PhysicistMad Physicist
38k1674107
38k1674107
add a comment |
add a comment |
What you could do here is import *
from script1, which would also include all its imports as well. Something like this:
# in script2.py
from script1 import *
This way you only have to do the imports once. Is this what you mean?
This is not a very good idea. You may clobber some unexpected names.
– Mad Physicist
Nov 15 '18 at 4:52
add a comment |
What you could do here is import *
from script1, which would also include all its imports as well. Something like this:
# in script2.py
from script1 import *
This way you only have to do the imports once. Is this what you mean?
This is not a very good idea. You may clobber some unexpected names.
– Mad Physicist
Nov 15 '18 at 4:52
add a comment |
What you could do here is import *
from script1, which would also include all its imports as well. Something like this:
# in script2.py
from script1 import *
This way you only have to do the imports once. Is this what you mean?
What you could do here is import *
from script1, which would also include all its imports as well. Something like this:
# in script2.py
from script1 import *
This way you only have to do the imports once. Is this what you mean?
answered Nov 15 '18 at 4:45
David LDavid L
31611
31611
This is not a very good idea. You may clobber some unexpected names.
– Mad Physicist
Nov 15 '18 at 4:52
add a comment |
This is not a very good idea. You may clobber some unexpected names.
– Mad Physicist
Nov 15 '18 at 4:52
This is not a very good idea. You may clobber some unexpected names.
– Mad Physicist
Nov 15 '18 at 4:52
This is not a very good idea. You may clobber some unexpected names.
– Mad Physicist
Nov 15 '18 at 4:52
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%2f53312535%2fhow-can-i-use-the-same-imports-from-a-different-script%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
Why do you not want to import it twice? As a reader, if I see someone import or use islice from script1, I'll assume that's a different function from the one in itertools. This just makes your code confusing, and introduces an unnecessary dependency on script1 from script2, that there must be that itertools import line.
– Melvin
Nov 15 '18 at 4:48
1
Actual imports only happen once. Successive imports do not reload anything, just fetch the reference.
– Mad Physicist
Nov 15 '18 at 4:49
@MadPhysicist it's because my professor told me to not import the same thing twice. Can you please elaborate on what you mean by not reloading anything so I can tell it to him
– sneedshelp
Nov 15 '18 at 4:52
1
@sneedshelp. Your professor seems to be missing some key information, to put it mildly. I'll post an answer momentarily (or find a duplicate if I can).
– Mad Physicist
Nov 15 '18 at 12:27