Question about namespaces and import. How to avoid calling os.system

P

Pekka Niiranen

Hi there,

I have two scripts. The first "main.py" sets some variables
and then imports another called "gen.py". The idea is to
provide "main.py" that defines some paths, variables etc.
without using Windows environment variables. Various other "hackers"
will make additional Python scripts (subroutines) like "gen.py"
that utilize variables set by the "main.py" and which "main.py" calls.
I can do this with "subprocess" module by setting its env -variable
but I try to avoid calling shell. How can I merge temporary
the namespaces of the two modules?

----- example run STARTS ---
c:\home\pekka>main.py

imported module: <module 'gen' from 'c:\home\gen.py'>
{'todir': 'c:\\'}
Traceback (most recent call last):
File "C:\home\pekka\main.py", line 16, in ?
gencmd.run_gen()
File "c:\home\gen.py", line 7, in run_gen
print env_params # HOW MAKE THIS DICTIONARY FROM main.py VISIBLE
NameError: global name 'env_params' is not defined

----- example run STOPS ---

---- main.py STARTS ----
import os, sys
env_params = {}
env_params['EDITOR'] = "foo"

def import_libs(dir, script):
""" Imports python script"""
sys.path.insert(0,(os.path.normpath(dir)))
module_name, ext = os.path.splitext(script)
my_script = __import__(module_name)
print "\nimported module: %s" % (my_script)
del sys.path[0]
return my_script

if __name__ == "__main__":
gencmd = import_libs("c:\home", "gen.py")
gencmd.run_gen()

---main.py ENDS -----

---- gen.py STARTS ----
# Store this script to directory c:\home"
my_env_params ={}
my_env_params['todir'] = "c:\\"

def run_gen():
# Get commandline arguments
print my_env_params
print env_params # HOW MAKE THIS DICTIONARY FROM main.py VISIBLE

if __name__ == "__main__":
run_gen()

---gen.py ENDS -----


-pekka-
 
J

Jeff Epler

In main.py, execfile("gen.py")

or

In gen.py, have something like
from __main__ import env_params

or

In main.py, have something like
import __builtins__; __builtins__.env_params = env_params

or

call a function in the gen.py with env_params as a parameter
import gen
gen.do(env_params)

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFC4UtJJd01MZaTXX0RAof0AJ9XXxAeMIANlBdMOXYX/guMDYuPAACgnc8P
SX5bkxWZc8Kk/o6Fphx49zk=
=HcTB
-----END PGP SIGNATURE-----
 
P

Pekka Niiranen

Thanks,

I will analyse these 4 options and select the most suitable
since there are other issues involved too, like
"the "main.py" reads contents of a file to a list that gets
passed to the "gen.py" with dictionary "env_params"".
I try to avoid parsing the contents of the file both
in "main.py" and in "gen.py"

-pekka-
 
P

Pekka Niiranen

Thanks,

I will analyse these 4 options and select the most suitable
since there are other issues involved too, like
"the "main.py" reads contents of a file to a list that gets
passed to the "gen.py" with dictionary "env_params"".
I try to avoid parsing the contents of the file both
in "main.py" and in "gen.py"

-pekka-
 
P

Patricia J. Hawkins

PN> Hi there,
PN> I have two scripts. The first "main.py" sets some variables
PN> and then imports another called "gen.py". The idea is to
PN> provide "main.py" that defines some paths, variables etc.
PN> without using Windows environment variables. Various other "hackers"
PN> will make additional Python scripts (subroutines) like "gen.py"
PN> that utilize variables set by the "main.py" and which "main.py" calls.
PN> I can do this with "subprocess" module by setting its env -variable
PN> but I try to avoid calling shell. How can I merge temporary
PN> the namespaces of the two modules?

The built-in execfile should do the trick.
 
D

Dennis Lee Bieber

Hi there,

I have two scripts. The first "main.py" sets some variables
and then imports another called "gen.py". The idea is to
provide "main.py" that defines some paths, variables etc.
without using Windows environment variables. Various other "hackers"
will make additional Python scripts (subroutines) like "gen.py"
that utilize variables set by the "main.py" and which "main.py" calls.
I can do this with "subprocess" module by setting its env -variable
but I try to avoid calling shell. How can I merge temporary
the namespaces of the two modules?
Normally, I'd say that is setting the namespaces upside down...
That is, that the "main" program has access to the namespaces of those
that modules that /it/ imports (and they can access the namespace of
those they import), but imported modules can only access stuff
explicitly passed to them from the main/parent module.

If your main is being used to parse out some data file, and you
then want the parsed data available in randomly imported modules... I'd
make a separate module just for the parser AND parsed data.

In pseudo-code
-=-=-=-=-=-=-=-=-=-
#common.py

a = None
b = None
c = []
d = {}
#etc

def parseFile(fin):
global a, b, c, d
ln = fin.readline()
# do whatever needs to be done to parse the line
# "assigning" the result to a..d whatever

-=-=-=-=-=-=-=-=-=-
#main.py

import common
import other

f = open("data.file", "r")
common.parseFile(f)
#
other.func()
print common.d

-=-=-=-=-=-=-=-=-=-
#other.py
import common

def func():
print common.a
print common.b
for i in range(len(common.c)):
common.d = common.c

-=-=-=-=-=-=-=-=-=-=-

DO NOT use
from common import *
since you want all modules to reference the /same/ "common" namespace.

Or, to use your example code (modified).

-=-=-=-=-=-=-=-=-
#common.py
pass # Basically just an empty module for the creation of a namespace

---- main.py STARTS ----
import os, sys

import common #############

common.env_params = {} #######
common.env_params['EDITOR'] = "foo" #######

def import_libs(dir, script):
""" Imports python script"""
sys.path.insert(0,(os.path.normpath(dir)))
module_name, ext = os.path.splitext(script)
my_script = __import__(module_name)
print "\nimported module: %s" % (my_script)
del sys.path[0]
return my_script

if __name__ == "__main__":
gencmd = import_libs("c:\home", "gen.py")
gencmd.run_gen()

---main.py ENDS -----

---- gen.py STARTS ----
# Store this script to directory c:\home"

import common #########

my_env_params ={}
my_env_params['todir'] = "c:\\"

def run_gen():
# Get commandline arguments
print my_env_params
print common.env_params ############
# HOW MAKE THIS DICTIONARY FROM main.py VISIBLE

if __name__ == "__main__":
run_gen()

---gen.py ENDS -----

Should be all that is needed (Please note, I did not TRY this)
--
 

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,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top