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..

ShitalSavekar
  • 379
  • 2
  • 4
  • 10
  • Easily fixed with Zangetsu anwer, but why not just make it a module-level attribute? – wim Apr 18 '13 at 06:04
  • 2
    Also, make sure you don't have that `’` character in your source code but an `'` instead. – wim Apr 18 '13 at 06:06

2 Answers2

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

Community
  • 1
  • 1
neotam
  • 2,611
  • 1
  • 31
  • 53
  • 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' – ShitalSavekar Apr 18 '13 at 06:44
  • 1
    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 – neotam Apr 18 '13 at 09:08
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)
Zangetsu
  • 1,900
  • 17
  • 25
  • 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. :-( – ShitalSavekar Apr 18 '13 at 06:35
  • I have made certain changes, have a look – Zangetsu Apr 18 '13 at 07:20