【问题标题】:Ruby script cannot locate required gemRuby 脚本找不到所需的 gem
【发布时间】:2014-01-15 22:13:19
【问题描述】:

我正在尝试运行过去运行良好的 Ruby 脚本。我需要使用 Mechanize gem,所以在脚本的顶部,它显示require 'mechanize'。但是,当我现在尝试运行脚本时,终端输出显示为:

/Users/codebiker/.rvm/rubies/ruby-2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- mechanize (LoadError)
from /Users/codebiker/.rvm/rubies/ruby-2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from myscript.rb:2:in `<main>'

当我按照这里的建议运行 gem env 时,我得到:

RubyGems Environment:
  - RUBYGEMS VERSION: 2.2.1
  - RUBY VERSION: 2.1.0 (2013-12-25 patchlevel 0) [x86_64-darwin13.0]
  - INSTALLATION DIRECTORY: /Users/codebiker/.rvm/gems/ruby-2.1.0
  - RUBY EXECUTABLE: /Users/codebiker/.rvm/rubies/ruby-2.1.0/bin/ruby
  - EXECUTABLE DIRECTORY: /Users/codebiker/.rvm/gems/ruby-2.1.0/bin
  - SPEC CACHE DIRECTORY: /Users/codebiker/.gem/specs
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-darwin-13
  - GEM PATHS:
     - /Users/codebiker/.rvm/gems/ruby-2.1.0
     - /Users/codebiker/.rvm/gems/ruby-2.1.0@global
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
     - :benchmark => false
     - :sources => ["http://rubygems.org/", "http://gemcutter.org"]
     - "install" => "--no-rdoc --no-ri"
     - "update" => "--no-rdoc --no-ri"
  - REMOTE SOURCES:
     - http://rubygems.org/
     - http://gemcutter.org
  - SHELL PATH:
     - /Users/codebiker/.rvm/gems/ruby-2.1.0/bin
     - /Users/codebiker/.rvm/gems/ruby-2.1.0@global/bin
     - /Users/codebiker/.rvm/rubies/ruby-2.1.0/bin
     - /usr/local/bin
     - /usr/bin
     - /bin
     - /usr/sbin
     - /sbin
     - /usr/local/bin
     - /opt/X11/bin
     - /usr/texbin
     - /Users/codebiker/.rvm/bin

当我运行rvm get head 时,我得到:

Upgrading the RVM installation in /Users/codebiker/.rvm/
RVM PATH line found in /Users/codebiker/.bashrc /Users/codebiker/.zshrc.
RVM sourcing line found in /Users/codebiker/.bash_profile /Users/codebiker/.zprofile.
Upgrade of RVM in /Users/codebiker/.rvm/ is complete.

当我运行which ruby 时,我得到:

/Users/codebiker/.rvm/rubies/ruby-2.1.0/bin/ruby

ruby -v 给了我:

ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-darwin13.0]

任何建议、想法或提示将不胜感激!我不知道这里有什么问题。

【问题讨论】:

  • 可能太明显了,gem安装对了吗?
  • 键入这是 cmd gem install mechanize

标签: ruby rubygems rvm


【解决方案1】:

必须先安装 Gem,然后才能在脚本中 require 它们。安装gem;首先打开一个终端窗口,然后输入:

gem install GEM-NAME 或者在你的情况下:gem install mechanize

【讨论】:

  • 这完全修复了它——谢谢!但是,我不确定为什么 Mechanize 不再存在。我之前在我的计算机上的脚本中使用过 Mechanize gem 很多次,所以我只是假设没有必要再次安装它。为什么会突然消失?这可能与我最近安装的 Ruby 2.1 有关吗?
  • 是的,完全正确。每个 ruby​​ 安装都有一个独立的 gems 目录。很高兴我能帮上忙。编码愉快!
【解决方案2】:

如果您使用,请添加到您的 链接到 gem:

gem 'mechanize'

然后为您的 或其他项目文件夹发出捆绑安装:

$ bundle install

确保安装包后 gem 出现在 中。

那么即使你不使用 bundler,也要确保 bundle 找到了 gem:

$ bundle show mechanize
/path/to/mechanize/gem

如果您的应用不是 应用,只需运行您的应用:

$ bundle exec ./your_app

【讨论】:

  • 很好的回应,谢谢!我认为 feed_me_code 的答案是正确的,因为这只是一个脚本,而不是一个完整的应用程序,但我很欣赏这个建议。我很好奇你是否知道这颗宝石为什么会消失。 (我永远不会卸载它,因为我经常使用它。)
  • @CodeBiker 如果您使用 rvm,您可以将其安装到其他 gemset 中。或者,如果您使用的是 root 安装,也可以将其视为其他 gemset。
猜你喜欢
  • 1970-01-01
  • 2016-04-09
  • 2011-05-11
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
  • 1970-01-01
  • 2012-04-07
  • 2012-11-05
相关资源
最近更新 更多