NameError: name '_name_' is not defined

46,848

Solution 1

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__':

Solution 2

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.

Solution 3

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.

Share:
46,848
Cin Sb Sangpi
Author by

Cin Sb Sangpi

About me:http://en.wikipedia.org/wiki/User:Sbsangpi Visit www.sbsangpi.com (only in Myanmar Language)

Updated on March 25, 2020

Comments

  • Cin Sb Sangpi
    Cin Sb Sangpi about 4 years

    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()