Questions tagged [nameerror]

The Ruby or Python `NameError` exception.

In Ruby, NameError exceptions are raised when a given name is invalid or undefined. For example:

puts pancakes

will raise a NameError if there is no pancakes method or variable. You will also get a NameError if you try to define a constant whose first letter is not a capital:

SomeModule.const_set :pancakes, 11

In Python, a NameError is raised when:

... a local or global name is not found. This applies only to unqualified names. The associated value is an error message that includes the name that could not be found.

An example is below in which a NameError is raised because x is not defined in the current scope:

>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>>
1276 questions
269
votes
15 answers

input() error - NameError: name '...' is not defined

I am getting an error when I try to run this simple script: input_variable = input("Enter your name: ") print("your name is" + input_variable) Let's say I type in "dude", the error I am getting is: line 1, in input_variable =…
chillpenguin
  • 2,989
  • 2
  • 15
  • 18
185
votes
7 answers

NameError: global name 'unicode' is not defined - in Python 3

I am trying to use a Python package called bidi. In a module in this package (algorithm.py) there are some lines that give me error, although it is part of the package. Here are the lines: # utf-8 ? we need unicode if isinstance(unicode_or_str,…
TJ1
  • 7,578
  • 19
  • 76
  • 119
185
votes
3 answers

NameError: name 'self' is not defined

Why such structure class A: def __init__(self, a): self.a = a def p(self, b=self.a): print b gives an error NameError: name 'self' is not defined?
chriss
  • 4,349
  • 8
  • 29
  • 36
160
votes
14 answers

python NameError: global name '__file__' is not defined

When I run this code in python 2.7, I get this error: Traceback (most recent call last): File "C:\Python26\Lib\site-packages\pyutilib.subprocess-3.5.4\setup.py", line 30, in long_description = read('README.txt'), File…
Kaibing Yang
  • 1,603
  • 2
  • 12
  • 5
118
votes
1 answer

Python NameError: name 'include' is not defined

I'm currently developing a website with the framework Django (I'm very beginner), but I have a problem with Python: since I have created my templates, I can't run server anymore for this reason (the stack trace points to a line in file…
eloiletagant
  • 1,351
  • 2
  • 8
  • 13
77
votes
1 answer

NameError: global name 'long' is not defined

I have a Python version 3.3.0 and I am not sure why it does not let me do long for b and m here... I tried to look up the answers on here and but nothing helped...thanks im getting an error saying NameError: global name 'long' is not defined power…
Manual
  • 1,627
  • 5
  • 17
  • 20
72
votes
4 answers

Python NameError: name is not defined

I have a python script and I am receiving the following error: Traceback (most recent call last): File "C:\Users\Tim\Desktop\pop-erp\test.py", line 1, in s = Something() NameError: name 'Something' is not defined Here is the code…
user1899679
  • 839
  • 1
  • 6
  • 4
43
votes
1 answer

python: NameError:global name '...‘ is not defined

in my code, I have: class A: def a(): ...... def b(): a() ...... b() Then the compiler will say "NameError: global name a() is not defined." If I pull all the stuffs out of the class A, it would be no problem,…
Robert
  • 2,189
  • 6
  • 31
  • 38
32
votes
2 answers

function name is undefined in python class

I am relatively new to python and i am experiencing some issues with namespacing. class a: def abc(self): print "haha" def test(self): abc() b = a() b.test() #throws an error of abc is not defined. cannot explain why is…
aceminer
  • 4,089
  • 9
  • 56
  • 104
31
votes
3 answers

iPython debugger raises `NameError: name ... is not defined`

I can't understand the following exception that is raised in this Python debugger session: (Pdb) p [move for move in move_values if move[0] == max_value] *** NameError: name 'max_value' is not defined (Pdb) [move for move in move_values] [(0.5, (0,…
Bill
  • 10,323
  • 10
  • 62
  • 85
31
votes
1 answer

Python: NameError: global name 'foobar' is not defined

I have written the following class: class myClass(object): def __init__(self): pass def foo(self, arg1, arg2): pp = foobar(self, arg1, arg2) if pp: return 42 else return -666 def…
skyeagle
  • 3,211
  • 11
  • 39
  • 41
29
votes
3 answers

openAi-gym NameError

I am trying to use the famous 'Gym' module from OpenAI on WSL and executing code on python 3.5.2. When I try to run an environment as explained here, using the code: import gym env = gym.make('CartPole-v0') for i_episode in range(20): …
26
votes
4 answers

Timedelta is not defined

Below is the code I am working on. From what I can tell there is no issue, but when I attempt to run the piece of code I receive an error. import os import datetime def parseOptions(): import optparse parser = optparse.OptionParser(usage=…
Mitchell
  • 449
  • 1
  • 5
  • 10
23
votes
5 answers

NameError: name 'pd' is not defined

I am attempting run in Jupyter import pandas as pd import matplotlib.pyplot as plt # plotting import numpy as np # dense matrices from scipy.sparse import csr_matrix # sparse…
dbldee
  • 241
  • 2
  • 3
  • 5
17
votes
2 answers

Python - NameError: name itemgetter not defined

I just started learning Python came across this very simple code could not get it right: import operator; b=[(5,3),(1,3),(1,2),(2,-1),(4,9)] sorted(b,key=itemgetter(1)) I got the error: NameError: name 'itemgetter' is not defined. Any idea?
Ricky Nelson
  • 197
  • 1
  • 1
  • 7
1
2 3
84 85