2012-05-15 24 views
3

我在Heroku中添加了Redistogo插件,但我無法在控制檯模式下對其進行測試。 我已經按照documentation這樣做過。Heroku的Python沒有找到用於導入的redis(redistogo)

$ 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 

的Heroku的Python的發現:操作系統,但裏urlparse找不到Redis的。
有誰幫我?我只需要Heroku的Python控制檯模式!
與本地Python和遠程REDISTOGO我沒有任何問題!

更新:

從文檔:

部署到Heroku的

要使用Redis的去的Heroku,安裝redistogo附加:

$ heroku addons:add redistogo 

從Heroku控制檯測試它的工作原理:

$ 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' 

從Heroku控制檯不起作用!

請分享你的這方面的做法。

回答

0

爲什麼你們試圖讓所有的遠程控制檯?在本地執行應用程序(創建store.py,requirements.txt)然後部署它! Heroku在部署(或推送)應用程序時會看到requirements.txt,並添加必要的庫。所以這就是爲什麼它不起作用,因爲你的heroku dyno沒有安裝redis庫。

我不是一個Python開發,但我這樣做沒有問題

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

+1

我需要它用於調試和因爲文檔的! –

3

的步驟應在本地完成,致力於Git裏,然後推到Heroku的。當你這樣做:

heroku run python --app redis-to-go 

它旋轉了一個孤立的應用程序實例。這不是持久性的,只存在於該測功機內部。你運行你的應用程序下一次

pip install redis 

但是,這不會是可供選擇:如果你想在一個孤立的例子充分測試它,你可以加載的virtualenv然後。相反,你應該檢查所有文件然後推送。即使您只是簡單地將redis添加到您的requirements.txt中,它也應該在獨立的測試版中運行。

根據您的命令這應該充分工作:

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 

在你的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') 
+0

謝謝。我已經通過git完成了,但是文檔呢? 另外,我可以運行純Python應用程序嗎? –

+0

**結果:** ...至[email protected]:morning-sky-4527.git 0443ab8..f014ce1 master - > master 'bvi @ bvi-1201NL:〜/ venv $ heroku run python 運行Python附加到終端... up,run.1 Python 2.7.2(默認,2011年10月31日,16:22:04) [GCC 4.4.3]在linux2上 鍵入「help」,「copyright」, 「信用」或「許可證」以獲取更多信息。 >>> import app 從store.py調試:host = lab.redistogo.com 從app.py調試:answer:42 >>> bvi @ bvi-1201NL:〜/ venv $' –