The Heroku's Python doesn't find redis(redistogo) for import

2.3k Views Asked by At

I've added the Redistogo add-on on Heroku,but I can't to test it out in the console mode. I have done this in accordance with the documentation.

$ heroku run python --app redis-to-go
Running python attached to terminal... up, run.1
Python 2.7.2 (default, Oct 31 2011, 16:22:04) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> f=open('requirements.txt', 'w')
>>> f.write('redis==2.4.12'+'\n' )
>>> f.close()
>>>
>>> f=open('store.py', 'w')
>>> f.write('import os'+'\n' ) 
>>> f.write('import urlparse'+'\n' ) 
>>> f.write('import redis'+'\n' ) 
>>> f.write("url = urlparse.urlparse(os.environ.get('REDISTOGO_URL', 'redis://localhost'))"+'\n' ) 
>>> f.write('redis = redis.Redis(host=url.hostname, port=url.port, db=0,     password=url.password)'+'\n' ) 
>>> f.close()
>>>
>>> from store import redis
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "store.py", line 3, in <module>
    import redis
ImportError: No module named redis

The Heroku's Python finds:os,urlparse but can't find redis.
Is there anyone who help me? I need only the Heroku's Python console mode!
With the local Python and remote REDISTOGO I don't have any problem!

Update:

From the documentation:

Deploying to Heroku

To use Redis To Go on Heroku, install the redistogo add-on:

$ heroku addons:add redistogo

Test that it works from the Heroku console:

$ heroku run python
Python 2.7.2 (default, Oct 31 2011, 16:22:04) 
>>> from store import redis
>>> redis.set('answer', 42)
True
>>> redis.get('answer')
'42'

It doesn't work from the Heroku console!

Please share your practice on this.

2

There are 2 best solutions below

1
On

Why you guys try to make all with the remote console?? Do the app locally (create store.py, requirements.txt) and then deploy it!! Heroku sees the requirements.txt when you deploy (or push) your app and it adds the necessary libraries. So that's why it didn't work, because redis library is not installed on your heroku dyno.

I'm not a python developer but I've done this without problems

https://github.com/ismaelga/python-redis-heroku

2
On

The steps should be done locally, committed to git, then pushed to heroku. When you do:

heroku run python --app redis-to-go

It spins up an isolated instance of your application. This is not persistent and only exists inside that dyno. If you wanted to fully test it in an isolated instance you could load the virtualenv then:

pip install redis

However, this would not be available the next time you run your application. Instead you should have all files checked in then push. Even once you've simply added redis to your requirements.txt it should work in an isolated dyno.

Based on your command this should fully work:

cat "redis==2.4.12" >> requirements.txt
git add requirements.txt
git commit -m 'adding redis'
git push heroku master
heroku addons:add redis-to-go
heroku run python --app redis-to-go

The inside your python intrepreter:

import os
import urlparse

import redis

url = urlparse.urlparse(os.environ.get('REDISTOGO_URL', 'redis://localhost'))

redis = redis.Redis(host=url.hostname, port=url.port, db=0, password=url.password)
redis.set('answer', 42)
redis.get('answer')