Forums

Newbie- Using modules

I am a fairy new Python user. I want to install the random module. I have used the math module before without any issues (as long as I include 'import math'. But when I type 'import random', I receive an error. Can anyone help me import the random module for python 2.7?

Thanks!

Works OK for me - open a Python 2.7 shell from the PA dashboard, then:

import random
print random.random()

What error message do you get?

HTH

Jim

Thanks for responding. I used the exact code you had above, and I kept getting a traceback about not having an attribute to the module. Then, I went into my files and saw a file with the extension .pyc and it had the same name as my file. So I had: random.py random.pyc
But the file with .pyc I couldn't edit. I don't know where it came from. I deleted it, and then went back into random.py, and everything worked. Still not sure what happened...

Thanks again for trying to help!

I would recommend not naming your file random.py, I think you were getting a 'name clash' with the imported 'random'. So maybe just rename it to rantest.py or something?

Ohh--- that makes sense! Because later I changed the file name to something totally different. Thank you!

You're welcome.

This might help a bit..

The file "random.pyc" is (or was) a "compiled" python file, with pre-processed contents so that future use of that module can start faster.

So as to not repeat the internet, I'll just refer to this discussion here:

http://stackoverflow.com/questions/2998215/if-python-is-interpreted-what-are-pyc-files