繁体   English   中英

Ruby脚本无法加载通过捆绑程序安装的gem

[英]Ruby script cannot load a gem installed via bundler

我正在尝试将ruby-mysql gem包含在我的ruby脚本中。 我已经使用bundler安装了gem,但是当我运行bundle exec ./mysql_connector ,在./mysql_connector:4:in ``require': cannot load such file -- ruby-mysql (LoadError)收到错误./mysql_connector:4:in ``require': cannot load such file -- ruby-mysql (LoadError) 您能帮我解决问题是什么吗?

我做了什么

在我的主目录中安装了rails。 我没有对该服务器的root访问权限,因此我已按照此处的说明在本地目录中安装了rails: http : //www.r-bloggers.com/installing-ruby-on-linux-as-a-user-非根目录/

为我的应用程序创建了一个目录。 我的应用程序位于我的主目录中的一个名为connector的文件夹中。 它具有一个Gemfile,如下所示:

source 'https://rubygems.org'
gem 'ruby-mysql'

调用bundle install

Using ruby-mysql 2.9.14
Using bundler 1.11.2
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Bundled gems are installed into ./vendor/bundle.

向我的脚本添加依赖项。 我的脚本在connector / mysql_connector中,内容为:

#!/home/dcox/bin/ruby
require 'rubygems'
require 'bundler/setup'
require 'ruby-mysql'

使脚本可执行。 我看到您需要使用可执行文件运行bundle exec ,因此我按照此处的说明将脚本设为可执行文件: http : //commandercoriander.net/blog/2013/02/16/making-a-ruby-script-可执行文件/

运行脚本。 我使用bundle exec mysql_connector执行,请参见:

/home/dcox/bin/mysql_connector:4:in `require': cannot load such file -- ruby-mysql (LoadError)
    from /home/dcox/bin/mysql_connector:4:in `<main>'

是$ LOAD_PATH吗? 在寻找答案之后,我发现了很多SO答案以及一篇博客文章( https://codedecoder.wordpress.com/2013/09/23/require-and-load-in-ruby-loaderror-cannot-似乎暗示问题的load-such-file / )是gem没有安装在$ LOAD_PATH的目录中。 这是从IRB运行$LOAD_PATH时看到的内容:

irb(main):002:0> $LOAD_PATH
=> ["/home/dcox/lib/ruby/site_ruby/2.1.0", 
"/home/dcox/lib/ruby/site_ruby/2.1.0/x86_64-linux", 
"/home/dcox/lib/ruby/site_ruby", "/home/dcox/lib/ruby/vendor_ruby/2.1.0", 
"/home/dcox/lib/ruby/vendor_ruby/2.1.0/x86_64-linux", 
"/home/dcox/lib/ruby/vendor_ruby", "/home/dcox/lib/ruby/2.1.0", 
"/home/dcox/lib/ruby/2.1.0/x86_64-linux"]

接下来,我检查了ruby-mysql的位置:

dcox@analytics1:~/connector$ bundle show ruby-mysql
/data/home/dcox/connector/vendor/bundle/ruby/2.1.0/gems/ruby-mysql-2.9.14

显然,我的connector/vendor/bundle路径不在$ LOAD_PATH上。 我可以添加它,但是我感觉这里缺少一些简单的东西,因为只要您按照说明进行操作,捆绑程序就可以正常工作,对吗?

任何建议或帮助都非常感谢! 谢谢!!

如果您只需要此特定的gem,则require 'mysql'应该可以工作(例如, https : //github.com/tmtm/ruby-mysql/blob/master/test/test_mysql.rb#L10 )。

您的文件应调用Bundler.setup http://bundler.io/bundler_setup.html

更好的是,如果您改为调用Bundler.require(:default) ,它将设置并要求您Gemfile中的所有gem。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM