-1

I'm testing a web service written with python and flask. Within that service I want to generate a random number. None of the solutions posted on the web seem to work, as if the "random" module does not work. I checked, and there's no random.py or random.pyc within the folders around the webservice.py

Here's how I start the web service:

$ . venv/bin/activate
$ python webservice.py

How can I fix random module not being available when running in a virtual environment?

import random
from random import randint

x = random.random()*100
x = randint(0,10)
print 'r' + x #does not work 

Here's what's in the venv folder:

enter image description here

Alex Stone
  • 46,408
  • 55
  • 231
  • 407
  • 3
    *as if the "random" module does not work* ?? what is the exact error you get ? If you do not get any error, what is the behavior you see ? – karthikr Aug 25 '14 at 16:08
  • One more thing : change the local variable names from `random = ...` to something else. – karthikr Aug 25 '14 at 16:09
  • I tried multiple local variable names, started with x, but nothing worked – Alex Stone Aug 25 '14 at 17:45
  • 1
    gonna need more info than this. What does "not work" mean? Does it print `r` to the console or does it provide some kind of traceback? – corvid Aug 25 '14 at 17:51
  • You dont have to try multiple variable name. just `x` should do. And what is "does not work". If we were to start guessing, your problem might end up being something else altogether. – karthikr Aug 25 '14 at 19:07
  • By does not work I mean that the web service returns 500 Internal Server Error when processing the call to the random function. I don't know how to debug it further than that, but every call to functions from Random that I made ended up crashing the web service – Alex Stone Sep 10 '14 at 14:35

2 Answers2

2

Check to see if the help("random") method will give a list.

  • Then reinstall the module in that venv. Also what is in the lib folder?

a link to help.

https://docs.python.org/2/library/random.html

  • print help("random") actually prints help when used from Python. FILE /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.py – Alex Stone Aug 25 '14 at 17:40
  • lib contains frameworks for Flask, flup, pip, parse_rest, werkezeug, jinja and some other .py files. Should I try to use something like pip random? – Alex Stone Aug 25 '14 at 17:43
0

Seems like you try to add string and integer.

Making a string out of a string and an integer in Python

print 'x' + str(randint(0,10)) # try casting the int to string :)
Community
  • 1
  • 1
Hhhhhhh
  • 29
  • 3