记事本++缩进混乱
我使用 Python 进行编码,并且非常喜欢 Notepad++。然而,当我使用制表符缩进时,在Notepad ++中似乎很好,但是当我运行程序时,我收到缩进错误,当我在Emacs或其他东西中检查我的代码时,我发现Notepad ++实际上添加了比它显示在屏幕上。怎么了?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
没有通用的制表符大小,所以我总是确保用空格替换制表符(这样你就知道你看到的就是你在其他地方得到的)
。 “偏好设置...”->语言菜单/选项卡设置并选中“按空格替换”
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'
我建议去查看>显示符号>显示空格和制表符以更好地了解缩进的外观。
I would suggest going to View > Show Symbol > Show Whitespace and Tab to get an better idea of how your indentations look.
PEP 8 告诉我们在 Python 中使用空格而不是制表符以实现跨编辑器兼容性和一致性:
http://www.python.org/dev/peps/pep-0008/
看看这个答案,了解如何在 Notepad++ 中将制表符更改为空格
在 Notepad++ 中将制表符转换为空格
也许这会解决您的问题
PEP 8 tells us to use spaces instead of tabs in Python for cross-editor compatibility and consistency:
http://www.python.org/dev/peps/pep-0008/
Have a look at this answer for how to change tabs to spaces in Notepad++
Convert tabs to spaces in Notepad++
Perhaps that will fix your problem
小更新 - 要将空格作为选项卡,您现在转到“设置”>“首选项”>“选项卡设置”并选中“用空格替换”框
Tiny update - to get spaces as tabs, you now go to Settings>>Preferences>>Tab Settings and check the "Replace by space" box
我是 python 新手,开始使用 Notepad++。但我遇到了和你一样的问题......缩进问题。根据前辈的建议,我切换到了 PyCharm 社区版。我将代码从 Notepad++ 粘贴到 PyCharm,它突出显示了存在缩进问题的块...问题是,一些行使用了空格,一些使用了制表符。发生这种情况是因为我正在处理的代码是从互联网上获取的。
检查此类不一致即可解决问题。或者,有更好的选择...切换到 PyCharm。它是专门为 python 编码而构建的。希望这可以帮助像我这样的人在 Notepad++ 中寻找缩进问题的解决方案
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.
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++
使用 Notepad++ 的 Python 缩进插件:
我已经使用过 Pycharm 和Notepad++,坦率地说 - 尽管现在我的机器上都安装了 Notepad++,但我更喜欢使用 Notepad++。因此,如果您想继续使用 Notepad++ 进行 Python 开发,您一定要安装“Python Indent”插件。当你编写Python代码时,这个插件会自动为你创建选项卡。该插件的唯一缺点是,当您想要使用它时,您必须记住启用它(通过转到“插件 --> Python 缩进”,然后单击“启用”)。
要在 Notepad++ 中安装 Python Indent 插件,只需转到“插件”-->; “插件管理器”,然后单击“显示插件管理器”。然后选中“Python 缩进”并单击“安装”按钮。
此外,您应该遵循 Python 的使用指南:
不同程序的制表符间距可能有所不同,根据 PEP8(Python 增强提案编号 8 - 这是 Python 公认的样式和使用指南)的建议,您应该使用空格键进行缩进。请在此处查看有关间距的 PEP8 页面: https://www .python.org/dev/peps/pep-0008/#tabs-or-spaces
祝你好运!
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.
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.
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
Good luck!