Skip to content
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

Debug console print() statements return printed object + None #678

Closed
ReveStobinson opened this issue Jul 23, 2021 · 15 comments
Closed

Debug console print() statements return printed object + None #678

ReveStobinson opened this issue Jul 23, 2021 · 15 comments
Labels
enhancement New feature or request
Milestone

Comments

@ReveStobinson
Copy link

Environment data

  • VS Code version: 1.45.1
  • Extension version: 2020.6.91350
  • OS and version: Windows 10 Enterprise, version 1909, Build 18363.1679
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.7.0
  • Type of virtual environment used: virtualenv
  • Relevant/affected Python packages and their versions: N/A
  • Relevant/affected Python-related VS Code extensions and their versions: N/A
  • Value of the python.languageServer setting: Jedi

Expected behaviour

Calling print(x) in debug console with "redirectOutput": true in launch.json prints x to the debug console

Actual behaviour

Behavior instead looks like this:

>>>print("Hello world!")
Hello world!
None

Notes

This is occurring after I found and implemented the fix for microsoft/ptvsd#2036. Before, any print statement always just returned None. After adding "redirectOutput": true to my launch.json, print statements print the desired object, but the None still remains on the next line.

Changing "console": from "integratedTerminal" to "integratedConsole" in launch.json also seems to have no effect.

@ReveStobinson ReveStobinson changed the title Debug console print statements return printed object + None Debug console print() statements return printed object + None Jul 23, 2021
@karthiknadig karthiknadig transferred this issue from microsoft/vscode-python Jul 23, 2021
@fabioz fabioz added the enhancement New feature or request label Jul 29, 2021
@fabioz
Copy link
Collaborator

fabioz commented Jul 29, 2021

From what I can tell, the "redirectOutput": true isn't needed (the extension version: 2020.6.91350 seems to be pretty old -- please try the one with the latest debugger (2021.7.1060902895)).

Now, None is still printed in the end.

Right now this is by design: if the code can be evaluated as a single statement and it has a return, that return is shown (whether it's None or not).

Note that it's shown with a different color from the output color (which VSCode uses to signal that this is the result of the evaluation).

We could probably do a special-casing just for the print function and don't provide a return for VSCode in this case, but I'm not sure if it's worth it or not.

Opinions @karthiknadig @int19h?

Also, I'm curious, @ReveStobinson why are you using print at all (in general, you could just type in the variable you want and it'd be printed -- calling the print seems to be redundant for me).

@karthiknadig
Copy link
Member

/cc @luabud

@int19h
Copy link
Collaborator

int19h commented Aug 2, 2021

The standard Python REPL doesn't output None when the expression produces that; we should probably just do the same. I'm not entirely sure how to wire it up to VSCode, though. Perhaps if we see the evaluation context equal to "repl", report the expression as not returning a value at all if it returned None?

@luabud
Copy link
Member

luabud commented Aug 4, 2021

I like Pavel's point about matching the same behaviour as the Python REPL 😊

@pabloazurduy
Copy link

pabloazurduy commented Apr 14, 2022

Hi everyone,
I have a similar issue as the one described in #88362, I've tried everything, but print('str') still don't appear anywhere. I have the following configuration launch.json for some reason I've tried all the configurations possible:
with "redirectOutput": truewith "console": "internalConsole", also with integratedTerminal and so on, I have another mac that shows the prints even using the same configuration. I seriously don't know what's going on

  // Use IntelliSense to learn about possible attributes.
   // Hover to view descriptions of existing attributes.
   // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
   "version": "0.2.0",
   "configurations": [
       {
           "name": "Python: Current File",
           "type": "python",
           "request": "launch",
           "program": "${file}",
           "env": {//"DISPLAY":":1",
                   //"PYTHONPATH": "${workspaceRoot}",
                   //"PYDEVD_WARN_EVALUATION_TIMEOUT": "500",
                   //"PYDEVD_UNBLOCK_THREADS_TIMEOUT": "500",
                   //"PYDEVD_THREAD_DUMP_ON_WARN_EVALUATION_TIMEOUT":"true"
               },
           "cwd": "${workspaceFolder}",
           "console": "internalConsole",
           "redirectOutput": true
       }
   ]
}

Any ideas ?
I have the same issue as the one described here

@fabioz
Copy link
Collaborator

fabioz commented Apr 14, 2022

Can you provide the logs for the run?

i.e.:

  • Run the command Extensions: Open Extensions Folder
  • Locate the Python extension directory, typically of the form ms-python.python-2022.*.****
  • In that directory ensure you do not have any debug*.log files, if you do, please delete them
  • Go back into VS Code and modify your launch.json to add the setting "logToFile": true, see below:
{
    "name": "Python: Current File (Integrated Terminal)",
    "type": "python",
    "request": "launch",
    "program": "${file}",
    "console": "integratedTerminal",
    "logToFile": true
}
  • Start debugging
  • When done, go back into the extension directory and upload the debug*.log files into this GitHub issue.

@pabloazurduy
Copy link

pabloazurduy commented Apr 14, 2022

hi @fabioz
I tried using the configuration that you gave me
image
but for some reason the degug.log file never appeared in the extension folder
image
image
do you know what might be happening ?

@fabioz
Copy link
Collaborator

fabioz commented Apr 14, 2022

I believe it's not picking up that launch configuration when you're launching (you're probably launching through the dropdown). To launch using the launch configuration you need to run it in the debug view.

i.e.:

image

@pabloazurduy
Copy link

pabloazurduy commented Apr 14, 2022

ohhh I'm running it via the TESTING tab using the debug button, the thing is I'm running tests (via-pytest)

image

adding "purpose": ["debug-test"], might solve that issue ?

update: it worked, files attached
update2: files removed ;). I add the solution in a comment below

@karthiknadig
Copy link
Member

@pabloazurduy That is correct. Adding "purpose": ["debug-test"], will mark the configuration to use when run via test explorer.

@pabloazurduy
Copy link

pabloazurduy commented Apr 14, 2022

Another update: I've tried with the configuration, but launching using the run and debug tab instead the testing tab. I launched the module that I was testing (not the test file, but directly the module) adding a main, using a breakpoint worked

        {
            "name": "Python: Current File (Integrated Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "logToFile": true,
            "purpose": ["debug-test"], 
            "redirectOutput": true,
            "env": {"PYTHONPATH": "${workspaceRoot}"}
        }

image

still is for some reason this is not working when launching the pytest from the testing tab, I should be using the same config no ?. I don't understand where the problem is coming from.

@fabioz
Copy link
Collaborator

fabioz commented Apr 14, 2022

@pabloazurduy this is because pytest captures the output.

See: #899 (comment)

@pabloazurduy
Copy link

pabloazurduy commented Apr 14, 2022

Thanks, @fabioz ! that was it !!!!
(Edited)
I tried changing the parameters from vscode to pytest so it doesn't redirect the output, it's very annoying not to have a print statement working especially when you want to debug some objects and scripts that have stdout or iterate and print.

anyway, as @fabioz mentioned in the #899 comment this is happening since the last version of debugpy v1.6.0 which is automatically upgraded by vscode since the 2022.4.0 therefore the workaround that worked for me was to downgrade the version of the python extension for the one before v2022.2.1924087327 that solved the print()function in the debug console.

that was also the reason why it worked in my other mac, the debugpy version was v1.5.1

tldr

downgrade your vscode python extension to v2022.2.1924087327 that will do the trick

@avraam-inside
Copy link

Спасибо,@fabioz! это было!!!! (Отредактировано) Я предложил изменить параметры с vscode таким образом pytest, чтобы он не перенаправлялся в сторону, очень раздражает отсутствие printработы оператора, особенно когда вы хотите отлаживать некоторые объекты и сценарии, которые имеют стандартный вывод или итерацию и печать.

в случае возникновения, как@fabiozупоминается в комментариях # 899 , это происходит с тех пор, как последняя версия debugpy v1.6.0автоматически обновляется vscode, поскольку обходной путь, который сработал для меня, заключен в том, чтобы понизить версию расширения python для той, которая была до того, как эта функция была решена в консоли отладки .2022.4.0v2022.2.1924087327``print()

это также было причиной, по которой он работал на моем компьютере Mac, отладочная версия былаv1.5.1

tldr

Понизьте расширение vscode python до v2022.2.1924087327того, что поможет

Thanks! It works!

I hope that the extension developers will fix this problem in the next versions

@stev-0
Copy link

stev-0 commented May 9, 2024

Although it's a bit of a faff, you can also add the following to your settings.json. This is global though so you need to change it each time you want to change the output.

"python.testing.pytestArgs": [
     "-s",
 ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

9 participants