python - sublime Node.js was not found in the default path.

 人鱼同体 发布于 2022-11-01 13:33

系统环境是mac 环境,python版本是2.7,当使用HTML-CSS-JS-Prettify 格式化代码的时候出现下面的错误:

Using node.js path on 'osx': /usr/local/bin/node
Unexpected error(): 'module' object has no attribute 'getoutput'
error: You won't be able to use this plugin without specifying the path to node.js.
Traceback (most recent call last):
  File "./sublime_plugin.py", line 362, in run_
  File "./HTMLPrettify.py", line 48, in run
  File "./HTMLPrettify.py", line 110, in get_output_diagnostics
AttributeError: 'NoneType' object has no attribute 'find'

之前也查了些资料,已经做了下面的修改,并且项目路径也没有中文名称

PluginUtils.get_node_path() 成为PluginUtils.get_node_path().encode("utf-8")

有没有人遇到过同样的问题

3 个回答
  • windows7 下碰见的 格式化的时候报错,我只知道是要格式话的文件路径有中文就不对了,但是我照着网上的方法把那个HTMLPrettify.py文件的里面加了encode('utf-8'),依然不起作用,至今未解决

    2022-11-02 06:15 回答
  • 我之前在linux下面碰到个有点像的,但是当时的报错忘了

    就是做了个链接就好了。。。

    sudo ln -s /usr/bin/nodejs /usr/bin/node
    2022-11-02 06:23 回答
  • 我也遇到了这个问题,我的报错跟你一样:

    Using node.js path on 'osx': /usr/local/bin/node
    Unexpected error(<type 'exceptions.AttributeError'>): 'module' object has no attribute 'getoutput'
    Traceback (most recent call last):
      File "./sublime_plugin.py", line 362, in run_
      File "./HTMLPrettify.py", line 48, in run
      File "./HTMLPrettify.py", line 110, in get_output_diagnostics
    AttributeError: 'NoneType' object has no attribute 'find'
    

    目前还不知道哪里的问题,因为之前这个插件都工作正常的,但是我在安装sublime3之后,就出现这个问题了,不知道是不是两个sublime版本导致的


    经过一个下午的排查,已经解决了此问题,但是可能方法并不是特别规范。

    我先说下排查经过:

    首先,查看sublime的调试输出是通过Ctrl+`下的console的,在使用HTML-CSS-JS Prettify的时候,得到报错:

    Node.js was not found in the default path. Please specify the location.
    

    可能很多人被错误提示误导,以为是node没有安装,但是我本地的node地址确实是/usr/local/bin/node,此错误提示并不准确:

    andy@AndyMacBookPro:~$ which node
    /usr/local/bin/node
    andy@AndyMacBookPro:~$
    

    node本身并没有错,然后我们查看console中的输出:

    Using node.js path on 'osx': /usr/local/bin/node
    Unexpected error(<type 'exceptions.AttributeError'>): 'module' object has no attribute 'getoutput'
    Traceback (most recent call last):
      File "./sublime_plugin.py", line 362, in run_
      File "./HTMLPrettify.py", line 48, in run
      File "./HTMLPrettify.py", line 110, in get_output_diagnostics
    AttributeError: 'NoneType' object has no attribute 'find'
    

    对于以上的报错,我一直是着眼于这个错误:

    'module' object has no attribute 'getoutput'
    

    我觉得这是最大的问题,我一直是把这个作为线索去寻找的!

    而主要的排查位置,我们应该确定在提示的HTMLPrettify.py中,这个文件在我本地的绝对位置是:

    /Users/andy/Library/Application Support/Sublime Text 2/Packages/HTML-CSS-JS Prettify/HTMLPrettify.py

    主要的报错原因,是因为在第200行,

    commands.getoutput(run)

    的时候,commands其实并没有实际导入。

    我本地python的commands是正常的:

    andy@AndyMacBookPro:~$ python
    Python 2.7.5 (default, Mar  9 2014, 22:15:05)
    [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import commands
    >>> commands.getoutput('pwd')
    '/Users/andy'
    >>>

    (我在本地编写python脚本import commands都是正常的,所以对于为什么没有导入,这个问题的原因还是不清楚),我在HTMLPrettify.py的源码第200行之前打印commands,结果是这样:

    <module 'commands' from './commands.pyc'>

    可以看到,插件需要获取的commands是从自己的当前目录获取的,而当前目录并没有这个文件,所以才导致了后面报出的'module' object has no attribute 'getoutput'错误,那怎么解决呢?最笨的办法,当然就是copy一份正确的commands.py到本地了。

    我本地有好几个commands.py,这里我们选择2.7版本即可:

    andy@AndyMacBookPro:/System/Library/Frameworks/Python.framework/Versions$ ll
    total 8
    drwxr-xr-x   7 root  wheel   238B  6 21  2014 ./
    drwxr-xr-x   9 root  wheel   306B  6 21  2014 ../
    drwxr-xr-x   6 root  wheel   204B  8 25  2013 2.3/
    drwxr-xr-x  11 root  wheel   374B  6 21  2014 2.5/
    drwxr-xr-x  11 root  wheel   374B  6 21  2014 2.6/
    drwxr-xr-x  12 root  wheel   408B 10 29 15:44 2.7/
    lrwxr-xr-x   1 root  wheel     3B  6 21  2014 Current@ -> 2.7
    andy@AndyMacBookPro:/System/Library/Frameworks/Python.framework/Versions$ cd Current/lib/python2.7/
    andy@AndyMacBookPro:/System/Library/Frameworks/Python.framework/Versions/Current/lib/python2.7$ ll commands.py*
    -rw-r--r--  1 root  wheel   2.5K  6 21  2014 commands.py
    -rw-r--r--  1 root  wheel   2.7K  6 21  2014 commands.pyc
    -rw-r--r--  1 root  wheel   2.7K  6 21  2014 commands.pyo
    andy@AndyMacBookPro:/System/Library/Frameworks/Python.framework/Versions/Current/lib/python2.7$
    

    copy之:

    andy@AndyMacBookPro:~/Library/Application Support/Sublime Text 2/Packages/HTML-CSS-JS Prettify$ ll
    total 184
    drwxr-xr-x  18 andy  staff   612B  1 21 17:44 ./
    drwxr-xr-x  78 andy  staff   2.6K  1 21 17:30 ../
    -rw-r--r--   1 andy  staff    20K  1 21 17:36 .HTMLPrettify.py.swp
    -rw-r--r--   1 andy  staff    16B  1 21 17:30 .gitignore
    -rw-r--r--   1 andy  staff   3.9K  1 21 17:30 .jsbeautifyrc
    -rw-r--r--   1 andy  staff     0B  1 21 17:30 .no-sublime-package
    -rw-r--r--   1 andy  staff   506B  1 21 17:30 Context.sublime-menu
    -rw-r--r--   1 andy  staff   393B  1 21 17:30 Default (Linux).sublime-keymap
    -rw-r--r--   1 andy  staff   398B  1 21 17:30 Default (OSX).sublime-keymap
    -rw-r--r--   1 andy  staff   394B  1 21 17:30 Default (Windows).sublime-keymap
    -rw-r--r--   1 andy  staff   7.7K  1 21 17:36 HTMLPrettify.py
    -rw-r--r--   1 andy  staff   8.3K  1 21 17:36 HTMLPrettify.pyc
    -rw-r--r--   1 andy  staff   473B  1 21 17:30 HTMLPrettify.sublime-commands
    -rw-r--r--   1 andy  staff   600B  1 21 17:30 HTMLPrettify.sublime-settings
    -rw-r--r--   1 andy  staff   1.3K  1 21 17:30 Main.sublime-menu
    -rw-r--r--   1 andy  staff    10K  1 21 17:30 README.md
    -rw-r--r--   1 andy  staff   258B  1 21 17:30 package-metadata.json
    drwxr-xr-x   4 andy  staff   136B  1 21 17:30 scripts/
    andy@AndyMacBookPro:~/Library/Application Support/Sublime Text 2/Packages/HTML-CSS-JS Prettify$ cp /System/Library/Frameworks/Python.framework/Versions/Current/lib/python2.7/commands.py ./
    andy@AndyMacBookPro:~/Library/Application Support/Sublime Text 2/Packages/HTML-CSS-JS Prettify$ ll
    total 200
    drwxr-xr-x  20 andy  staff   680B  1 21 17:44 ./
    drwxr-xr-x  78 andy  staff   2.6K  1 21 17:30 ../
    -rw-r--r--   1 andy  staff    20K  1 21 17:36 .HTMLPrettify.py.swp
    -rw-r--r--   1 andy  staff    16B  1 21 17:30 .gitignore
    -rw-r--r--   1 andy  staff   3.9K  1 21 17:30 .jsbeautifyrc
    -rw-r--r--   1 andy  staff     0B  1 21 17:30 .no-sublime-package
    -rw-r--r--   1 andy  staff   506B  1 21 17:30 Context.sublime-menu
    -rw-r--r--   1 andy  staff   393B  1 21 17:30 Default (Linux).sublime-keymap
    -rw-r--r--   1 andy  staff   398B  1 21 17:30 Default (OSX).sublime-keymap
    -rw-r--r--   1 andy  staff   394B  1 21 17:30 Default (Windows).sublime-keymap
    -rw-r--r--   1 andy  staff   7.7K  1 21 17:36 HTMLPrettify.py
    -rw-r--r--   1 andy  staff   8.3K  1 21 17:36 HTMLPrettify.pyc
    -rw-r--r--   1 andy  staff   473B  1 21 17:30 HTMLPrettify.sublime-commands
    -rw-r--r--   1 andy  staff   600B  1 21 17:30 HTMLPrettify.sublime-settings
    -rw-r--r--   1 andy  staff   1.3K  1 21 17:30 Main.sublime-menu
    -rw-r--r--   1 andy  staff    10K  1 21 17:30 README.md
    -rw-r--r--   1 andy  staff   2.5K  1 21 17:44 commands.py
    -rw-r--r--   1 andy  staff   2.3K  1 21 17:44 commands.pyc
    -rw-r--r--   1 andy  staff   258B  1 21 17:30 package-metadata.json
    drwxr-xr-x   4 andy  staff   136B  1 21 17:30 scripts/
    andy@AndyMacBookPro:~/Library/Application Support/Sublime Text 2/Packages/HTML-CSS-JS Prettify$
    

    之后再次使用插件,console输出:

    Using node.js path on 'osx': /usr/local/bin/node
    Using prettify options: {
      "html": {
        "allowed_file_extensions": [
          "htm",
          "html",
          "xhtml",
          "shtml",
          "xml",
          "svg"
        ],
        "brace_style": "collapse",
        "end_with_newline": false,
        "indent_char": " ",
        "indent_handlebars": false,
        "indent_inner_html": false,
        "indent_scripts": "keep",
        "indent_size": 4,
        "max_preserve_newlines": 0,
        "preserve_newlines": true,
        "unformatted": [
          "a",
          "span",
          "img",
          "code",
          "pre",
          "sub",
          "sup",
          "em",
          "strong",
          "b",
          "i",
          "u",
          "strike",
          "big",
          "small",
          "pre",
          "h1",
          "h2",
          "h3",
          "h4",
          "h5",
          "h6"
        ],
        "wrap_line_length": 0
      },
      "css": {
        "allowed_file_extensions": [
          "css",
          "scss",
          "sass",
          "less"
        ],
        "end_with_newline": false,
        "indent_char": " ",
        "indent_size": 4,
        "newline_between_rules": true,
        "selector_separator": " ",
        "selector_separator_newline": true
      },
      "js": {
        "allowed_file_extensions": [
          "js",
          "json",
          "jshintrc",
          "jsbeautifyrc"
        ],
        "brace_style": "collapse",
        "break_chained_methods": false,
        "e4x": false,
        "end_with_newline": false,
        "indent_char": " ",
        "indent_level": 0,
        "indent_size": 4,
        "indent_with_tabs": false,
        "jslint_happy": false,
        "keep_array_indentation": false,
        "keep_function_indentation": false,
        "max_preserve_newlines": 0,
        "preserve_newlines": true,
        "space_after_anon_function": false,
        "space_before_conditional": true,
        "space_in_empty_paren": false,
        "space_in_paren": false,
        "unescape_strings": false,
        "wrap_line_length": 0
      }
    }
    

    没有再报错,并且文件被成功Prettify,问题解决 :-)

    但是,我的HTML-CSS-JS Prettify插件一直使用没有出过问题,这次出现问题的原因还是不清楚,可能跟我前段时间在本地安装了Sublime3有关系吧,至于为什么commands没有正常import,这个的原因也没有确定。希望有了解的朋友可以继续解答这个问题!


    现在是第二天上午,我打开电脑发现(我并没有关机):

    /Users/andy/Library/Application Support/Sublime Text 2/Packages/HTML-CSS-JS Prettify/

    目录下的commands.py文件已经不见了,但是之前的报错也不见了,很诡异啊~~~

    2022-11-02 06:27 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有