Python3 and tabs

Hello;

I know my question is more into python, but I am asking it here because it is also related to erpnext.
Currently I am running ubuntu 18 and python 3.6, as I see that all the erpnext code is built based on each tab is 8 spaces.

The problem is:
Using vim, if I pressed tab, it is only move 4 spaces, but as I see that the code of erpnext is build based on each tab is 8 spaces and not 4 spaces. In python 2, I was able to press 2 tabs and it is accepting it, now in python 3, if I press two tabs to be able to make the code align with the remaining code, it is not working and it is considering my tabs as spaces !

How I can resolve this using vim editor with python 3? Is it settings related to vim?

By the way, using notepad plus, I am able to work fine and by pressing 2 tabs in notepad plus, I see it in the unix vim as one tab of 8 spaces.

Regards
Bilal

I know what you mean Bilal,
The code in erpnext seems to have been done with tabs, not spaces.

Pep 8 specifies to try and use spaces, which is how I have my editor set up, instead of tabs. but with python3 you can’t mix. I’ve been trying to convert what I can to the standard 4 spaces, which you will find most python code on the web uses this standard.

I use pycharm as the editor, and it has smarts in it to know what is currently being used in the py file, and it will then keep using that for the rest of the file while your editing it.

However moving to version 12 of erpnext, hopefully things will be standardized to 4 spaces.

I’d just do a search and replace for the whole file you’re editing and set the tabs to 4 spaces instead. Then the file will be consistent.

Hello @cjpit

So I can not use vim?
What is the solution to edit the code from unix CLI (same as we use vim or vi editor)?

By the way: I can understand that python3 is considering each tab to be 4 spaces while ERPNext code currently written as each tab is 8 spaces, correct?

Note: I checked erpnext version 12 and still the code is using that each tab is 8 spaces and not 4 spaces.

Regards
Bilal

ERPNext code was written with tabs. However, those tabs don’t “mean” or “represent” anything. They are not 8 spaces. They are not 4 spaces. They are just tabs.

How those tabs are interpreted, depends on your own machine’s software.

When I open an ERPNext python file, I have configured my editors to display an indentation of 4 spaces, whenever it sees a Tab. But I easily could have chosen 2 spaces, or 100 spaces.

You just need to configure your tools to display whatever your eyes are comfortable with. Just make sure that your editor is actually writing Tabs, though. Don’t write spaces, no matter how many.

(Addendum - I disagree with using tabs, and would rather we used spaces, per pep8. But that’s a whole other topic.)

1 Like

Hello @brian_pond

Are you using python 2 or 3?
Please note that I am using Tabs and it is not working. This problem is only appearing with python 3 (certainly python 3.6) and was not appear in python 2.

Regards
Bilal

Both, depending on the work I’m doing. For ERPNext, I’m using Python 3.7.

Python is just the interpreter of these files, though. On your operating system, these are just ordinary text files.

I don’t use vim. Otherwise I could advise you on configuring how it interprets tabs.

@brian_pond
As you do not use vim, so what do you use as text editor in command line?
Command line editor is important because it enables to change the code at the server without need to do upload the files on the server.

Regards
Bilal

If I need to do a quick edit, through a terminal, I use nano. I don’t have enough experience with vim to be good at it. It has a strong learning curve.

However, most of the time, I don’t edit text files from a shell. I mount the remote directories on my VPS directly to my laptop, using sshfs. Then I use GUI tools. My laptop software doesn’t even know the files aren’t local.

Otherwise, I edit local files. When I’m done, do commits and push to GitHub/GitLab. Then pull down the changes on my remote VPS.

If you use vim and linux 18, you will face the problem that I am facing.

Anyway: what the GUI tools that you are using?
Regards
Bilal