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

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

P: n/a
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-
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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-----

Jul 22 '05 #2

P: n/a
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-
Jeff Epler wrote: 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

Jul 22 '05 #3

P: n/a
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-
Jeff Epler wrote: 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

Jul 22 '05 #4

P: n/a
>>>>> "PN" == Pekka Niiranen <pe************@wlanmail.com> writes:

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.

--
Patricia J. Hawkins
Hawkins Internet Applications
www.hawkinsia.com
Jul 23 '05 #5

P: n/a
On Fri, 22 Jul 2005 22:24:18 +0300, Pekka Niiranen
<pe************@wlanmail.com> declaimed the following in
comp.lang.python:
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[i] = common.c[i]

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

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)
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.