By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,526 Members | 1,618 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,526 IT Pros & Developers. It's quick & easy.

How to reload local namespace definitions in the python interpreter?

P: n/a
Hi,

I am a beginner using the python interpreter. To reduce typing effort,
I created a module called "aliases.py" containing some aliases for
objects I commonly use like -

aliases.py :
import filecmp, os, commands

op = os.path
go = commands.getoutput
dc = filecmp.dircmp
p1 = '/mnt/usbkey/flash/'
p2 = '/mnt/fat32/myfiles/flash/'

When I start up the interpreter, I can simply type -

from aliases import *

This works fine, but each time I change any of the definitions in
aliases.py, I
have to restart the interpreter and type "from aliases import *"
again. Is there any way to reload these definitions without restarting
the interpreter?

-Slath

Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
te***********@yahoo.co.uk wrote:
Hi,

I am a beginner using the python interpreter. To reduce typing effort,
I created a module called "aliases.py" containing some aliases for
objects I commonly use like -

aliases.py :
import filecmp, os, commands

op = os.path
go = commands.getoutput
dc = filecmp.dircmp
p1 = '/mnt/usbkey/flash/'
p2 = '/mnt/fat32/myfiles/flash/'

When I start up the interpreter, I can simply type -

from aliases import *

This works fine, but each time I change any of the definitions in
aliases.py, I
have to restart the interpreter and type "from aliases import *"
again. Is there any way to reload these definitions without restarting
the interpreter?

-Slath


reload(aliases)

See: http://www.python.org/doc/2.4.1/lib/built-in-funcs.html

By the way, are you aware of the import ... as ... idiom?
e.g. import os.path as op

--
Website: www DOT jarmania FULLSTOP com
Jul 18 '05 #2

P: n/a
Tim Jarman wrote:
te***********@yahoo.co.uk wrote:

Hi,

I am a beginner using the python interpreter. To reduce typing effort,
I created a module called "aliases.py" containing some aliases for
objects I commonly use like -

aliases.py :
import filecmp, os, commands

op = os.path
go = commands.getoutput
dc = filecmp.dircmp
p1 = '/mnt/usbkey/flash/'
p2 = '/mnt/fat32/myfiles/flash/'

When I start up the interpreter, I can simply type -

from aliases import *

This works fine, but each time I change any of the definitions in
aliases.py, I
have to restart the interpreter and type "from aliases import *"
again. Is there any way to reload these definitions without restarting
the interpreter?

-Slath

reload(aliases)

Unfortunately a simple reload of the module won't result in the required
changes to the local namespace:

$ cat test87.py
val = 33
from test87 import *
print val 33 val = 42
reload(test87) <module 'test87' from 'test87.pyc'> print val 42

See: http://www.python.org/doc/2.4.1/lib/built-in-funcs.html

By the way, are you aware of the import ... as ... idiom?
e.g. import os.path as op


This would, of course, require the user to qualify the names by
prefixing them with "op.".

regards
Steve
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/

Jul 18 '05 #3

P: n/a
On Apr 4, 2005 11:10 AM, Steve Holden <st***@holdenweb.com> wrote:
Tim Jarman wrote:
te***********@yahoo.co.uk wrote:

Hi,

I am a beginner using the python interpreter. To reduce typing effort,
I created a module called "aliases.py" containing some aliases for
objects I commonly use like -

aliases.py :
import filecmp, os, commands

op = os.path
<snip> By the way, are you aware of the import ... as ... idiom?
e.g. import os.path as op


This would, of course, require the user to qualify the names by
prefixing them with "op.".


What the OP listed above requires that too.

Peace
Bill Mill
bill.mill at gmail.com
Jul 18 '05 #4

P: n/a
Bill Mill wrote:
On Apr 4, 2005 11:10 AM, Steve Holden <st***@holdenweb.com> wrote:
Tim Jarman wrote:
te***********@yahoo.co.uk wrote:

Hi,

I am a beginner using the python interpreter. To reduce typing effort,
I created a module called "aliases.py" containing some aliases for
objects I commonly use like -

aliases.py :
import filecmp, os, commands

op = os.path
<snip>
By the way, are you aware of the import ... as ... idiom?
e.g. import os.path as op


This would, of course, require the user to qualify the names by
prefixing them with "op.".

What the OP listed above requires that too.

I believe the OP was suggesting

from aliases import *

which specifically loads the names in the module (or those specified in
module.__all__) into the importing namespace for use without qualification.

regards
Steve
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.