【问题标题】:Mechanize/Ruby: `require': cannot load such file -- mechanize (LoadError)机械化/Ruby:`require':无法加载这样的文件——机械化(LoadError)
【发布时间】:2017-12-04 18:11:37
【问题描述】:

我一直在尝试使用以下命令从终端运行 ruby​​ 文件:

ruby file_cleanse_auto.rb 

但我从 mechanize 得到一个错误:

/Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- mechanize (LoadError)
from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from file_cleanse_auto.rb:2:in `<main>'

在我的 ruby​​ 文件中,我有:

require 'open-uri'
require 'mechanize'

我可以在 Ruby shell (irb) 中使用 mechanize 而不会出现任何错误。当我尝试从命令行运行 ruby​​ 文件时,这似乎只是一个问题。 我安装的 ruby​​ 和 mechanize 的版本分别是 2.0.0 和 2.7.5。 我还尝试将机械化添加到 Gemfile(有和没有 nokogiri)。这就是现在的样子:

source "https://rubygems.org"
gem 'nokogiri'
gem 'mechanize'

运行后:

bundle

我明白了:

Resolving dependencies...
Using mini_portile2 2.2.0
Using unf_ext 0.0.7.4
Using mime-types-data 3.2016.0521
Using net-http-digest_auth 1.4.1
Using net-http-persistent 2.9.4
Using ntlm-http 0.1.1
Using webrobots 0.1.2
Using bundler 1.15.1
Using nokogiri 1.8.0
Using unf 0.1.4
Using mime-types 3.1
Using domain_name 0.5.20170404
Using http-cookie 1.0.3
Using mechanize 2.7.5
Bundle complete! 2 Gemfile dependencies, 14 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

但是当我再次尝试运行 ruby​​ 文件时,我仍然遇到同样的错误。有人知道我如何解决这个错误吗?谢谢!

【问题讨论】:

  • 你可以试试bundle exec ruby
  • 顶部可能是require 'rubygems'
  • 我已经尝试了这两种方法,但是当我输入 bundle exec ruby​​ 并按 Enter 时,什么也没有发生。光标在空行上。
  • 添加 require 'rubygems' 并运行文件仍然给我错误。

标签: ruby command-line terminal mechanize


【解决方案1】:

我最近安装ruby 2.4 后遇到了同样的问题。

你应该试试gem install mechanize

这是因为每个ruby installation have an independent directory for gems。希望这行得通。

【讨论】:

    猜你喜欢
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多