VSCode "Open file in editor" hover not working in Python - python

When I get a syntax error in Python I think VSCode is supposed to offer a mouse hover over the error message, which lets you Ctrl-click to go to the errored line in the editor.
I'm not getting the hover on the final line in the traceback (but it works OK on earlier lines).
I think it's some kind of configuration problem - but what?
This does work - I get "Open file in editor" (mouse at red arrow):
But this doesn't work (no hover message; mouse at red arrow):
(naturally, per Murphy's Law, the latter is the one I want to work, as the error is in fact on line 2171.)

UPDATE:
The problem is that there are spaces in your file path.
To fix it, change your folder name to make the file path recognizable. Generally, the file path should be in full English, with no spaces and no special symbols.
Hover over the file path to see the underscore including the line number. At this point Ctrl + click will navigate to the corresponding line number of the corresponding file.

Related

Pycharm TabError: inconsistent use of tabs and spaces in indentation

In Pycharm I keep running into this error:
TabError: inconsistent use of tabs and spaces in indentation
I know its a problem with tabs/spaces.
I want:
if len(myresults)==0:
print("TEST")
Whenever I type, pressing enter after every line typed I actually type:
if len(myresults)==0:
print("TEST")
Causing this error. How do I fix it? Here are my setting s for pycharm:
Pycharm Settings
I'm probably missing something obvious, but I simply cannot find it.
Try,
In the "Settings | Editor | Code Style" -- try disabling "Detect and use existing file indents for editing" in case if you have it enabled (it is by default). NOTE: re-opening file in editor may be required.
Do you have any .editorconfig files anywhere in the path of that file? Settings from .editorconfig have priority (will overwrite) over your IDE settings.
Go to Settings --> Editor --> Code Style --> Python --> Tabs and Idents.
Here activate (checkbox) Use tab character AND Smart tabs
Thats works for me.
What worked for me:
"Settings | Editor | Code Style" -- disable "Detect and use existing file indents for editing" (as #Nipun Sampath suggestted)
Together with:
"Settings | Editor | Code Style | Python" -- disable "Use tab character"
For reformatting the file: Ctr+Alt+L -> 'Code cleanup' on either selected text or whole file (depending on what is needed)
I'm using Pycharm and Jupyter Notebook and had the same problem with both of them. I could not fix it with "convert Indents", So I uninstalled some of the modules that I was using in my programm and reinstall them and worked for me.
I'm using Pycharm 2019.1. For me, this error constantly appeared always I pressed Enter to write a new line, and I have to manually rewrite every indentation in order to disappear
the red sub-lines that indicate error. I fixed it by analyzing the full code into another text editor (kate editor in my case, but you can use another one). I verified there's some indentations written as [TAB] and the most of them written as four simple spaces. So I replaced all indentations written as [TAB] by indentations written as four spaces (most of the editors replace by using [Ctrl R] shortcut) and... voilà. Everything works fine.
Note: I couldn't do the replace into Pycharm editor itself. Apparently pycharm editor does not differentiate a [Tab] of four spaces when you try replace by [Ctrl R].
Hope it help future users.

File with "|"s in Atom editor has smiley faces printed from Python; split("|") doesn't work

I have an input file I'm trying to process with Python, which appears to have content like the following:
# This works, when run at a REPL
line = 'aababasdf|75=2|asdfa|150=17|asdfasdf'
date = line.split('|75=')[1].split('|',1)[0]
When I run the above by hand, or copy-and-paste the file's contents from Atom, it works. However, when I have the Python open the file and read the line itself, it fails:
# This fails, reading from the file from which contents were copy-and-pasted
with open(filename) as curfile:
for line in curfile:
date = line.split('|75=')[1].split('|',1)[0]
This code fails with an IndexError: the split() creates only a single segment, so no [1] segment exists.
When I print the line from the file-based code, it prints smiley faces where the |s should be.
What could be going wrong here? How can I better debug this scenario?
If you're running this from the Windows console (code page 437) there are two vertical bar characters: b'\x7c' and b'\xb3'. The first is part of the ASCII character set, and the second is one of the line-drawing characters that were part of the original PC.
>>> print(b'\x7c\xb3'.decode('cp437'))
|│
In addition you appear to be using a text editor that shows b'\x01' as a vertical bar as well. That's a non-standard way of displaying that character, which is generally invisible since it's an ASCII/Unicode control character.
Once you've figured out the actual character in the file, you can substitute it in your split call.

Line comments in Pycharm

Hi when I comment out a line in Pycharm using the ctrl+slash keyboard shortcut, I get
# code
but instead I want
##Code
like in IDLE.
Is this possible?
You can register a new file type and at the time of registration specify comment characters. You need to reassign *.py to this new type from the default Python file type that comes with installation.
Step by step procedure:
Ctrl+Alt+S to open File Types dialogue.
Alt+Insert (or click on +) to create a new file type
In the new File Type dialogue for Syntax Highlighting -> Line comment: provide ##
Provide rest of the details (including Name and Description) as needed and click OK.
Select the newly created File Type and in Registered Patterns section click + to add a wildcard.
In Add wildcard dialogue box enter *.py and click Ok
Since *.py is already assigned to the default type you will be asked to confirm Reassign wildcard. Do so.
Apply and Ok
Note: We have to do all this because we can not edit default file types and Python is a default file type.
Now, open a Python file (.py extension) and check Ctrl+Slash on any line. You should now see ## prepended to the line.
Ctrl+Slash on any line comments the line i.e. Ctrl + /

How do I mark a .txt file as "not executable"?

I am hosting a text file on OpenShift inside the cron/minutely directory. The server is trying to run the text file every minute, only I don't want it to: all I want the text file to do is stay on the server so I can save useful information inside it.
How do I tell the server that I want it to ignore the text inside the file instead of running it as code? Is there a way to do it by adding a specific "shebang" line at the beginning? Is it even possible to keep the text file in the cron/minutely directory without it being executed?
Thank you.
You can add this as the first line of the text file:
#!/usr/bin/true
That's a sort of fake shebang line. It means that if the file is executed, the "interpreter" to "run" the file is the true program, which is a trivial program which does nothing but return the status code for success (0).
Therefore, a true shebang will make any file "successfully" execute, but do nothing.
Please don't take this to mean that leaving data files in a directory meant for executables is a good idea; it's not. This is just a hack of last resort.

Spyder IDE: How do you configure default end-of-line character?

I am using Spyder IDE to develop code and the IDE currently has default end-of-line character set to be CRLF. I would like to use '\n' instead because all my existing source code uses '\n' and so I don't want to have a bunch of new files that use a different end-of-line character.
Is there a way to specify the default end-of-line character in Spyder IDE? If yes, how?
(Spyder maintainer here) To configure the end of line characters you want to use, you need to go to the menu
Tools > Preferences > Editor > Advanced settings
then to the section End of line characters and select the ones that Spyder will use on save.
I just started exploring Spyder 4.1.5 (unfortunately could not get 5.1.5 installed), and found this for EOL settings:
'Tools' > 'Preferences' > choose 'Editor' in the list on the left > 'Advanced setting' tab on the right > at the bottom is the 'End-of-line characters' section.
To convert existing CRLF to LF, select the LF option in the dropdown, then for each file you want to convert, make any edit and save the file and voila. (I think you have to do the edit+save step file-by-file.)
As Carlos Cordoba wrote, Spyder does not allow setting the line ending but it does respect the line endings of a file.
If a file has LF line endings and you hit enter, it wont add CRLF even if you are on windows.
My solution is to change the line endings of a new file with notepad++ (edit-> EOL conversion)
For many existing files you can use the replace function, explained here

Categories

Resources