I use webbrowser only in one part of my code, and it seems waste to import it since the beginning of the script, is it a way to import it only at the part of the code I need to use it? Thanks!
Asked
Active
Viewed 308 times
1 Answers
1
Yes. You move the import statement from the top of the script to the point in your script where you want it. For example, you would move the below line from the top of your script to somewhere in the code.
import your_library
Be warned though, if the import is in a function, every time you call that function it will import that library, which may add time to the overall running of the file.
Let me know if you meant a different question.

A. N. Other
- 392
- 4
- 14
-
Thats what I meant, awesome! – Nitzan Shwartz Oct 14 '16 at 11:40
-
Thanks a lot, sorry for 2 comments I sent the other one accidently before finishing :). – Nitzan Shwartz Oct 14 '16 at 11:41
-
By the way, about the import inside of a function, isn't there a way that you can terminate the import so you can open it when you need it in the function and when it done you can just elliminate it (then reimport and so-on). – Nitzan Shwartz Oct 14 '16 at 11:48
-
Sadly not, as it doesn't seem to be imported. See http://stackoverflow.com/questions/8781257/remove-an-imported-python-module and then http://stackoverflow.com/questions/437589/how-do-i-unload-reload-a-python-module – A. N. Other Oct 14 '16 at 12:53
-
Finally, this one is probably the most helpful - http://stackoverflow.com/questions/3105801/unload-a-module-in-python – A. N. Other Oct 14 '16 at 12:57
-
The last one will do! – Nitzan Shwartz Oct 14 '16 at 14:18
-
Again just wanted shift+enter and it sends it :(, anyways the last one will do thanks a lot! – Nitzan Shwartz Oct 14 '16 at 14:19