NameError: name '_name_' is not defined [closed]
Asked Answered
S

3

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()
Synovitis answered 12/8, 2015 at 5:19 Comment(3)
Please indent the program correctly.Unearthly
Also you should write the error as it is. It probably says NameError: name '_name_' is not defined.Merna
You are using single underscores _name_ and '_main_'. You should be using double underscores: __name__ and '__main__'Stigmatic
U
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__':
Unearthly answered 12/8, 2015 at 5:23 Comment(3)
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.thxSynovitis
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.Unearthly
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.Cf
P
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.

Poisson answered 12/8, 2015 at 5:44 Comment(3)
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 <sript>.py __name__ is set to __main__ .Unearthly
it should be only ' quotation, not " for main.Synovitis
@Sb Sangpi: That won't make any difference.Quinquevalent
S
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.

Synovitis answered 12/8, 2015 at 5:39 Comment(4)
Don't add your own answer, accept the one that told you this. Note that you also have e.g. _init_ with exactly the same problem.V1
Perfect answer , I too had the same issue and after adding one more underscore. It got resolved.Forspent
@V1 you are correct, dont add your own answer,to your questionPennipennie
@RohanDevaki Well, not in general. It's fine to answer your own question, but it's not fine to duplicate an answer.Masochism

© 2022 - 2024 — McMap. All rights reserved.