-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
__name__ in python script, when sent to interpreter is '' #21989
Comments
That's unfortunately not possible as Python sets that value, not us. |
@brettcannon thanks for the feedback, this is a big usability issue IMO, maybe we should keep it open and put an "blocked-upstream" tag to not mix it up with other requests? Moreover, the line number in the stacktrace of code that causes them don't match anything in the editor, it makes it very difficult to figure anything with the code. @gvanrossum @dsyme, I believe this makes the interpreter much less useful than it could to people writing python code:
F# interpreter and how it is integrated with main editors supports this through, with the editor sending hidden directives such as : #cd "/path/where/the/script/I/just/evaluated/a/snippet/from/"
#line 10 "file.fsx" I belive the python interpreter could handle something similar to make it usable with same attributes I described about F# interpreter. Please let me know if I should bring the issue to another repository / another location. Thanks a lot! |
This comment was marked as outdated.
This comment was marked as outdated.
@smoothdeveloper please don't @ mention other people not involved with this repo as that comes off as spamming others (i.e., Don has nothing to do with Python and this has nothing to do with Guido; I am a Python core developer and member of the Python steering council, so please don't loop in other people as I am fully empowered to handle this).
No, you just have to make sure
Python automatically handles this. Your bug report was about sending a single line to the REPL that's trying to print If you would like help with Python itself, https://discuss.python.org/ is probably a good place to go. |
@brettcannon thanks for the feedback, let's not speculate how I come across to the persons I mentioned, or assume I did wrong here, thanks for specifying you are involved with the Python language itself.
To clarify, my use case is not to load an adjacent python file (for which I'd need to alter the sys.path, and deal with the python module system, which I agree, is different than F#), see for example:
#test.py in same folder as test.csv
import pandas as pd
pd.read_csv("test.csv")
This works fine, but if I try to evaluate the code from the same file in vscode, it fails to locate test.csv file.
Notice that in the stacktrace, instead of having the location within test.py, I get Similarly, when I evaluate this: print(os.path.abspath(os.path.curdir)) I get the equivalent of When I execute the script from shell, I get the equivalent of As far as I understand, this is something that will end up having to be handled in this extension, by letting the interpreter know the location of submitted code, and possibly changing the working directory before submitting the code. Note that in jupyter notebooks, it doesn't suffer from the same issue, I assume it does the operations I describe. I'm ok opening topics on the python forum about the two issues (relative path and stack trace not showing proper location), and will look at the forum, but still have a gut feeling the extension itself will need to be adjusted, more à propos here than on the forum which touches on the language itself. Thanks again for sharing your thoughts and guidance. |
That's by design because you typed your code into the REPL via stdin, hence that being the fake file name. Otherwise since you are not operating from a file there isn't really a concept of a file to specify.
That's all going to depend on how you launched Python (and I advice you use
As I said, this isn't under our control and Python does not directly provide a mechanism to fake a file name for the REPL, nor do I even have a good idea of what fake file name to specify since the REPL has no path to speak of.
I don't know what Jupyter does in this instance, but it would be specific to Jupyter and not Python itself which is what we launch on your behalf when you run Python's REPL.
As I have said, there is nothing here for the extension to adjust as what you are asking for is not how Python's REPL operates. As such, I continue to consider this issue closed and will probably not reply further to this issue. |
@brettcannon, it is a bit of a shame we can't find a middle ground to discuss the two usability issues I brought, which limit the user friendliness of the REPL integration that this extension provides.
I'm not typing any code in the REPL, I'm selecting it in a file in the vscode text editor, this extension is doing the "typing in" when I use the action to submit the code I selected. How the F# vscode extension does: How the F# REPL does: Achieving the same usability (focusing on the file name and line number in stack traces for now) in python will require both the interpreter and the extension to collaborate, and if we'd agree it would make the world better, we could break it down into:
Is there any interest to change the status-quo? How can we collaborate to get in that direction? |
Correct, which makes it the same as if you had copy-and-pasted it in or typed it all out manually. We aren't doing anything special here with the REPL is my point.
Unfortunately I personally don't have the time to push for this sort of change upstream. We are looking into what a VS Code-specific REPL experience may look like, and in that scenario we may control enough to be able to handle mapping tracebacks back to the code that caused it (but no promises).
You definitely don't need me to propose such a change upstream. You can go to discuss.python.org and there's an Ideas category where you can bring up proposals. See https://peps.python.org/pep-0001/ for what it takes to author a PEP. |
Thanks for the additional considerations @brettcannon, and it is good to know that the REPL experience is under consideration, in a more general sense, even if my particular issue / suggestion is not something that gets into the scope. I've submitted the idea, and based on your feedback, I may open an issue specific to the backtraces, just to track it separately. |
steps to reproduce
make a python file:
open in VS Code
select the line, and send to the interpreter
expected results
it should print the file path of the script I sent the code from, if it is from a file buffer
actual results
it prints an empty string
see also, when I do the same with F# .fsx script:
It does what I want.
The text was updated successfully, but these errors were encountered: