简体   繁体   English

在 python 3.7 中使用黑色作为预提交钩子

[英]Use black with python 3.7 as pre-commit hook

I'm trying to use black on pre-commit, by using this settings:我试图通过使用以下设置在预提交时使用黑色:

repos:
-   repo: https://github.com/ambv/black
    rev: stable
    hooks:
    - id: black
      language_version: python3.7

Unfortuntely I get this error: (it doesn't happen if I try to format with python3.6, but I really need the 3.7!)不幸的是,我收到此错误:(如果我尝试使用 python3.6 进行格式化,则不会发生这种情况,但我确实需要 3.7!)

### version information

```
pre-commit version: 1.21.0
sys.version:
    2.7.15 (default, Jan 12 2019, 21:07:57) 
    [GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.5)]
sys.executable: /usr/local/opt/python@2/bin/python2.7
os.name: posix
sys.platform: darwin
```

### error information

```
An unexpected error has occurred: CalledProcessError: command: ('/usr/local/opt/python@2/bin/python2.7', '-mvirtualenv', '/Users/user name/.cache/pre-commit/repoi4s0y8j6/py_env-python3.7', '-p', 'python3.7')
return code: 3
expected return code: 0
stdout:
    The path python3.7 (from --python=python3.7) does not exist

stderr: (none)
```

```
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/pre_commit/error_handler.py", line 72, in error_handler
    yield
  File "/usr/local/lib/python2.7/site-packages/pre_commit/main.py", line 344, in main
    return run(args.config, store, args)
  File "/usr/local/lib/python2.7/site-packages/pre_commit/commands/run.py", line 292, in run
    install_hook_envs(hooks, store)
  File "/usr/local/lib/python2.7/site-packages/pre_commit/repository.py", line 220, in install_hook_envs
    hook.install()
  File "/usr/local/lib/python2.7/site-packages/pre_commit/repository.py", line 98, in install
    self.prefix, self.language_version, self.additional_dependencies,
  File "/usr/local/lib/python2.7/site-packages/pre_commit/languages/python.py", line 169, in install_environment
    _make_venv(env_dir, python)
  File "/usr/local/lib/python2.7/site-packages/pre_commit/languages/python.py", line 181, in make_venv
    cmd_output_b(*cmd, env=env, cwd='/')
  File "/usr/local/lib/python2.7/site-packages/pre_commit/util.py", line 141, in cmd_output_b
    raise CalledProcessError(returncode, cmd, retcode, stdout_b, stderr_b)
CalledProcessError: command: ('/usr/local/opt/python@2/bin/python2.7', '-mvirtualenv', '/Users/user name/.cache/pre-commit/repoi4s0y8j6/py_env-python3.7', '-p', 'python3.7')
return code: 3
expected return code: 0
stdout:
    The path python3.7 (from --python=python3.7) does not exist

stderr: (none)

```

Any ideas on how to solve it?关于如何解决它的任何想法?

The error message indicates you do not have python3.7 installed on your machine -- you'll need to install that to use language_version: python3.7错误消息表明您的机器上没有安装 python3.7 - 您需要安装它才能使用language_version: python3.7

perhaps you intend to use black's --target argument instead?也许您打算改用 black 的--target参数?

    -   id: black
        # or configure black in pyproject.toml (probably a better suggestion)
        args: [--target-version, py37]

disclaimer: I'm the author of pre-commit免责声明:我是 pre-commit 的作者

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

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