【问题标题】:The Heroku's Python doesn't find redis(redistogo) for importHeroku 的 Python 找不到要导入的 redis(redistogo)
【发布时间】:2012-05-22 20:11:17
【问题描述】:

我在 Heroku 上添加了 Redistogo 插件,但我无法在控制台模式下对其进行测试。 我是按照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

Heroku 的 Python 找到:os,urlparse 但找不到 redis。
有人帮助我吗?我只需要 Heroku 的 Python 控制台模式!
使用本地 Python 和远程 REDISTOGO 我没有任何问题!

更新:

来自文档:

部署到 Heroku

要在 Heroku 上使用 Redis To Go,请安装 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 控制台上不起作用!

请分享你在这方面的实践。

【问题讨论】:

    标签: python heroku redis redistogo


    【解决方案1】:

    你们为什么要尝试使用远程控制台来制作所有内容?在本地执行应用程序(创建 store.py、requirements.txt)然后部署它!当您部署(或推送)您的应用程序时,Heroku 会看到 requirements.txt,它会添加必要的库。所以这就是它不起作用的原因,因为你的heroku dyno上没有安装redis库。

    我不是 python 开发人员,但我已经做到了,没有问题

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

    【讨论】:

    • 由于文档原因,我需要它进行调试!
    【解决方案2】:

    这些步骤应该在本地完成,提交到 git,然后推送到 heroku。当你这样做时:

    heroku run python --app redis-to-go
    

    它会启动您的应用程序的一个独立实例。这不是持久的,只存在于该测功机内部。如果你想在一个独立的实例中完全测试它,你可以加载 virtualenv 然后:

    pip install redis
    

    但是,当您下次运行应用程序时,这将不可用。相反,您应该检查所有文件然后推送。即使您只是将 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解释器内部:

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

    【讨论】:

    • 谢谢。我已经通过 git 完成了,但是文档呢?另外,我可以运行一个纯python应用程序吗?如何?
    • 结果: ...到 git@heroku.com:morning-sky-4527.git 0443ab8..f014ce1 master -> master bvi@bvi-1201NL:~/venv$ heroku run python 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. &gt;&gt;&gt; import app Debug from store.py: host= lab.redistogo.com Debug from app.py: answer: 42 &gt;&gt;&gt; bvi@bvi-1201NL:~/venv$
    猜你喜欢
    • 1970-01-01
    • 2019-01-18
    • 2015-05-04
    • 1970-01-01
    • 2012-02-07
    • 2018-01-30
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    相关资源
    最近更新 更多