Re: unloading a module created with imp.new_module

2014-11-24 Thread Patrick Stinson
> On Nov 24, 2014, at 2:44 AM, Ned Batchelder wrote: > > On 11/23/14 5:10 AM, Patrick Stinson wrote: >> I am defining a single class with a destructor method that prints >> ‘__del__’, and running that source code string using exec with the >> module’s dict like so: >> >> import rtmidi >> import

Re: unloading a module created with imp.new_module

2014-11-24 Thread Chris Angelico
On Tue, Nov 25, 2014 at 5:48 PM, Patrick Stinson wrote: > Is it still possible to import non-module objects? I guess this question is > just for fun now :) rosuav@sikorsky:~$ python3 Python 3.5.0a0 (default:23ab1197df0b, Nov 20 2014, 12:57:44) [GCC 4.7.2] on linux Type "help", "copyright", "credi

Re: unloading a module created with imp.new_module

2014-11-24 Thread Patrick Stinson
> On Nov 24, 2014, at 6:12 AM, Ian Kelly wrote: > > > On Nov 24, 2014 1:27 AM, "Patrick Stinson" > wrote: > > > > How does the __del__ method have a reference to the module’s globals dict? > > because it references the print function? > > The module's dict becom

Re: unloading a module created with imp.new_module

2014-11-24 Thread Chris Angelico
On Tue, Nov 25, 2014 at 2:12 AM, Ian Kelly wrote: >> Crazy. Is there any other way to comfort when a module is being deleted >> beside defining a class object with a printing dtor? > > Modules don't strictly have to be ModuleType. You could substitute a class > with a __del__ method for the module

Re: unloading a module created with imp.new_module

2014-11-24 Thread Ian Kelly
On Nov 24, 2014 1:27 AM, "Patrick Stinson" wrote: > > How does the __del__ method have a reference to the module’s globals dict? because it references the print function? The module's dict becomes the __globals__ dict used by the function for looking up globals and builtins. > Crazy. Is there an

Re: unloading a module created with imp.new_module

2014-11-24 Thread Chris Angelico
On Mon, Nov 24, 2014 at 10:21 PM, Ned Batchelder wrote: > Keep in mind that Python object have a reference to their class. If your > users are defining classes in their modules, and you instantiate the class > to create objects, then even when you reload updated code, the old objects > will still

Re: unloading a module created with imp.new_module

2014-11-24 Thread Ned Batchelder
On 11/23/14 5:10 AM, Patrick Stinson wrote: I am defining a single class with a destructor method that prints ‘__del__’, and running that source code string using exec with the module’s dict like so: import rtmidi importsys import types importtime importgc s= """ class A: def __del__(self)

Re: unloading a module created with imp.new_module

2014-11-24 Thread Ned Batchelder
On 11/24/14 2:25 AM, Patrick Stinson wrote: Basically, I have an app and am making it possible for the users to automate it by writing python scripts that run against the app's API. It includes PyQt5 widgets, a QScintilla editor, and a simple API for I/O. Apple’s app store is the plan. Right now

Re: unloading a module created with imp.new_module

2014-11-24 Thread Chris Angelico
On Mon, Nov 24, 2014 at 6:27 PM, Patrick Stinson wrote: > How does the __del__ method have a reference to the module’s globals dict? > because it references the print function? > > Crazy. Is there any other way to comfort when a module is being deleted > beside defining a class object with a print

Re: unloading a module created with imp.new_module

2014-11-24 Thread Patrick Stinson
How does the __del__ method have a reference to the module’s globals dict? because it references the print function? Crazy. Is there any other way to comfort when a module is being deleted beside defining a class object with a printing dtor? > On Nov 23, 2014, at 4:27 PM, Ian Kelly wrote: >

Re: unloading a module created with imp.new_module

2014-11-24 Thread Patrick Stinson
Basically, I have an app and am making it possible for the users to automate it by writing python scripts that run against the app's API. It includes PyQt5 widgets, a QScintilla editor, and a simple API for I/O. Apple’s app store is the plan. Right now I create a new module with types.ModuleTyp

Re: unloading a module created with imp.new_module

2014-11-24 Thread Patrick Stinson
I am defining a single class with a destructor method that prints ‘__del__’, and running that source code string using exec with the module’s dict like so: import rtmidi import sys import types import time import gc s = """

Re: unloading a module created with imp.new_module

2014-11-23 Thread Steven D'Aprano
On Sat, 22 Nov 2014 21:49:32 -0900, Patrick Stinson wrote: > If I create a module with imp.new_module(name), how can I unload it so > that all the references contained in it are set to zero and the module > is deleted? deleting the reference that is returned doesn’t seem to do > the job, and it’s

Re: unloading a module created with imp.new_module

2014-11-23 Thread Ned Batchelder
On 11/23/14 1:49 AM, Patrick Stinson wrote: If I create a module with imp.new_module(name), how can I unload it so that all the references contained in it are set to zero and the module is deleted? deleting the reference that is returned doesn’t seem to do the job, and it’s not in sys.modules,

Re: unloading a module created with imp.new_module

2014-11-23 Thread Ian Kelly
On Nov 23, 2014 4:10 AM, "Patrick Stinson" wrote: > m = types.ModuleType('mine') > exec(s, m.__dict__) > print('deleting...') > m = None > print('done') > > and the output is: > > deleting... > done > __del__ > > I the “__del__" to come between “deleting…” and “done”. This is not being run from th

Re: unloading a module created with imp.new_module

2014-11-23 Thread Ian Kelly
On Sun, Nov 23, 2014 at 2:48 AM, Ian Kelly wrote: > On Sat, Nov 22, 2014 at 11:49 PM, Patrick Stinson > wrote: >> If I create a module with imp.new_module(name), how can I unload it so that >> all the references contained in it are set to zero and the module is >> deleted? deleting the referen

Re: unloading a module created with imp.new_module

2014-11-23 Thread Ian Kelly
On Sat, Nov 22, 2014 at 11:49 PM, Patrick Stinson wrote: > If I create a module with imp.new_module(name), how can I unload it so that > all the references contained in it are set to zero and the module is deleted? > deleting the reference that is returned doesn’t seem to do the job, and it’s >

unloading a module created with imp.new_module

2014-11-23 Thread Patrick Stinson
If I create a module with imp.new_module(name), how can I unload it so that all the references contained in it are set to zero and the module is deleted? deleting the reference that is returned doesn’t seem to do the job, and it’s not in sys.modules, so where is the dangling reference? Thanks!

Re: Problem with Dynamically unloading a module

2010-01-12 Thread Aahz
In article <78388a7a-b148-499a-8894-34e55721e...@k19g2000pro.googlegroups.com>, lordofcode wrote: > >Thanks you all for your replies ,cleared quiet a few doubts about >importing modules and namespace references . >Currently am going with static importing of all modules. But it may >not be efficie

Re: Problem with Dynamically unloading a module

2009-12-24 Thread Lie Ryan
On 12/24/2009 11:51 PM, Jean-Michel Pichavant wrote: But what he *can* do is to learn how importing works. I'm not sure it's terribly helpful to tell somebody all the things they can't do instead of what they can. Hacking python imports would not be in the top of the list of things I would tea

Re: Problem with Dynamically unloading a module

2009-12-24 Thread Jean-Michel Pichavant
Steven D'Aprano wrote: On Wed, 23 Dec 2009 15:31:53 +0100, Jean-Michel Pichavant wrote: Steven D'Aprano wrote: On Wed, 23 Dec 2009 13:37:06 +0100, Jean-Michel Pichavant wrote: But believe me, you don't want to mess up with the python import mechanism. Unle

Re: Problem with Dynamically unloading a module

2009-12-23 Thread Steve Holden
lordofcode wrote: > Hi All > > Not an expert in Python, so sorry if this sounds like a silly > question. > I went through other few threads in the mailing list but they are not > helping me much. > I have run into a problem related to dynamically loading and unloading &g

Re: Problem with Dynamically unloading a module

2009-12-23 Thread Steven D'Aprano
On Wed, 23 Dec 2009 15:18:10 -0800, Carl Banks wrote: >> > # will unload mod1 assuming mod1 was the only reference to that >> > module. >> >> Which is highly unlikely. Any classes or functions from the module will >> keep the module alive. > > Actually, they won't. Neither classes nor functions

Re: Problem with Dynamically unloading a module

2009-12-23 Thread lordofcode
Hi All, Thanks you all for your replies ,cleared quiet a few doubts about importing modules and namespace references . Currently am going with static importing of all modules. But it may not be efficient in future as the number of interchangeable modules that I have to import may run in 30-40's.(B

Re: Problem with Dynamically unloading a module

2009-12-23 Thread Carl Banks
On Dec 23, 7:40 am, Steven D'Aprano wrote: > On Wed, 23 Dec 2009 13:37:06 +0100, Jean-Michel Pichavant wrote: > > 3/ if you really need to unload the previous module, it's a little bit > > tedious. > > > import mod1 > > del mod1 > > sys.modules['mod1'] = None > > Assigning sys.modules[name] to Non

Re: Problem with Dynamically unloading a module

2009-12-23 Thread Steven D'Aprano
On Wed, 23 Dec 2009 15:31:53 +0100, Jean-Michel Pichavant wrote: > Steven D'Aprano wrote: >> On Wed, 23 Dec 2009 13:37:06 +0100, Jean-Michel Pichavant wrote: >> >> >>> But believe me, you don't want to mess up with the python import >>> mechanism. >>> >>> >> Unless you understand how it

Re: Problem with Dynamically unloading a module

2009-12-23 Thread Jean-Michel Pichavant
Steven D'Aprano wrote: On Wed, 23 Dec 2009 13:37:06 +0100, Jean-Michel Pichavant wrote: 3/ if you really need to unload the previous module, it's a little bit tedious. import mod1 del mod1 sys.modules['mod1'] = None Assigning sys.modules[name] to None is not the same as deleting the

Re: Problem with Dynamically unloading a module

2009-12-23 Thread Jean-Michel Pichavant
Steven D'Aprano wrote: On Wed, 23 Dec 2009 13:37:06 +0100, Jean-Michel Pichavant wrote: But believe me, you don't want to mess up with the python import mechanism. Unless you understand how it works. Let me quote the OP: 'Not an expert in Python' I was just answering the OP ques

Re: Problem with Dynamically unloading a module

2009-12-23 Thread Steven D'Aprano
On Wed, 23 Dec 2009 01:41:27 -0800, lordofcode wrote: > I need to dynamically load a module and unload it and load another > module. Why bother unloading it? Unless you're programming for an embedded device, it's highly unlikely that you're so strapped for memory that a module will make any rea

Re: Problem with Dynamically unloading a module

2009-12-23 Thread Steven D'Aprano
On Wed, 23 Dec 2009 13:37:06 +0100, Jean-Michel Pichavant wrote: > 3/ if you really need to unload the previous module, it's a little bit > tedious. > > import mod1 > del mod1 > sys.modules['mod1'] = None Assigning sys.modules[name] to None is not the same as deleting the entry. None has speci

Re: Problem with Dynamically unloading a module

2009-12-23 Thread Jean-Michel Pichavant
Jean-Michel Pichavant wrote: lordofcode wrote: Hi All Not an expert in Python, so sorry if this sounds like a silly question. I went through other few threads in the mailing list but they are not helping me much. I have run into a problem related to dynamically loading and unloading a module

Re: Problem with Dynamically unloading a module

2009-12-23 Thread Jean-Michel Pichavant
lordofcode wrote: Hi All Not an expert in Python, so sorry if this sounds like a silly question. I went through other few threads in the mailing list but they are not helping me much. I have run into a problem related to dynamically loading and unloading a module. I need to dynamically load a

Re: Problem with Dynamically unloading a module

2009-12-23 Thread Lie Ryan
On 12/23/2009 8:41 PM, lordofcode wrote: Hi All Not an expert in Python, so sorry if this sounds like a silly question. I went through other few threads in the mailing list but they are not helping me much. I have run into a problem related to dynamically loading and unloading a module. I need

Problem with Dynamically unloading a module

2009-12-23 Thread lordofcode
Hi All Not an expert in Python, so sorry if this sounds like a silly question. I went through other few threads in the mailing list but they are not helping me much. I have run into a problem related to dynamically loading and unloading a module. I need to dynamically load a module and unload it

Re: Unloading a module

2009-10-22 Thread Gabriel Genellina
En Thu, 22 Oct 2009 11:59:58 -0300, lallous escribió: If a reference to an imported module reaches zero will Python cleanup everything related to that module and unload the compiled code, etc, etc...? The module object itself (as any other object whose reference count reaches zero) will be

Unloading a module

2009-10-22 Thread lallous
Hello Group, If a reference to an imported module reaches zero will Python cleanup everything related to that module and unload the compiled code, etc, etc...? For example: import sys m = [__import__(str(x)) for x in xrange(1,4)] del sys.modules['1'] del m[0] print m Is module['1'] really un