refresing the edited python function

S

Sudheer Joseph

Hi,
I have been using ipython and ipython with qtconsole and working on a code with functions. Each time I make a modification in function 

I have to quit IPTHON console (in both with and with out qt console )and reload the function freshly. If I need to see the changed I made in the function. I tried below options
del function name

import the module again  by issuing "from xxx.py import yy"
import xxx.py
make changes
reload(xxx.py)
this
works only if the the function in the code hassame name as the code.
But even this do not reflect the changes made byediting the code.
So what is the standard way to update the function forfurther tests after an edit?
with best regards,
Sudheer
 
***************************************************************
Sudheer Joseph
Indian National Centre for Ocean Information Services
Ministry of Earth Sciences, Govt. of India
POST BOX NO: 21, IDA Jeedeemetla P.O.
Via Pragathi Nagar,Kukatpally, Hyderabad; Pin:5000 55
Tel:+91-40-23886047(O),Fax:+91-40-23895011(O),
Tel:+91-40-23044600(R),Tel:+91-40-9440832534(Mobile)
E-mail:[email protected];[email protected]
Web- http://oppamthadathil.tripod.com
***************************************************************
 
A

alex23

I have been using ipython and ipython with qtconsole and working on a
code with functions. Each time I make a modification in function
I have to quit IPTHON console (in both with and with out qt console )
and reload the function freshly. If I need to see the changed I made in
the function. I tried below options
del function name
import the module again by issuing "from xxx.py import yy"

This doesn't re-import the module if xxx has already been imported. It
simply rebinds xxx.yy to yy.
import xxx.py

This also doesn't re-import the module if it has already been imported.

When you import a module, or a function from a module, a module object
is created and stored in sys.modules. Any subsequent 'import <module>'
calls will return a reference to that module object, and won't reload
from file at all.

You can easily verify this by creating a test module 'foo' with a single
line of `print('loading foo')` and then trying this from the console:

In [1]: import foo
loading foo

In [2]: del foo

In [3]: import foo

In [4]:

Note that you only see 'loading foo' the first time you import the
module. In order to have the module loaded again rather than returning
the existing reference, you would use `reload(foo)`:

In [5]: reload(foo)
loading foo

So: in order to be able to use functions from a re-loaded module, you
should always refer to them via the module object, and not import them
directly:

Or: you can reload the module and then rebind the functions:

Hope this helps.
 
P

Prasad, Ramit

alex23
I have been using ipython and ipython with qtconsole and working on a
code with functions. Each time I make a modification in function
I have to quit IPTHON console (in both with and with out qt console )
and reload the function freshly. If I need to see the changed I made in
the function. I tried below options
del function name
import the module again by issuing "from xxx.py import yy"

This doesn't re-import the module if xxx has already been imported. It
simply rebinds xxx.yy to yy.
import xxx.py

This also doesn't re-import the module if it has already been imported.

When you import a module, or a function from a module, a module object
is created and stored in sys.modules. Any subsequent 'import <module>'
calls will return a reference to that module object, and won't reload
from file at all.

You can easily verify this by creating a test module 'foo' with a single
line of `print('loading foo')` and then trying this from the console:

In [1]: import foo
loading foo

In [2]: del foo

In [3]: import foo

In [4]:

Note that you only see 'loading foo' the first time you import the
module. In order to have the module loaded again rather than returning
the existing reference, you would use `reload(foo)`:

In [5]: reload(foo)
loading foo

So: in order to be able to use functions from a re-loaded module, you
should always refer to them via the module object, and not import them
directly:

Or: you can reload the module and then rebind the functions:

Hope this helps.

--

In Python 3 the reload built-in was moved to the imp module.
So use imp.reload(<module>) instead.


~Ramit



This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy andcompleteness of information, viruses, confidentiality, legal privilege, andlegal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top