Reason for not allowing import twice but allowing reload()
Hello list, We can not import a module twice in a session of Python (subsequent attempts to import same module don't result in any error though, but it is not-effective). However after making change to module, we can reload() it (if not reload(), we could possibly have reimport statement) to get the updates in module. I am a newbie in Python (learning via IDLE) and trying to understand - what is extra (special) that import does, that reload() doesn't? - if both do the same internally and externally, why subsequent imports of same module are ineffective? - if subsequent imports of same module in a session are not effective, why not simply flag those attempts as an error, rather than letting them go effect-less. Kindly help me understand the reasons behind. Regards Sandeep -- https://mail.python.org/mailman/listinfo/python-list
Re: Reason for not allowing import twice but allowing reload()
Thanks Chris and Ian, Your suggested experiments and explanations have really provided some good insights, something Tutorial didn't indicate. Ian's explanation reminds me of #ifndef _HEADER_H_ #define _HEADER_H_ ... #endif // _HEADER_H_ in C/C++, and I can draw parallels to why subsequent attempts to import same module shouldn't result in flagging an error, and need for multiple imports to refer to same information. -- https://mail.python.org/mailman/listinfo/python-list
Re: Reason for not allowing import twice but allowing reload()
Hello Rustom,
F5 in Idle restarts the Python interpreter (that's what my impression is).
Whatever you have done earlier at Idle prompt (in Idle session) before F5 is
gone after F5.
Try simple experiment at prompt.
>>> myvar="hello"
>>> myvar
'hello'
myvar is gone after F5.
As for need of import in Idle session, I use it to
- import sys
- sys.append.path('D:\\Where\\Ever\\My\\Modules\\Lie')
- import mymodule
--
https://mail.python.org/mailman/listinfo/python-list
Re: Reason for not allowing import twice but allowing reload()
> As for need of import in Idle session, I use it to
> - import sys
> - sys.append.path('D:\\Where\\Ever\\My\\Modules\\Lie')
Kindly read above as
sys.path.append()
> - import mymodule
--
https://mail.python.org/mailman/listinfo/python-list
Re: Reason for not allowing import twice but allowing reload()
> Why does one use (something like) idle? > To experiment. > > So what's your experiment-focus? True. Experiment only. What happens (and in environment) when you use - import module - from module import name - from module import name as othername Idle is start point, but we aren't always going to be Idle-ing, so need to work towards that as well while practicing basic concepts. [P.S.- I think we are digressing a lot from original thread question.] -- https://mail.python.org/mailman/listinfo/python-list
Re: Reason for not allowing import twice but allowing reload()
Steven, > There are better ways to manage your Python path than to manually insert > paths into sys.path like that. What version of Python are you using? I would love to know, apart from PYTHONPATH and sys.path.append() route. I am using Python 2.7.11 to start with as suggested by my employer. -- https://mail.python.org/mailman/listinfo/python-list
Re: Reason for not allowing import twice but allowing reload()
Based on the input from members, and my subsequent reading the textbook/tutorials, let me summarize my understanding of "why subsequent imports of same module are designed to be effect-less". 1. Imports are costly affair, as it involves - finding the module's file - compile it to byte code (if needed) - run the module's code to build the objects it defines. 2a. Quoting from "Learning Python: Mark Lutz" : In Python, cross-file module linking is not resolved until such import statements are **executed at runtime**. import being a statement, can come anywhere a statement can, so a module can be selectively imported depending upon the conditions at runtime. 2b. Inter-dependencies between modules: For example, A (main module) imports B, C, D and E. B again imports C and E, D imports B and C. If import statements are processed entirely from scratch each time, it would amount to lot of **redundant (and costly)** work. Secondly there could be dangerous? side-effects of reprocessing imports from scratch. Let us say, in above example, module B does some initialization of variables, or things like resetting a file for further work during the session. These variables and file are updated during the course of execution of A. Much later via some conditional path, module B is imported again, and BOOM (were import processed from scratch again). In this case - we are respecting that every import of a module should give same picture (assuming it is not modified across two different invocation during the program execution). An argument here could be: to code module B in such a way that those initializations don't happen again, but just once - check before resetting etc. during the import. But wouldn't that violate the *same picture* requirement? Probably my example is not that perfect. -- https://mail.python.org/mailman/listinfo/python-list
Re: Reason for not allowing import twice but allowing reload()
>From the discussions in this thread, I get the impression that there are >genuine requirements to reload() a module during a program's execution. It is fairly easy to see reload() in context of interactive execution, but how does it come into picture in case of non-interactive Python program executions? - one way could to be periodically poll for change of module version (time stamp etc.), and reload(), - other could be sending a signal to program and reload() selected modules during processing of that signal by the program. Is it done this way in Python as well? There used to be programs which could reload the configuration file (and act accordingly) without needing to restart the program) when specific signal was sent to them. Am I on right track in my understanding? What are the other ways to accomplish reload() during the execution of a non-interactive Python program? -- https://mail.python.org/mailman/listinfo/python-list
IDLE question: "you may still have to reload nested modules."
Hello list, I am following "Learning Python: Mark Lutz" and came across following in chapter 3 few days back. [quote] * You may still have to reload nested modules. Technically speaking, IDLE's Run->Run Module menu option always runs the current version of the top-level file only; imported files may still need to be interactively reloaded when changed. ... [/quote] It is slightly confusing, as to what it does imply. Could any IDLE guru clarify if I am missing or overlooking something? If a needed-by-top-level-file module was changed after 'Run->Run Module', it would any way need to be reloaded. If it was changed between two "Run->Run Module" actions, won't it be automatically loaded afresh as a part of restarting Python interpreter and executing top-level-file [where is the question of reloading then?]? [Side observation: though F5 or "Run->Run Module" restarts Python interpreter with top-level-file afresh [indicated by dir() content before and after], the command history is not reset during this. This likely confirms the GUI process being separate than interpreter.] Regards Sandeep -- https://mail.python.org/mailman/listinfo/python-list
