some import / namespace questions

S

stef mientki

hello,

I'm working on a rather strange program,
that is partially build dynamically,
and where users can create plugins,
without knowing any of the details of the core program.

Of course this leads to some weird bugs,
so one of the things I want to view/log is the order (and even better
the way) modules are imported.
For now I've put a print statement in every module.
But is there a more elegant way to see when (how, by which module ) a
module is imported ?

The main plugin of the user (with a fixed name, let's say
"Main_User.py") has a an (almost) infinite loop,
which can be aborted by raising an exception.
Now I have to make a few globals from this user module,
available to other existing modules.
I already created a list of these globals.
Now it's not possible to import in the existing modules the main plugin,
like
from Main_User import *
because this will start the infinite loop.
Is there a way of getting the list of collected globals from Main_User
to the existing modules,
without the standard import statement shown above ?

thanks,
Stef Mientki
 
M

Marc 'BlackJack' Rintsch

Now it's not possible to import in the existing modules the main plugin,
like
from Main_User import *
because this will start the infinite loop.

Which means there's code at the module level. Tell the users to put that
into a function, then you can import the module without executing this
main loop.

And don't do a * import because your users might use names that the
importing module uses already and which would get replaced by the * import.
Is there a way of getting the list of collected globals from Main_User
to the existing modules,
without the standard import statement shown above ?

No. In order to get them the module must be imported. Otherwise the
names and the objects they refer to don't exist.
 
S

stef mientki

Marc said:
Which means there's code at the module level. yes
Tell the users to put that
into a function, then you can import the module without executing this
main loop.
ok, I'll do that for the user (the user writes in another language),
and indeed it works,
but has one disadavantage ... see below
And don't do a * import because your users might use names that the
importing module uses already and which would get replaced by the * import.
Yes that's a principal problem with this program, for which I've no
(simple) solution.
I'm simulating a microprocessor language here, where "globality" is an
essential part of the language.
So I'll have to stick to import *, (although I'm aware it's not a good
programming method, especially in user plug-ins)
and now the problem is that I get a warning that import * is only
allowed at the module level :-(
But despite the warning it's still working. ;-)

cheers,
Stef Mientki
 
S

Steve Holden

stef said:
ok, I'll do that for the user (the user writes in another language),
and indeed it works,
but has one disadavantage ... see below
Yes that's a principal problem with this program, for which I've no
(simple) solution.
I'm simulating a microprocessor language here, where "globality" is an
essential part of the language.
So I'll have to stick to import *, (although I'm aware it's not a good
programming method, especially in user plug-ins)
and now the problem is that I get a warning that import * is only
allowed at the module level :-(
But despite the warning it's still working. ;-)
If you read the docs on the warnings module you should be able to
suppress that message.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
 
G

Graham Dumpleton

hello,

I'm working on a rather strange program,
that is partially build dynamically,
and where users can create plugins,
without knowing any of the details of the core program.

Of course this leads to some weird bugs,
so one of the things I want to view/log is the order (and even better
the way) modules are imported.

Set and export the environment variable:

PYTHONVERBOSE=1

in your environment before you run Python. This will generate lots of
internal information about what Python is up to, including details
about module importing, where it is searching for modules etc etc. For
example:
# /System/Library/Frameworks/Python.framework/Versions/2.3/lib/
python2.3/socket.pyc matches /System/Library/Frameworks/
Python.framework/Versions/2.3/lib/python2.3/socket.py
import socket # precompiled from /System/Library/Frameworks/
Python.framework/Versions/2.3/lib/python2.3/socket.pyc
dlopen("/System/Library/Frameworks/Python.framework/Versions/2.3/lib/
python2.3/lib-dynload/_socket.so", 2);
import _socket # dynamically loaded from /System/Library/Frameworks/
Python.framework/Versions/2.3/lib/python2.3/lib-dynload/_socket.so
dlopen("/System/Library/Frameworks/Python.framework/Versions/2.3/lib/
python2.3/lib-dynload/_ssl.so", 2);
import _ssl # dynamically loaded from /System/Library/Frameworks/
Python.framework/Versions/2.3/lib/python2.3/lib-dynload/_ssl.so
import errno # builtin

Output goes to standard error. There can be quite a lot, so you may
want to redirect standard error to a file.

Graham
 

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

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top