Let's say I have this python script script.py and I load it in the interpreter by typing
import script
and then I execute my function by typing:
script.testFunction(testArgument)
OK so far so good, but when I change script.py, if I try to import again the script doesn't update. I have to exit from the interpreter, restart the interpreter, and then import the new version of the script for it to work.
What should I do instead?
You can issue a reload script, but that will not update your existing objects and will not go deep inside other modules.
Fortunately this is solved by IPython - a better python shell which supports auto-reloading.
To use autoreloading in IPython, you'll have to type import ipy_autoreload first, or put it permanently in your ~/.ipython/ipy_user_conf.py.
Then run:
%autoreload 1
%aimport script
%autoreload 1 means that every module loaded with %aimport will be reloaded before executing code from the prompt. This will not update any existing objects, however.
See http://ipython.org/ipython-doc/dev/config/extensions/autoreload.html for more fun things you can do.
http://docs.python.org/library/functions.html#reload
reload(module)
Reload a previously imported module. The argument must
be a module object, so it must have been successfully imported before.
This is useful if you have edited the module source file using an
external editor and want to try out the new version without leaving
the Python interpreter. The return value is the module object (the
same as the module argument).
An alternative solution that has helped me greatly is to maintain a copy of sys.modules keys and pop the new modules after the import to force re-imports of deep imports:
>>> oldmods = set(sys.modules.keys())
>>> import script
>>> # Do stuff
>>> for mod in set(sys.modules.keys()).difference(oldmods): sys.modules.pop(mod)
>>> import script
Related
I am studying python, both 3. and 2. I started a few days ago.
I want to know the differences between site module and interpreter.
I got this question from
Python exit commands - why so many and when should each be used?
Those explanations are very clear but it's still hard to me.
If I am understanding your question correctly,site is a module in Python. A module is a file containing Python definitions and statements. In order to use the functions (for ex: exit() or quit(), you need to import the site module as those respective functions are defined in there.
The Python interpreter is the program that reads and executes Python code. This includes source code, pre-compiled code, scripts - in this case you reference, you would need to import the site module into your current Python interpreter session, in order to use say exit() or quit() in that given session.
So, the process of this particular question would be:
* Activate the Python interpreter by typing into your respective terminal the version of Python you have installed on your computer, ex. python3.
* In the Python interpreter, type import site
Hope that helps Hwan.
I assume you are stuck on understanding:
"Nevertheless, quit should not be used in production code. This is
because it only works if the site module is loaded. Instead, this
function should only be used in the interpreter."
Basically, what that is saying is that quit is a part of a module loaded in the python interpreter. That module's name, is site.
Firstly, the python interpreter is what you use to run python scripts or environments. It interprets python commands. For example, if you write a = 1 in a script or python environment, the interpreter takes that command and executes it without compiling it. (If it was a language like c you would need to compile the script before you run it).
Secondly, a module is a pre-written file that can define functions, classes and variables. When you write import numpy into python, you are importing the module, numpy. Therefore when they say "this only works if the site module is loaded", that means that import site must be executed.
When you start a python interpreter (by typing python into your command shell), it automatically imports site, which has sys, venv and main etc. which are all required to run an active interpreter session.
I do the following in my IPython notebook:
import sys
sys.path.append('my_directory')
from db import *
It works fine. But then I added a new function to the db.py and IPython does not see it. It OK. But it does not see it even if I reset everything end re-execute the cell that imports everything. It does not see it even if I user reload. It does not see it even if I close the IPython notebook and restart it.
What is the way to force IPython (or python) to see the updated content of the file?
You need to use autoreload. Check the manual at http://ipython.org/ipython-doc/dev/config/extensions/autoreload.html. Seems you need:
%autoreload 2
The above will automatically reload all imported modules. Except those inlcuded in a separate special list of modules specified by %aimport modulename. Those will only be autoreloaded if you specify %autoreload 1.
Say I have a script that imports various modules in Python.
import my_module
from some_other_module import foo
...
I then run this script from IPython.
Say I make changes to the function bar in my_module and foo in some_other_module.
Say that I now want to interactively call either my_module.bar() or foo() from my IPython session.
Is there a way to have IPython automatically reload every loaded module when I invoke a command before executing the command?
If not automatically, how can I reload every loaded module manually in IPython without having to explicitly name the module?
Finally, is there a way to set up my IPython session in my ipython_config.py (startup file) so that it supports this functionality off-the-shelf?
I would suggest a %load_ext autoreload followed by a %autoreload? to see how to use it.
You can also have a look at InteractiveShellApp.extensions and InteractiveShellApp.extra_extension configuration options for extension at startup.
Finally, you can also add a .py file in your IPython profile dir ($ ipython locate to get it), put it in the startup subfolder, it will be executed at startup time.
There is a restriction though, C modules cannot be reloaded.
Every time I make a change to a python script I have to reload python and re-import the module. Please advise how I can modify my scripts and run then without having to relaunch python in the terminal.
Thanks.
I've got a suggestion, based on your comment describing your work flow:
first, i run python3.1 in terminal second, i do "import module" then, i run a method from the module lets say "module.method(arg)" every time, i try to debug the code, i have to do this entire sequence, even though the change is minor. it is highly inefficient
Instead of firing up the interactive Python shell, make the module itself executable. The easiest way to do this is to add a block to the bottom of the module like so:
if __name__ == '__main__':
method(arg) # matches what you run manually in the Python shell
Then, instead of running python3.1, then importing the module, then calling the method, you can do something like this:
python3.1 modulename.py
and Python will run whatever code is in the if __name__ == '__main__' block. But that code will not be run if the module is imported by another Python module. More information on this common Python idiom can be found in the Python tutorial.
The advantage of this is that when you make a change to your code, you can usually just re-run the module by pressing the up arrow and hitting enter. No messy reloading necessary.
If it's just some module that's changing, you can call reload(module) in your script.
Do you mean that you enter script directly into the interactive python shall, or are you executing your .py file from the terminal by running something like python myscript.py?
I found a bypass online. Magic commands exist. It works like a charm for me. The Reload option didn't work for me. Got it from here and point 3 of this link.
Basically all you have to do is the following: and changes you make are reflected automatically after you save.
In [1]: %load_ext autoreload
In [2]: %autoreload 2
In [3]: Import MODULE
In [4]: my_class = Module.class()
my_class.printham()
Out[4]: ham
In [5]: #make changes to printham and save
In [6]: my_class.printham()
Out[6]: hamlet
You can use reload to re-import a module. I use it frequently when using the interactive mode to debug code.
However, from a higher level, I would be hesitant to use that in a production version of a program. Unless you will have very strict control over how sub-modules are changing, it would not be hard to have the reloaded module change in some way that breaks your program. Unless your program really needs 100% up-time, it would make sense to stop it and start it again when there is some kind of version change.
When I start the interactive django shell through manage.py, by executing
python -v manage.py shell
from the project directory, I see a lot of modules of format django.package.module getting imported in the verbose output but still I have to import them to use it in the shell.
The same happens when I just run the Python shell (with the -v argument). For example I see this in the verbose output,
import os # precompiled from /usr/local/gdp/lib/python2.4/os.pyc
but still i have to do import os to import and use the os module. What is being imported that am seeing in the verbose output and why I have to import them explicitly again to use them in the shell? Does Python load some essential modules while starting the shell or is it some kind of behind-the-scene magic?
-v traces the first import of a module -- the one that actually loads the module (executes its code, and so may take a bit of time) and sticks it into sys.modules.
That has nothing to do whether your interactive session (module __main__) gets the module injected into its namespace, of course. To ensure module 'goo' does get into the namespace of module 'X' (for any X, so of course including __main__... among many, many others), module 'X' just needs to import goo itself (a very fast operation indeed, if sys.modules['goo'] is already defined!-).
Python loads the site module implicitly when starting up, which may in turn import other modules for its own use. You can pass -S to disable this behavior.
They are getting imported (look at sys.modules) and references to the module are created in whichever modules have imported it.
When you do an import in your shell, if the module has already been imported, you will just get a copy of the reference to it in sys.modules