Nameerror: name long is not defined

How do I fix the Python nameerror: name long is not defined error message?

If this error gives you a hard time, keep on reading!

Today, we are going to discuss what this error means and why it occurs in your Python code.

And the highlight of this article is to help you to fix the name long is not defined error.

What is “nameerror name long is not defined”?

The nameerror: name long is not defined occurs when you’re trying to use a variable or function named “long”  that has not been defined.

In addition to that, this error occurs because the “long” data type has been removed and replaced with the “int” data type in Python version 3.

That also represents integers of arbitrary size. In Python version 2, long was a built-in data type for representing integers of arbitrary size.

For example:

sample = long(10)
print(sample)

If you try to run this code, it will throw a NameError that indicates name long is not defined.

What are the root causes of “nameerror: name long is not defined”?

This error message can occur due to several reasons, which include the following:

👎 There’s a typo in the variable or function name in your code.

👎 If you are using the “long” built-in data type in Python version 3, the error arises because “long” was written for Python version 2.

👎 There’s a missing import statement for a module that defines a variable or function named long.

How to fix “nameerror: name long is not defined”?

To fix the nameerror name long is not defined error message, you have to change the “long” data type with “int” in your Python code.

The reason is that “long” has been renamed to “int” in Python version 3.

Solution 1: Change “long” with “int”

When your Python code was only applicable for Python version 2 you have to change it if you’re running it for Python version 3.

In this case, we have to change “long”  with “int” in order to fix this issue.

Incorrect code:

sample = long(10)
print(sample)

Corrected code:

 sample = int(100)
print(sample)

Solution 2: Define “long” as a function or variable

You can define “long” as a function or variable when your Python code requires a variable or function named “long,” you can easily define it in your code to resolve the error.

For example:

 def long(sample):
    return int(sample)

sample = long(120)

print(sample)

Output:

120

Solution 3: Use a third-party library

You can also use a third-party libraries that supports the “long” data type in Python version 3.

For example:

 from past.builtins import long

sample = long(130)
print(sample)

Output:

130

Solution 4: Check the version of Python

Ensure that you are running the code in the correct version of Python.

If the code was written for Python version 2, where long was a built-in data type, and you are running it in Python version 2, then there’s no problem with it.

However, if it’s used in Python version 3, where long has been removed, you have to change it, as you can see in solution 1.

You can either update the code to be compatible with Python version 3 or change “long” to “int.” Otherwise, you can run the code in Python version 2.

 import sys
print(sys.version)

or

 import sys

print("Python version")
print(sys.version)
print("Version info.")
print(sys.version_info)

Additional solution for”nameerror: name long is not defined”

✅ Check for Typos

Check your code for typos, and ensure that all variable and function names are spelled correctly.

✅ Check Variable Scope

If you’re getting a name error due to scope issues, you’ll need to check where the variable is defined and where you’re trying to use it. Ensure that the variable or function is defined in the correct scope.

✅ Check Imports

Ensure sure that you’ve imported all the necessary modules and libraries. Double-check the spelling and make sure that you’re using the correct syntax for importing modules.

Conclusion

In conclusion, the Python error message nameerror: name long is not defined occurs when you are trying to use a variable or function named “long”  that has not been defined.

In addition to that, this error occurs because the “long” data type has been removed and replaced with the “int” data type in Python version 3, which can also represent integers of arbitrary size.

This article discusses what this error is all about and already provides solutions to help you fix this error.

You could also check out other “nameerror” articles that may help you in the future if you encounter them.

We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊

Leave a Comment