Python 记事本++缩进搞砸了

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3366499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:40:30  来源:igfitidea点击:

Notepad++ indentation messes up

pythonnotepad++indentation

提问by iman453

I'm coding in Python and I really like Notepad++. However, off late when I use tab to indent, it seems fine in Notepad++, but when I run the program I get an indentation error, and when I check my code in Emacs or something, I find that Notepad++ actually adds more tab spaces than it shows on screen. What is happening?

我用 Python 编码,我真的很喜欢 Notepad++。但是,当我使用制表符缩进时,在 Notepad++ 中似乎没问题,但是当我运行程序时出现缩进错误,当我在 Emacs 或其他东西中检查我的代码时,我发现 Notepad++ 实际上添加了更多的制表符空格它显示在屏幕上。怎么了?

采纳答案by ikottman

There is no universal tab size, so I always make sure to replace tabs by spaces (so you know what you see is what you get everywhere else as well)

没有通用的标签大小,所以我总是确保用空格替换标签(所以你知道你看到的就是你在其他地方得到的)

Go to Settings -> "Preferences..." -> Language Menu/Tab Settings and check 'Replace by space'

转到设置->“首选项...”-> 语言菜单/选项卡设置并选中“按空格替换”

回答by Ashley Grenon

I would suggest going to View > Show Symbol > Show Whitespace and Tab to get an better idea of how your indentations look.

我建议去查看 > 显示符号 > 显示空白和制表符以更好地了解缩进的外观。

回答by Nobody

PEP 8 tells us to use spaces instead of tabs in Python for cross-editor compatibility and consistency:

PEP 8 告诉我们在 Python 中使用空格而不是制表符来实现跨编辑器的兼容性和一致性:

http://www.python.org/dev/peps/pep-0008/

http://www.python.org/dev/peps/pep-0008/

Have a look at this answer for how to change tabs to spaces in Notepad++

看看这个答案,了解如何在 Notepad++ 中将制表符更改为空格

Convert tabs to spaces in Notepad++

在记事本++中将制表符转换为空格

Perhaps that will fix your problem

也许这会解决你的问题

回答by NotAnAmbiTurner

Tiny update - to get spaces as tabs, you now go to Settings>>Preferences>>Tab Settings and check the "Replace by space" box

微小更新 - 要将空格作为选项卡,您现在转到设置>>首选项>>选项卡设置并选中“按空格替换”框

回答by Vikas

I am new to python and started using Notepad++. But I faced the same issue as you... Indentation problems. On my senior's advice, I switched to PyCharm community edition. I pasted the code from Notepad++ to PyCharm and it highlighted the block with indentation problems... The issue was that, some of the lines used spaces and some used tabs. This happened because the code on which I was working was taken from the internet.

我是 python 新手,开始使用 Notepad++。但是我遇到了和你一样的问题......缩进问题。在我前辈的建议下,我切换到 PyCharm 社区版。我将代码从 Notepad++ 粘贴到 PyCharm,它突出显示了有缩进问题的块……问题是,有些行使用了空格,有些使用了制表符。发生这种情况是因为我正在处理的代码来自互联网。

Checking for such inconsistencies would solve the problem. Or, there is a better alternative... Switch to PyCharm. It is exclusively built for python coding.. Hope this helps people like me searching for solutions for indentation issues in Notepad++

检查此类不一致将解决问题。或者,有更好的选择……切换到 PyCharm。它专为 Python 编码而构建。希望这能帮助像我这样的人在 Notepad++ 中寻找缩进问题的解决方案

回答by Jaxian

Use Python Indent Plugin for Notepad++: I have used both Pycharm & Notepad++, and frankly - even though both are installed on my machine right now I prefer using Notepad++. So, if you want to continue using Notepad++ for Python development you should definitely install the 'Python Indent' Plugin. This plugin will automatically create tabs for you when writing Python code. The only downside to the plugin is you have to remember to Enable it (by going to Plugins --> Python Indent and then clicking 'Enable') when you want to use it.

为 Notepad++ 使用 Python Indent 插件:我已经使用了 Pycharm 和 Notepad++,坦率地说 - 尽管现在我的机器上都安装了它们,但我更喜欢使用 Notepad++。因此,如果您想继续使用 Notepad++ 进行 Python 开发,您绝对应该安装“Python Indent”插件。这个插件会在你编写 Python 代码时自动为你创建标签。该插件唯一的缺点是您必须记住在您想要使用它时启用它(通过转到插件 --> Python 缩进,然后单击“启用”)。

To install the Python Indent plugin in Notepad++ just go to 'Plugins' --> 'Plugin Manager' and then click on 'Show Plugin Manager'. Then check off 'Python Indent' and click on the 'Install' button.

要在 Notepad++ 中安装 Python Indent 插件,只需转到“插件”-->“插件管理器”,然后单击“显示插件管理器”。然后勾选“Python Indent”并单击“安装”按钮。

Additionally, you should Follow Python's Usage Guide: Tab spacing can differ across programs and, following the recommendations of PEP8 (Python Enhancement Proposals Number 8 - which is Python's Accepted Styling and Usage Guide) you should use the space bar to make your indents. Check out the PEP8 Page about spacing here: https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces

此外,您应该遵循 Python 的使用指南:制表符间距可能因程序而异,并且按照 PEP8(Python 增强提案编号 8 - 这是 Python 接受的样式和使用指南)的建议,您应该使用空格键进行缩进。在此处查看有关间距的 PEP8 页面:https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces

Good luck!

祝你好运!