Python Forum

Full Version: import random
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I am using
import random
in my code. Every time I try and use this import, this error codes up.
Error:
NameError:name 'istart' is not defined random.py line 185 Python doesn't know what istart stands for
Python is giving me error for the random import. How do I fix this?
What version of python do you have?

Do you have a "random.py" in your local directory? Has the "random.py" in your python lib directory been edited?

Go to a different directory and run python -c 'import random'. Do you get the same error?
Do you have a file named random.py that you created? Post the full text of the error verbatim.
(Nov-15-2020, 05:24 AM)buran Wrote: [ -> ]Do you have a file named random.py that you created? Post the full text of the error verbatim.

No i dont have a file named random.py .But when i use import random, a file named random.py comes up. [Image: ERRRI_1.png]
You didn't say what version of python you have. The file that pops up looks like it has been modified and is missing a line.

This is the Python 3.7 random.py file. Note the line before your error is missing from your copy. I think your local version of python has been modified after installing.

    def randrange(self, start, stop=None, step=1, _int=int):
        """Choose a random item from range(start, stop[, step]).

        This fixes the problem with randint() which includes the
        endpoint; in Python this is usually not what you want.

        """

        # This code is a bit messy to make it fast for the
        # common case while still doing adequate error checking.
        istart = _int(start)
        if istart != start:
            raise ValueError("non-integer arg 1 for randrange()")
        if stop is None:
            if istart > 0:
                return self._randbelow(istart)
            raise ValueError("empty range for randrange()")