[英]Using the rubypython gem in ruby on rails, how do you call a python script from the lib folder?
我非常了解ruby,但是我以前从未使用过python。
有一个很棒的python脚本,称为colorific 。 我已经在我的rails应用程序的lib文件夹中签出了该脚本,并安装了rubypython gem。
我想知道我现在如何将此密码导入应用程序,到目前为止,我已经知道了这一点:
RubyPython.start # start the Python interpreter
test = RubyPython.import("#{Rails.root}/lib/colorific/setup.py")
RubyPython.stop # stop the Python interpreter
但是,它引发了错误……
RubyPython::PythonError: ImportError: Import by filename is not supported.
我想知道如何导入此脚本并开始使用它的方法?
colorific是一个命令行实用程序。 它似乎没有提供要导入的API。 我不会在这里使用Ruby-to-Python桥,只需使用与Python子进程模块相当的Ruby运行该工具即可。 作为一个单独的过程。 (马丁·彼得斯)
colorific测试套件本身会导入colorific,并且有一个名为setup.py的文件,因此colorific看起来像是标准的python模块发行版。
test = RubyPython.import("#{Rails.root}/lib/colorific/setup.py")
python模块分发中的setup.py文件用于将模块安装在文件系统中的特定位置。 通常,您可以这样安装python模块:
$ python setup.py install
然后,将文件导入到这样的python程序中:
import colorific
或者,如果您将模块名称作为字符串,则可以这样进行导入:
import importlib
importlib.import_module('colorific')
但是,python在特定目录中查找您导入的模块。 python.sys.path给出了python搜索导入模块的目录列表:
import sys
print sys.path
sys.path是一个python列表,可以修改。
我建议您首先在某个目录中构建colorific模块:在某个地方创建一个空的colorific目录,例如/ Users / YourUserName / colorific,然后将cd插入包含setup.py的目录中并执行以下操作:
$ python setup.py install --home=/Users/YourUserName/colorific
安装之后,将colorific目录移到您的Rails应用程序中的某个位置,例如/ your_app / lib。
然后在RubyPython中执行以下操作:
RubyPython.start # start the Python interpreter
sys = RubyPython.import("sys")
sys.path.append("#{Rails.root}/lib")
colorific = RubyPython.import('colorific')
RubyPython.stop
您可能还希望打印出sys.path,以查看rubypython gem在哪里设置以查找模块。
====
当我尝试:
$ python setup.py install --home=/Users/YourUserName/colorific
我得到了错误:
error: bad install directory or PYTHONPATH
所以我像通常安装python模块一样安装了colorific:
$ python setup.py install
将模块安装在系统相关的默认目录中,在Mac上为:
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
有关其他系统,请参见此处:
彩色安装在站点包中创建了一个目录,名为:
colorific-0.2.1-py2.7.egg/
我将该目录移到了应用程序的lib目录中:
$ mv /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/colorific-0.2.1-py2.7.egg /Users/7stud/rails_projects/my_app/lib
然后,我使用以下代码导入模块,并以colorific调用函数:
RubyPython.start # start the Python interpreter
logger.debug "hello " + "world"
sys = RubyPython.import('sys')
logger.debug sys.path
sys.path.append("#{Rails.root}/lib/colorific-0.2.1-py2.7.egg/")
colorific = RubyPython.import('colorific')
logger.debug colorific.hex_to_rgb("#ffffff")
RubyPython.stop
我将该代码付诸行动。 这是log / development.log中的输出:
hello world
[<Lots of paths here>, '/Users/7stud/rails_projects/test_postgres/lib/colorific-0.2.1-py2.7.egg/']
(255, 255, 255)
我发现RubyPython不断使$ rails server
(WEBrick)崩溃:
/Users/7stud/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/rubypython-0.6.3/lib/rubypython.rb:106: [BUG] Segmentation fault
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin10.8.0]
-- Crash Report log information --------------------------------------------
See Crash Report log file under the one of following:
* ~/Library/Logs/CrashReporter
* /Library/Logs/CrashReporter
* ~/Library/Logs/DiagnosticReports
* /Library/Logs/DiagnosticReports
the more detail of.
<1000+ lines of traceback omitted>
即使我可以这样写:
logger.debug "hello " + "world"
这将不起作用:
logger.debug "******" + colorific.hex_to_rgb("#ffffff")
也不是这样:
logger.debug "*********" + colorific.hex_to_rgb("#ffffff").rubify
就像任何红宝石一样,RubyPython的文档太恐怖了。 但是,在这种情况下,他们在python colorific文档中找到了相等的匹配项。
您的原始代码需要几行:
RubyPython.start # start the Python interpreter
sys = RubyPython.import("sys") # (add) method used to search for a directory
sys.path.append('./lib/colorific') # (add) execute search in directory
RubyPython.import("setup") # (add) call on setup.py in the directory
RubyPython.stop # stop the Python interpreter
我非常了解Python,但是我几乎从未使用过Ruby ...
这里有几处错误:
Python导入完成了模块名称 ; 您正尝试导入文件名。
通常,您会将目录添加到#{Rails.root}/lib
目录的Python模块搜索路径中(使用PYTHONPATH
环境变量或通过操作Python代码中的sys.modules
列表),但是您试图在此处导入安装脚本 。
您将运行python setup.py install
来安装该库。 或者,更好的方法是,使用pip
或easy_install
为您安装该软件包。
colorific
是一个命令行实用程序。 它似乎没有提供要导入的API。 我不会在这里使用Ruby-to-Python桥,只需使用与Python subprocess
模块相当的Ruby运行该工具即可。 作为一个单独的过程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.