5

I have gone through the similar question in stackoverflow but couldn't find the answer close to my problem. In the code below the 3 line before the last line is giving Error -

NameError: name '_name_' is not defined

I have copied the below code from the University Lab guide instruction. Not really sure, how the code is working. We were just told to copy and paste for this lab and see the result. However, We have to type all the code into the command line and I am stuck. How could I fix this error in the code?

#!/usr/bin/python
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.util import dumpNodeConnections
from mininet.log import setLogLevel
class SingleSwitchTopo(Topo):
    “Single switch connected to n hosts.”
def_init_(self,n=2,**opts):
#initialize topology and default options
      Topo._init_(self,**opts)
      switch=self.addSwitch(‘s1’)
#Python’s range(N) generates 0..N-1
      for h in range(n):
           host=self.addHost(‘h%s’%(h+1))
           self.addLink(host,switch)
def simpleTest():
     “Create and test a simple network”
      topo=SingleSwitchTopo(n=4)
      net=Mininet(topo)
      net.start
      print “Dumping host connections”
      dumpNodeConnections(net.hosts)
      print “Testing network connectivity”
      net.pingAll()
     net.stop()
if _name_==’_main_’:
     #Tell mininet to print useful information
     setLogLevel(‘info’)
     simpleTest()
Cin Sb Sangpi
  • 687
  • 3
  • 7
  • 22

3 Answers3

19

The issue is in line -

if _name_==’_main_’:

My guess is you have that line so that the code only runs when run as a script, and not when importing, if so, you need double underscore on both sides of name as well as main. And looks like the quotes are wrong, you need to use ' . Example -

if __name__=='__main__':
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • I not sure two code you have answered, doesn't look different to me, apart from the look! Isn't it there only ' key in keyboard? sorry...I didnt get it.thx – Cin Sb Sangpi Aug 12 '15 at 05:29
  • well you can see how SO is formatting them, might be some issue when you copied the code to SO , if you are actually not getting any error for that in your python code, then do not mind it. It can cause some issues when using editors that support other encodings , and your script is defined in some encoding like utf-8 or so. – Anand S Kumar Aug 12 '15 at 05:32
  • 3
    The key for the single or double quote on your keyboard is interpreted by the program into which you are typing. A Word processor is likely to interpret it as a curly single quote (curling opposite directions at beginning and end of a word.) These are different characters and will produce errors if copied from a WYSIWYG word processor into a programming environment. Even so, I suspect your error is the single underscore instead of double underscore on either side of __name__. – Bennett Brown Aug 12 '15 at 05:50
7

You need two underscores instead of one:

if __name__ == "__main__"

Python executes that directly. If its left out it will execute all the code from the 0th level of indention.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
zxvn
  • 289
  • 2
  • 3
  • 2
    *Python executes that directly. If its left out it will execute all the code from the 0th level of indention.* is wrong. Python executes everything directly from 0th level indentation, when importing a module, the `__name__` is set to the module name, when running the python code as a script using `python .py` `__name__` is set to `__main__` . – Anand S Kumar Aug 12 '15 at 05:47
  • it should be only ' quotation, not " for main. – Cin Sb Sangpi Aug 12 '15 at 05:51
  • 2
    @Sb Sangpi: That won't make any difference. – Matthias Aug 12 '15 at 06:57
4

In the

if _name_==’_main_’:

you haven't written the code by using double underscore, it should be like following

if __name__==’__main__’:

thank you for above answer, they pointed the error for me yet it was not clear. I have make it clear by answering my own question.

Cin Sb Sangpi
  • 687
  • 3
  • 7
  • 22