4

I have a modules.py file :

global dns_server_ip
def SetVnetGlobalParameters():
    dns_server_ip = '192.168.3.120'

And I’m importing this file in say abc.py file

from modules import *
SetVnetGlobalParameters()
print(dns_server_ip)

But ‘dns_server_ip’ is still not accessible.

I want to set global parameters through Function only. Any help would be greatly appreciated! Thanks..

2
  • Easily fixed with Zangetsu anwer, but why not just make it a module-level attribute? Commented Apr 18, 2013 at 6:04
  • 2
    Also, make sure you don't have that character in your source code but an ' instead. Commented Apr 18, 2013 at 6:06

2 Answers 2

10

As per your question I understand you are the beginner to the python.

While importing the modules you have use just module name and don't need to include the extension or suffix(py) and in your code you miss the starting single quote .

Here is your modified code: it is modules.py

dns_server_ip = ''
def SetVnetGlobalParameters():
    global dns_server_ip
    dns_server_ip = '192.168.3.120′

Here is your abc.py

import modules 
modules.SetVnetGlobalParameters()
print modules.dns_server_ip

Here through the global keyword we are telling the python interpreter to change or point out the global variable instead of local variable and always the variable would be either global or local If the variable is both (local and global) you will get python UnboundLocalError exception and if you did not put that global keyword

global dns_server_ip

The dns_server_ip will be created as a new local variable . The keyword global intended to with in the functions only

you can check global keyword,python modules

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply. I've made changes in modules.py file. Now it is printing empty string only instead of expected '192.168.3.120'
you please try the my edited version, even I have checked my self It is working as you expected . Here you should not use " from modules import *" instead of that we have to use import modules When we use "from modules import *" all the attributes of the module are imported to the global name space of the current modules where we have imported the other modules content
4

In modules.py

dns_server_ip = None
def SetVnetGlobalParameters():
    global dns_server_ip
    dns_server_ip = '192.168.3.120'

In abc.py

import modules
modules.SetVnetGlobalParameters()
print(modules.dns_server_ip)

2 Comments

Thanks for the reply.. As per your suggestion I've made following changes.. Modules.py- global dns_server_ip def SetVnetGlobalParameters(): dns_server_ip = '192.168.3.120' abc.py- from modules import * SetVnetGlobalParameters() print(dns_server_ip) Still "dns_server_ip" is not accessible. :-(
I have made certain changes, have a look

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.