473,382 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Why I need to declare import as global in function

I have a stange side effect in my project :

in my project I need to write "gobal" to use global symbol :

....
import math
....
def f() :
global math # necessary ?????? else next line generate an error
message ?????
print math.pi

(the problem is for all global module symbol)

I have certainly change somthing in my project, but I can't find what ?

(just a small program wort fine without this global of course)

Can anybody help me : where can I search the mistake in my project ?

Nov 28 '05 #1
16 3079
Sounds like something, either in your program, in another lib you
imported, or perhaps some extension you recently installed (and which
automatically starts), overrides 'import' (replaces it with it's own
version) -- and forgets to add the imported modules properly to the
globlals?
Or something, some option, that perhaps changes the way that Python
recognizes globals?

If you declare another global variable, then try to use it in your
function, then what's the result?

What Python version do you use?

cheers,

--Tim

Nov 28 '05 #2
di*************@gmail.com wrote:
I have a stange side effect in my project :

in my project I need to write "gobal" to use global symbol :

...
import math
...
def f() :
global math # necessary ?????? else next line generate an error
message ?????
what error message?
print math.pi


you only need global in this case if you assign to the name somewhere
later in the function (e.g. "math = ..." or "import math" or some other
assignment)

please post the error message (the entire traceback).

</F>

Nov 28 '05 #3
the error message :

EXCEPTION RAISED::

Traceback (most recent call last):
File "../tu.py", line 21, in run_tu
execfile( filename )
File "TU_05_010.py", line 8, in ?
import TU_05_tools
File "./TU_05_tools.py", line 4, in ?
f()
File "./TU_05_tools.py", line 2, in f
print math.pi
NameError: global name 'math' is not defined

I have remarq that this problem is raised when I execute code in an
imported module (during importation)

I think I will be able to isolate it and have a simple sample soon ....

Nov 29 '05 #4
wrote:
I have remarq that this problem is raised when I execute code in an
imported module (during importation)

I think I will be able to isolate it and have a simple sample soon ....

Meanwhile, try adding:

import math

to the top of TU_05_tools.py.
Nov 29 '05 #5
di*************@gmail.com wrote:
the error message :

EXCEPTION RAISED::

Traceback (most recent call last):
File "../tu.py", line 21, in run_tu
execfile( filename )
File "TU_05_010.py", line 8, in ?
import TU_05_tools
File "./TU_05_tools.py", line 4, in ?
f()
File "./TU_05_tools.py", line 2, in f
print math.pi
NameError: global name 'math' is not defined

I have remarq that this problem is raised when I execute code in an
imported module (during importation)

I think I will be able to isolate it and have a simple sample soon ....


Random guess: change the execfile() call to

execfile(filename, globals())

or

exec file(filename).read()

If one of the above works, a minimal example of what happens could be

file("tmp.py", "w").write("""
import math
""")

def f():
execfile("tmp.py")
print locals()["math"].pi # 3.14159265359
print math.pi # Error, math looked up in the global
namespace

f()

execfile() puts symbols into the local namespace but keeps the compiler
clueless because it's just an ordinary function, whereas exec triggers the
generation of slightly different bytecode for the enclosing function.

Peter

Nov 29 '05 #6
Peter Otten wrote:
Traceback (most recent call last):
File "../tu.py", line 21, in run_tu
execfile( filename )
File "TU_05_010.py", line 8, in ?
import TU_05_tools
File "./TU_05_tools.py", line 4, in ?
f()
File "./TU_05_tools.py", line 2, in f
print math.pi
NameError: global name 'math' is not defined

I have remarq that this problem is raised when I execute code in an
imported module (during importation)

I think I will be able to isolate it and have a simple sample soon ....


Random guess: change the execfile() call to

execfile(filename, globals())

or

exec file(filename).read()


That is unlikely to help. The execfile target seems to have been
TU_05_010.py, but the file which cannot access math is TU_05_tools.py
accessed by a normal import, so adding some globals to the execfile call
won't really do anything useful.

Isn't it fun trying to guess the problem in the absence of the code?
Nov 29 '05 #7
Duncan Booth wrote:
Isn't it fun trying to guess the problem in the absence of the code?


What other reason could there be to forego the sane approach -- stick
'import math' everywhere it might belong?
Those exec/execfile() peculiarities are so much more interesting ;-)

Peter

Nov 29 '05 #8
Duncan Booth wrote:
That is unlikely to help. The execfile target seems to have been
TU_05_010.py, but the file which cannot access math is TU_05_tools.py
accessed by a normal import, so adding some globals to the execfile call
won't really do anything useful.

Isn't it fun trying to guess the problem in the absence of the code?


given that the NameError occurs on line 2 of the file, inside a function, this
is probably just a misunderstanding of how namespaces work in Python...

</F>

Nov 29 '05 #9
You're right, the problem is around the usage of "execfile".

But I have still difficulties to get a simple sample.... and have no
enough time to work on it until end of week.

I will post if I resolve my problem or if I can get a simple sample.

Nov 29 '05 #10
I think I understand my problem, but first the sample code extracted to
my project.

Rq : it's an automatic run of unitary test, the names of unitary test
are parameter of main program "imported" file via the "execfile"
function (an usage of "import" instead may be a solution....) :

file run.py :
----------------

def run_ut( test ) :
# to have the problem the execfile MUST be in a function
execfile( test )
run_ut( "ut_00.py" )
file ut_00.py :
--------------------
import math

def f() :
## global math # <-- just decomment this line to avoid error
print "Second access :"
print "\t",math.pi # ERROR

print "First access :"
print "\t",math.pi # OK
f()

Nov 30 '05 #11
lot's of solutions proposed in the discussion works fine :

file run.py :
----------------
def run_ut( test ) :
# to have the problem the execfile MUST be in a function
# execfile( test ) # ERROR
execfile( test, globals() ) # OK
exec "import ut_00" # OK
exec file(test).read() # OK
run_ut( "ut_00.py" )

Thanks a lor

Nov 30 '05 #12
yes I have imported math in the file I want to use it. But the imported
module "math" is not visible in function without a global instruction.

But the solutions already proposed seems to work file for my sample
program. I will try on my project soon :)

Nov 30 '05 #13
sample and solution posted in another branch of this thread....

Nov 30 '05 #14
di*************@gmail.com wrote:
I think I understand my problem, but first the sample code extracted to
my project.

Rq : it's an automatic run of unitary test, the names of unitary test
are parameter of main program "imported" file via the "execfile"
function (an usage of "import" instead may be a solution....) :

file run.py :
----------------

def run_ut( test ) :
# to have the problem the execfile MUST be in a function
execfile( test )
run_ut( "ut_00.py" )
file ut_00.py :
--------------------
import math

def f() :
## global math # <-- just decomment this line to avoid error
print "Second access :"
print "\t",math.pi # ERROR

print "First access :"
print "\t",math.pi # OK
f()


How interesting. Can anyone actually explain the behaviour here?

Without the global statement everything works as I expect: the 'import
math' is executed in the local scope of run_ut, so the function f() doesn't
find it either in its local scope nor in global scope.

Inserting 'global math' at the outer level in ut_00.py (e.g. before the
'import math') also works as I expect: math now becomes a global variable
and is visible to f().

What I really don't understand is why the global statement *inside* f
affects the scope of math outside f. If we had nested functions then a
global variable in an inner function doesn't affect scope in outer
functions. I realise that f() here isn't a nested function, but even so it
looks to me like a bug unless I'm missing something.

(Technically the program is invoking undefined behaviour as the language
reference says that names listed in a global statement must not be defined
in an import statement, but that isn't really relevant since you can
replace the import with an assignment statement and get the same weird
behaviour.)
Nov 30 '05 #15
Dennis Lee Bieber <wl*****@ix.netcom.com> wrote in
news:tm********************************@4ax.com:
On 30 Nov 2005 00:58:45 -0800, di*************@gmail.com
declaimed the following in comp.lang.python:
yes I have imported math in the file I want to use it. But the
imported module "math" is not visible in function without a
global instruction.
The code you just posted shows it, yes... My reading of an
earlier
message seemed to imply you had a nested import chain with the
"import math" at the wrong level... Something like:

#file 1
execfile("file 2")

#file 2
import math
import file_3

#file_3
print math.pi
But the solutions already proposed seems to work file for my
sample program. I will try on my project soon :)


Looking at the documentation for "execfile", I can see
/how/ the
problem occurs -- but can't determine if this can be considered
"expected".

-=-=-=-=-=-=-
execfile( filename[, globals[, locals]])

This function is similar to the exec statement, but parses a
file instead of a string. It is different from the import
statement in that it does not use the module administration --
it reads the file unconditionally and does not create a new
module.2.2 -=-=-=-=-=-=-

I'm guessing that the intent was only that "file 1" not
become an
entry on the module list, but it seems to be applying "...not
create a new module" recursively to the imported "math"... Maybe
an expert with the Python byte-code can verify. My hypothesis is
something on the order of:
Outer (file level) references to math (math.pi) are being
handled
during the byte-code compilation phase of execfile, so even if
"math" isn't in the module list, it is still in local scope for
the outer math.pi...

Inner (function) references to math become dynamic look-ups
evaluated at function execution; at that point math is not in
scope and is not in the module list.

Interestingly, I note that is the file calling execfile has
imported
math (so math is in the module list), the called file works...

#no import in run
E:\UserData\Dennis Lee Bieber\My Documents\Python Progs>python
run.py in module: <module 'math' (built-in)>
Traceback (most recent call last):
File "run.py", line 5, in ?
run_ut("ut_00.py")
File "run.py", line 3, in run_ut
execfile(test)
File "ut_00.py", line 8, in ?
f()
File "ut_00.py", line 5, in f
print "in function: \t %s" % math
NameError: global name 'math' is not defined

#import is in run
E:\UserData\Dennis Lee Bieber\My Documents\Python Progs>python
run.py <module 'math' (built-in)>
<module 'math' (built-in)>
in module: <module 'math' (built-in)>
in function: <module 'math' (built-in)>


This may be saying what you said, but it seems to depend on where
the import occurs:
# File: runt2.py
def Bobo():
import math
execfile( "ut_00.py" )
b = Bobo()
runt2 First access :
3.14159265359
Second access :
Traceback (most recent call last):
File "D:\pyWork\test\runt2.py", line 5, in ?
b = Bobo()
File "D:\pyWork\test\runt2.py", line 3, in Bobo
execfile( "ut_00.py" )
File "ut_00.py", line 10, in ?
f()
File "ut_00.py", line 6, in f
print "\t",math.pi # ERROR
NameError: global name 'math' is not defined

# File: runt.py
import math
def Bobo():
execfile( "ut_00.py" )
b = Bobo()
runt

First access :
3.14159265359
Second access :
3.14159265359

So the import at the module level of the caller works, but an
import at the function level of the caller doesn't. I don't see why
the namespace would be significantly different by the time execfile
is executed, though it apparently is.

--
rzed
Nov 30 '05 #16
Dennis Lee Bieber wrote:
But the solutions already proposed seems to work file for my sample
program. I will try on my project soon :)


Looking at the documentation for "execfile", I can see /how/ the
problem occurs -- but can't determine if this can be considered
"expected".

-=-=-=-=-=-=-
execfile( filename[, globals[, locals]])

This function is similar to the exec statement, but parses a file
instead of a string. It is different from the import statement in that
it does not use the module administration -- it reads the file
unconditionally and does not create a new module.2.2
-=-=-=-=-=-=-

I'm guessing that the intent was only that "file 1" not become an
entry on the module list, but it seems to be applying "...not create a
new module" recursively to the imported "math"... Maybe an expert with
the Python byte-code can verify. My hypothesis is something on the order
of:
Outer (file level) references to math (math.pi) are being handled
during the byte-code compilation phase of execfile, so even if "math"
isn't in the module list, it is still in local scope for the outer
math.pi...


the byte code is identical for both cases; the only difference I can see is
that when when you compile a function that contains a global statement,
the corresponding name is added to the globals for the function that does
the compilation (run_ut in this case).

if you don't use globals, or if the "global" name doesn't exist in the module
namespace, this doesn't happen.

hmm. puzzling.

</F>

Nov 30 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: dag4004 | last post by:
Hello, I have a strange behavior with global and module. Here is the module named: foo.py bar = "egg"
10
by: Jeff Wagner | last post by:
I am in the process of learning Python (obsessively so). I've been through a few tutorials and read a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting...
3
by: Brad Clements | last post by:
I was going to file this as a bug in the tracker, but maybe it's not really a bug. Poor Python code does what I consider to be unexpected. What's your opinion? With Python 2.3.2 (but also...
6
by: rick | last post by:
Noob problem. I prefer to keep all my scripts in an external '.js' file. I am currently loading the external '.js' file from the header. Problem is I would like to declare a global variable in the...
1
by: RJ Web | last post by:
I have a DOS program that needs to run in a Full Screen DOS window when running on WIN2000. I have not discoverd how to set up the call such that WIN2000 switches to full screen mode, as if I...
8
by: yinglcs | last post by:
Hi, I have the following code: colorIndex = 0; def test(): print colorIndex; This won't work. But it works if i do this:
4
by: =?Utf-8?B?UHVjY2E=?= | last post by:
The function that I'm trying to call through DLLImport has a parameter that has a C code's vector's Itrator to a structure. I Have marshalled the structure in C# but how do I do the C type...
7
by: bambam | last post by:
import works in the main section of the module, but does not work as I hoped when run inside a function. That is, the modules import correctly, but are not visible to the enclosing (global)...
6
by: zaina | last post by:
hi everybody i am nwebie in this forum but i think it is useful for me and the member are helpful my project is about connecting client with the server to start exchanging messages between...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.