views:

87

answers:

3

hello all

i have a module "B", i want to run it from a script "C", and i want to call global variables in "B", as they wer in "C" root, another problem that is if i imported sys in "B" when i run "C" it doesnt see sys

# NameError: global name 'sys' is not defined #

what shall i do?

+4  A: 

When you import a module B (like import B), every line in B will be interpreted. I assume this is what you mean when you say you want to run it. To reference members in B's namespace, you can get them like:

B.something_defined_in_B.

If you wish to use sys explicitly in C, you will need to import it within C as well.

danben
A: 

In C:

import B
B.loadGlobal(globals())

Where loadGlobal is a function that iterates all the variable you want as global:

def loadGlobal(g):
     g['sys'] = sys
Xavier
+1  A: 

is it in your PYTHON_PATH?

if not, in script C's init.py

import os, sys
sys.path.append('/PATH/TO/MODULE/B')

then, in module C

from B import *
something_defined_in_B()
neoprolog