Question about import and namespace

J

jdemoor

Hi,

I'm new to Python and have the following problem :
I have an application started by a main.py file, which does a ' from
module_1 import * '.
main.py is responsible from the creation of an object which is then
used in module_1.
What is the best way to make that object visible in the module_1
namespace ?
I guess that if I do an ' import module_1 ', I can make the object
visible with ' module_1.myObject = myObject ', but that won't work with
the from ... import * statement.

Thanks in advance for your help.
 
M

Marc 'BlackJack' Rintsch

I have an application started by a main.py file, which does a ' from
module_1 import * '.
main.py is responsible from the creation of an object which is then
used in module_1.
What is the best way to make that object visible in the module_1
namespace ?
I guess that if I do an ' import module_1 ', I can make the object
visible with ' module_1.myObject = myObject ', but that won't work with
the from ... import * statement.

Give the object as argument to functions in `module_1` is a clean solution.

Ciao,
Marc 'BlackJack' Rintsch
 
P

Peter Otten

I'm new to Python and have the following problem :
I have an application started by a main.py file, which does a ' from
module_1 import * '.
main.py is responsible from the creation of an object which is then
used in module_1.
What is the best way to make that object visible in the module_1
namespace ?
I guess that if I do an ' import module_1 ', I can make the object
visible with ' module_1.myObject = myObject ', but that won't work with
the from ... import * statement.

You can do both

from module import *
import module

as these kinds of import are not mutually exclusive.
But I recommend that you restructure your modules to avoid cyclic
dependencies.

Peter
 
J

jdemoor

Thanks for the replies.
You can do both

from module import *
import module

as these kinds of import are not mutually exclusive.

Would this run the code in 'module' twice, or just make the objects in
it accessible by several names ?
 
P

Peter Otten

Thanks for the replies.


Would this run the code in 'module' twice, or just make the objects in
it accessible by several names ?

The latter. But why don't you try it yourself by putting a

print "importing module" # you will see that once

statement in the module?

There is one notable exception, the application's main module:

$ cat main.py
import main
print "importing main as", __name__
$ python main.py
importing main as main
importing main as __main__

Peter
 
M

Marc 'BlackJack' Rintsch

Would this run the code in 'module' twice, or just make the objects in
it accessible by several names ?

The code at module level is only executed at first import.

Ciao,
Marc 'BlackJack' Rintsch
 

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,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top