Python Forum
is something missing? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: is something missing? (/thread-22377.html)



is something missing? - Skaperen - Nov-10-2019

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.


RE: is something missing? - Larz60+ - Nov-11-2019

I just tried it on my system and it works fine:
>>> from random import Random as _Random
>>>



RE: is something missing? - Skaperen - Nov-11-2019

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.


RE: is something missing? - Larz60+ - Nov-11-2019

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.


RE: is something missing? - LeanbridgeTech - Nov-11-2019

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 ;-)


RE: is something missing? - Larz60+ - Nov-11-2019

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.


RE: is something missing? - Skaperen - Nov-12-2019

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.