473,574 Members | 2,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Global var access in imported modules?

I have a main module doStuff.py and another module utility.py. At the
start of doStuff.py I call

import utility.py

Then I also proceed to initiallize some global variables

sName = ""

Then I create a class, some methods etc. In one of the methods I assign
a value to my variable sName. Then I call a function from within
my utility.py file:

utility.makeOne (stuff)
Within my utility.py file, I define the makeOne function. But I want to
use that same global variable "sName" In utility.py I have tried to
indicate that I'm using the global "sName" through the statement:

global sName

But when I go to use the variable it still gives me an error:

NameError: global name 'sName' is not defined

I thought perhaps I need to indicate 'globality' in my main module, so
before I initiallized sName in doStuff.py I added:

global sName

But it doesn't help me. I had this issue before and resolved it by
declaring the variable global in the sub-module utility.py, but then I
needed to reference it in my main module with a prefix:

utility.sName = ""

It's more verbose,and defining globals in a submodule seems backward.
But also, what if I need to access "sName" in another imported module,
say "otherstuff.py" ? I would do my "import otherstuff" call in my main
module, but would I have to put an "import utility" into the
otherstuff.py file?

Is there some way I can define globals in my main module, and have them
accessible in all my imported submodule?

As you can see I'm a little unsure about the global handling in a
multi-module environment. Any suggestions appreciated. I've read
http://docs.python.org/ref/naming.html but it hasn't enlightened me on
this one.
Aug 27 '08 #1
4 5985
RgeeK wrote:
I have a main module doStuff.py and another module utility.py. At the
start of doStuff.py I call

import utility.py
that tries to import a module named "py" from the package "utility".
Then I also proceed to initiallize some global variables

sName = ""
Within my utility.py file, I define the makeOne function. But I want to
use that same global variable "sName" In utility.py I have tried to
indicate that I'm using the global "sName" through the statement:

global sName
the "global" directive in Python is used *inside* a function or method
to indicate that a given name is not local.

Python doesn't have "program-wide global" variables; if you need that,
create a support module and import that module everywhere you need to
access those variables:

# file: globalvars.py
sName = ""

# file: myprogram.py
import globalvars
print globalvars.sNam e

etc.

</F>

Aug 27 '08 #2
Rgg
Fredrik Lundh wrote:
> import utility.py
that tries to import a module named "py" from the package "utility".
oops - that was just a typo in my post - I meant of course "import utility"
>
Python doesn't have "program-wide global" variables; if you need that,
create a support module and import that module everywhere you need to
access those variables:

# file: globalvars.py
sName = ""

# file: myprogram.py
import globalvars
print globalvars.sNam e

etc.

</F>
That's news - thanks, I didn't realize that there just wasn't the
concept of program-wide globals. The support module idea sounds like a
path forward.

-R.
Aug 28 '08 #3
Fredrik Lundh wrote:
> import utility.py
that tries to import a module named "py" from the package "utility".
oops - that was just a typo in my post - I meant of course "import utility"
>
Python doesn't have "program-wide global" variables; if you need that,
create a support module and import that module everywhere you need to
access those variables:

# file: globalvars.py
sName = ""

# file: myprogram.py
import globalvars
print globalvars.sNam e

etc.

</F>
That's news - thanks, I didn't realize that there just wasn't the
concept of program-wide globals. The support module idea sounds like a
path forward.

-R.

Aug 28 '08 #4
Dennis Lee Bieber wrote:
On Wed, 27 Aug 2008 16:21:03 -0400, RgeeK <Ro**@no.thanks .spammers>
declaimed the following in comp.lang.pytho n:
>I have a main module doStuff.py and another module utility.py. At the
start of doStuff.py I call

import utility.py

I hope not... import utility no .py
>Then I also proceed to initiallize some global variables
Python does not have global variables. Names belong within a module
(or within functions defined within the module).
>sName = ""

Then I create a class, some methods etc. In one of the methods I assign
a value to my variable sName. Then I call a function from within
my utility.py file:

utility.makeOne (stuff)
Within my utility.py file, I define the makeOne function. But I want to
use that same global variable "sName" In utility.py I have tried to
indicate that I'm using the global "sName" through the statement:

global sName
The global statement is only used within functions (def blocks) to
indicate that "writes" to the specified name are to modify the MODULE
level version of the name, otherwise a write modifies a function local
version of the name (you don't need global for read-only access of
names, the search for names first looks inside the function, then out to
the module)
>But when I go to use the variable it still gives me an error:

NameError: global name 'sName' is not defined

I thought perhaps I need to indicate 'globality' in my main module, so
before I initiallized sName in doStuff.py I added:

global sName

But it doesn't help me. I had this issue before and resolved it by
declaring the variable global in the sub-module utility.py, but then I
needed to reference it in my main module with a prefix:

utility.sName = ""

It's more verbose,and defining globals in a submodule seems backward.
But also, what if I need to access "sName" in another imported module,
say "otherstuff.py" ? I would do my "import otherstuff" call in my main
module, but would I have to put an "import utility" into the
otherstuff.p y file?
If you really need "globals" the common solution is to create a
module such as "myglobals" , define all the shared names within that
module, and import that module where ever you need access to one of the
names. And yes, you will need to qualify all those names with the module
name (though you can do things like:

import myglobals as mg

and then use

mg.somename

instead of

myglobals.somen ame)

Thanks for the reply. Good to see that approach has broad support :)
I'll do that. I like the idea of a nice short alias for the import to
keep the qualifications brief.

Ross.
Aug 28 '08 #5

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

Similar topics

3
1389
by: Jason | last post by:
Hi, I've been having trouble understanding the difference between global namespace within and between modules. For example, I can program a subthread to see a global name (a threaded event to be specific); however, somehow when I break up my code into different modules, the subthread can no longer see the global name in the main program. ...
4
1984
by: Bart | last post by:
Hi all I don't understand globals between multiple modules in a python program. I really don't. I've narrowed it down to the following two very simple programs a.py and b.py. When I run a.py I get the following output: inc: 2 A: 2 inc: 3 B: 3
2
1629
by: P K | last post by:
Hi, I have an Enum which I need to use in many pages and many classes written in asp.net 2.0 I would like to have a central repository for the enum so that changes can be made once. Also I would like a global access to it, meaning any page should be able to access it. How can I achieve this ?
1
1598
by: rodrigostrauss | last post by:
Having the following code: ==== Module1.py ==== import logging def X(): logging.error('test') If I import it into PythonWin console and call X(), the error message is not printed. If I do the same in Python console the message is printed. Do I need to configure the logging module or it's a problem
9
2536
by: Joseph Wakeling | last post by:
Hello all, I sometimes make use of global variables within modules, which I denote with "static" so that they will not be seen outside the module, and will preserve values between module calls. Recently I was looking back at a rather important function and found I'd forgotten to put the static declaration in place. I'm not worried about...
3
1818
by: Mohamed Yousef | last post by:
Hello , The problem I'm asking about is how can imported modules be aware of other imported modules so they don't have to re-import them (avoiding importing problems and Consicing code and imports ) Take Example :- in A.py :- import B print dir() # no problems we can see B which contain re module and C module
0
1018
by: Mohamed Yousef | last post by:
Hello , The problem I'm asking about is how can imported modules be aware of other imported modules so they don't have to re-import them (avoiding importing problems and Consicing code and imports ) Take Example :- in A.py :- import B print dir() # no problems we can see B which contain re module and C module
0
922
by: Terry Reedy | last post by:
Mohamed Yousef wrote: If you want to use module A in both B and C, B and C should both import A. No problem. ??
0
1353
by: Gabriel Genellina | last post by:
En Sun, 24 Aug 2008 07:34:41 -0300, Mohamed Yousef <harrrrpo@gmail.comescribió: Yes. That way, when you see a name "foo" used in a module, you can look at the imports to see where it comes from. (BTW, this is why using "from module import *" is not a good idea) Having a single global namespace is a lot worse. Yes. How often do you...
0
7753
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8095
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8265
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8132
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6500
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5332
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3769
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3787
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2265
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.