Python Forum

Full Version: is something missing?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Output:
File "/usr/lib/python3.6/tempfile.py", line 184, in <module> from random import Random as _Random ImportError: cannot import name 'Random'
is this a bad import or is something missing?

no... it's the danger of a new command script having the same name as a module.
I just tried it on my system and it works fine:
>>> from random import Random as _Random
>>>
create a file named random.py in your home directory and foo.py with that statement in it and try running foo.py and see what happens.
does the file have a class named ramdom, and does that class have a method named Random?
if not, there's your reason for the fail.
Your code is correct and it should work. The reason it is not working is, in the same folder, where you are running your r.py you also have file (your previous program) with the name: random.py, see this from your error print out:

File "/home/aakash/python_yt/random.py", line 5, in <module>
So what it is happening is, python ALWAYS first look into your folder for modules and it finds your random.py, and the real inbuild random function with the same name can't be called since your 'module' has always priority. Rename your previous file into something else and your current code will work as it should ;-)
As I stated, you can override if your file contains a random class, (best if it inherits the original class),
and must contain methods for those you want to override.
it didn't have Random in it at all. it was just a command script that prints out some random hex. it got a new name. if figured out the issue before i posted. that's why i included that 2nd line in my post. i guess it was too subtle of an answer.