简体   繁体   English

为什么不需要加载 Python 格式化程序(黑色)和 linter(pylint)和 vs 代码?

[英]Why no need to load Python formatter (black) and linter (pylint) and vs code?

I am learning how to use VS code and in the process, I learnt about linting and formatting with "pylint" and "black" respectively.我正在学习如何使用 VS 代码,在此过程中,我分别了解了"pylint""black" "pylint"和格式化。

Importantly, I have Anaconda installed as I often use conda environments for my different projects.重要的是,我安装了 Anaconda,因为我经常在不同的项目中使用 conda 环境。 I have therefore installed "pylint" and "black" into my conda environment.因此,我在"pylint"环境中安装了"pylint""black"

My questions are as follows:我的问题如下:

  • If "pylint" and "black" are Python packages, why do they not need to be imported into your script when you use them?如果"pylint""black"是 Python 包,为什么在使用它们时不需要将它们导入到您的脚本中? (ie "import pylint" and "import black" at the top of a Python script you want to run). (即,您要运行的 Python 脚本顶部的"import pylint""import black" )。 I am very new to VS code, linting and formatting so maybe I'm missing something obvious but how does VS code know what to do when I select "Run Linting" or "Format document" in the command palette?我对 VS 代码、linting 和格式化非常陌生,所以也许我遗漏了一些明显的东西,但是当我在命令面板中选择“运行 Linting”或“格式化文档”时,VS 代码如何知道该怎么做? Or is this nothing to do with VS code ?或者这与 VS 代码无关?

I guess I am just suprised at the fact we don't need to import these packages to use them.我想我只是惊讶于我们不需要导入这些包来使用它们。 In contrast you would always be using import for other packages (sys, os, or any other).相比之下,您将始终对其他包(sys、os 或任何其他包)使用 import。

  • I'm assuming if I used a different conda environment, I then need to install pylint and black again in it right?我假设如果我使用了不同的 conda 环境,那么我需要在其中再次安装 pylint 和 black 对吗?
  1. Yes, black and pylint are only available in the conda environment you installed them in. You can find them in the "Scripts"-folder of your environment.是的, blackpylint仅在您安装它们的 conda 环境中可用。您可以在您的环境的“脚本”文件夹中找到它们。
  2. VS Code knows where to look for those scripts, I guess you can set which package is used for "Run Linting" or "Format document". VS Code 知道在哪里查找这些脚本,我猜您可以设置用于“运行 Linting”或“格式化文档”的包。
  3. You only need to import python modules or functions that you want to use inside your python module.您只需要import要在 python 模块中使用的 python 模块或函数。 But that's not what you do.但这不是你要做的。

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

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