mqttwarn icon indicating copy to clipboard operation
mqttwarn copied to clipboard

Running mqttwarn from VSCode

Open psyciknz opened this issue 4 years ago • 14 comments

moved to official image as you know, and have found one of my items not working (in the pushover service I think there's a 2.7 > 3.9 incompatibility).

I think i have a fix for it, but just wanting to run it in debug mode in visual studio code. The structure has changed significantly since I last ran it.

What python file do I execute? And where should the mqttwarn.ini file reside to run?

psyciknz avatar Jun 13 '21 22:06 psyciknz

Dear @psyciknz,

thank you for your report.

I found one of my items not working: In the pushover service I think there's a 2.7 > 3.9 incompatibility.

That could well have happened, apologies. We made the transition to Python 3 the other day but haven't been able to keep up with migrating and verifying all service plugins and just take care about them as we go. We would appreciate any patches for the pushover service plugin and beyond.

What python file do I execute?

The main program file entrypoint is just called mqttwarn, it will invoke mqttwarn.commands:run.

Where should the mqttwarn.ini file reside to run?

By default, it will use a file mqttwarn.ini from the current working directory. Otherwise, you can adjust the MQTTWARNINI environment variable to point to a different configuration file.

With kind regards, Andreas.

amotl avatar Jun 14 '21 08:06 amotl

What python file do I execute?

The main program file entrypoint is just called mqttwarn, it will invoke mqttwarn.commands:run.

If you were running it on linux, what python command (to what file) would you execute: python mqttwarn/core.py? AS I don't see a mqttwarn.py file in the file system any more.

I think I've manage to get around it by creating a run.py module that calls mqttwarn.commands.run().

psyciknz avatar Jun 14 '21 20:06 psyciknz

Hi @psyciknz,

If you were running it on linux, what python command (to what file) would you execute?

The command entrypoint mqttwarn is actually a Python file, it just does not have a .py extension. When installing the package in development mode into a virtualenv (like, pip install --editable=.), the mqttwarn program will be located within its bin/ directory, e.g. .venv/bin/mqttwarn.

I hope VSC will be able to invoke that program somehow?

With kind regards, Andreas.

amotl avatar Jun 15 '21 17:06 amotl

Hi again,

by following the documentation at [1], I am able to confirm that those commands seem to work:

source .venv/bin/activate
pip install debugpy
python -m debugpy --listen 5678 .venv/bin/mqttwarn

With kind regards, Andreas.

P.S.: If you don't want to create a virtualenv by hand or such, you can easily invoke make test in the toplevel directory of the repository's working tree and the process will automatically create a virtualenv in the .venv directory so you should be good to go.

[1] https://code.visualstudio.com/docs/python/debugging#_command-line-debugging

amotl avatar Jun 15 '21 17:06 amotl

Hi again,

have you been able to resolve this issue? Shall we just close the ticket or add anything valuable to the documentation?

With kind regards, Andreas.

amotl avatar Jun 17 '21 19:06 amotl

I think I've partially been able to resolve it. I may have added some vscode specifc files to the repo for run time. I've got one other module it's complaining about when running, but I'm hoping that due to the mqttwarn.ini file I'm using which is based off my main one. I'll cut that down.

But what I might look at, and is probable a better direction for the project, is to create, in this case, a test_pushover.py test class for testing/debugging.

psyciknz avatar Jun 17 '21 20:06 psyciknz

I think I've partially been able to resolve it. I may have added some vscode-specific files to the repo for run time.

That is really sad. Don't you see any way to make VSCode just invoke the main program mqttwarn or its Python entrypoint mqttwarn.commands:run? According to [1,2], you might be able to configure mqttwarn in your launch.json?

[1] https://code.visualstudio.com/docs/python/debugging [2] https://stackoverflow.com/questions/48164843/how-to-run-python-in-visual-studio-code-as-a-main-module

I've got one other module it's complaining about when running, but I'm hoping that due to the mqttwarn.ini file I'm using which is based off my main one. I'll cut that down.

All right. Let us know about the outcome.

But what I might look at, and is probable a better direction for the project, is to create, in this case, a test_pushover.py test class for testing/debugging.

I carried a discussion about that topic forward to #526.

amotl avatar Jun 17 '21 23:06 amotl

I think I've partially been able to resolve it. I may have added some vscode-specific files to the repo for run time.

That is really sad. Don't you see any way to make VSCode just invoke the main program mqttwarn or its Python entrypoint mqttwarn.commands:run? According to [1,2], you might be able to configure mqttwarn in your launch.json?

[1] https://code.visualstudio.com/docs/python/debugging [2] https://stackoverflow.com/questions/48164843/how-to-run-python-in-visual-studio-code-as-a-main-module

Yeah I couldn't figure out what I needed to run it as a module - which I think is the terminology.

I've got one other module it's complaining about when running, but I'm hoping that due to the mqttwarn.ini file I'm using which is based off my main one. I'll cut that down.

All right. Let us know about the outcome.

Yep sorted that, I think it was a lack of function directive in the ini file, so I added the entry and put in a basic samplefuncs.py.

But what I might look at, and is probable a better direction for the project, is to create, in this case, a test_pushover.py test class for testing/debugging.

I carried a discussion about that topic forward to #526.

Ok will conitnue there.

psyciknz avatar Jun 18 '21 00:06 psyciknz

Dear @psyciknz,

thank you for your contribution at #529. However, I was looking for a less invasive patch on that matter. So, can you now please check whether the VSCode launch.json configuration coming from #530 can satisfy your VSCode installation already? On my macOS machine, using VSCodium, it worked like a charm and I was able to interactively set breakpoints and use the debugger successfully.

May I also humbly ask which operating system you are using? On the detail of how to setup the virtualenv stuff (see [1]), we might have to adjust the documentation when it comes to, for example, running that procedure on Windows. In that case, I am kindly asking for corresponding contributions to those guidelines.

With kind regards, Andreas.

[1] https://github.com/jpmens/mqttwarn/blob/main/doc/sandbox.rst

amotl avatar Jun 19 '21 12:06 amotl

Dear @psyciknz,

thank you for your contribution at #529. However, I was looking for a less invasive patch on that matter. So, can you now please check whether the VSCode launch.json configuration coming from #530 can satisfy your VSCode installation already? On my macOS machine, using VSCodium, it worked like a charm and I was able to interactively set breakpoints and use the debugger successfully.

May I also humbly ask which operating system you are using? On the detail of how to setup the virtualenv stuff (see [1]), we might have to adjust the documentation when it comes to, for example, running that procedure on Windows. In that case, I am kindly asking for corresponding contributions to those guidelines.

With kind regards, Andreas.

[1] https://github.com/jpmens/mqttwarn/blob/main/doc/sandbox.rst

I'm on windows, with Visusul studio code.

I don't tend to run venv for any python projects. I've pulled from main back into my master to re-do the publish version changes, and so took the oppurtunity to try the launch.json that was merged into master. I get the following upon running:

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Users\d_col\Code\Git\mqttwarn>  & 'c:\Users\d_col\AppData\Local\Programs\Python\Python39\python.exe' 'c:\Users\d_col\.vscode\extensions\ms-python.python-2021.5.926500501\pythonFiles\lib\python\debugpy\launcher' '53840' '--' 'mqttwarn'
pydev debugger: critical: unable to get real case for file. Details:
filename: mqttwarn
drive: 
parts: ['mqttwarn']
(please create a ticket in the tracker to address this).
Traceback (most recent call last):
  File "c:\Users\d_col\.vscode\extensions\ms-python.python-2021.5.926500501\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd_file_utils.py", line 221, in _get_path_with_real_case
    return _resolve_listing(drive, iter(parts))
  File "c:\Users\d_col\.vscode\extensions\ms-python.python-2021.5.926500501\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd_file_utils.py", line 184, in _resolve_listing 
    dir_contents = cache[resolved_lower] = os.listdir(resolved)
FileNotFoundError: [WinError 3] The system cannot find the path specified: ''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\d_col\.vscode\extensions\ms-python.python-2021.5.926500501\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd_file_utils.py", line 226, in _get_path_with_real_case
    return _resolve_listing(drive, iter(parts))
  File "c:\Users\d_col\.vscode\extensions\ms-python.python-2021.5.926500501\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd_file_utils.py", line 184, in _resolve_listing 
    dir_contents = cache[resolved_lower] = os.listdir(resolved)
FileNotFoundError: [WinError 3] The system cannot find the path specified: ''
pydev debugger: critical: unable to get real case for file. Details:
filename: mqttwarn
drive:
parts: ['mqttwarn']
(please create a ticket in the tracker to address this).
Traceback (most recent call last):
  File "c:\Users\d_col\.vscode\extensions\ms-python.python-2021.5.926500501\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd_file_utils.py", line 221, in _get_path_with_real_case
    return _resolve_listing(drive, iter(parts))
  File "c:\Users\d_col\.vscode\extensions\ms-python.python-2021.5.926500501\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd_file_utils.py", line 184, in _resolve_listing 
    dir_contents = cache[resolved_lower] = os.listdir(resolved)
FileNotFoundError: [WinError 3] The system cannot find the path specified: ''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\d_col\.vscode\extensions\ms-python.python-2021.5.926500501\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd_file_utils.py", line 226, in _get_path_with_real_case
    return _resolve_listing(drive, iter(parts))
  File "c:\Users\d_col\.vscode\extensions\ms-python.python-2021.5.926500501\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd_file_utils.py", line 184, in _resolve_listing 
    dir_contents = cache[resolved_lower] = os.listdir(resolved)
FileNotFoundError: [WinError 3] The system cannot find the path specified: ''

psyciknz avatar Jun 19 '21 20:06 psyciknz

I attempted to follow the instructions from https://github.com/jpmens/mqttwarn/blob/main/doc/sandbox.rst

But none seem to be clear.

PS C:\Users\d_col\Code\Git\mqttwarn> make test
make : The term 'make' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify  
that the path is correct and try again.
At line:1 char:1
+ make test
+ ~~~~
    + CategoryInfo          : ObjectNotFound: (make:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
PS C:\Users\d_col\Code\Git\mqttwarn> python3 -m venv .venv
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
PS C:\Users\d_col\Code\Git\mqttwarn> source .venv/bin/activate
source : The term 'source' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, 
verify that the path is correct and try again.
At line:1 char:1
+ source .venv/bin/activate
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (source:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
PS C:\Users\d_col\Code\Git\mqttwarn>

psyciknz avatar Jun 19 '21 21:06 psyciknz

I see you have added a main.py....this does look similar to my .vscode\run.py, but you'll note I had to include more imports. Perhaps because I am not in a virutal environment?

import sys
#
# To Use this run.py a .env file is needed in the .vscode directory 
# with the location of python path and name/locaiton of the RUNINI file.
# eg
#PYTHONPATH=C:\\Users\\default\\AppData\\Local\\Programs\\Python\\Python39\\Lib\\site-packages
#RUNINI=.vscode\\run.ini

sys.path.append('..\\mqttwarn')
sys.path.append('.vscode')
import attr
from docopt import docopt
#from ..mqttwarn import mqttwarn
import mqttwarn.core
import mqttwarn.util
import mqttwarn.context
import mqttwarn.services
import mqttwarn.commands


mqttwarn.commands.run()

psyciknz avatar Jun 19 '21 21:06 psyciknz

Dear @psyciknz,

I'm on Windows, with Visual Studio Code.

Thank you.

PS C:\Users\d_col\Code\Git\mqttwarn> make test
make : The term 'make' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Apparently, Powershell does not have Unix tools like make available by default. So, the whole Makefile currently can not be used on your end so you might want to try executing some commands manually, like outlined within #539 [1].

# On Windows
python -m venv .venv
.venv/Scripts/activate

pip install --editable=.[test] --upgrade

#539 further tries to improve the situation (on Linux / macOS). However, when following the documentation [2], it might be needed to configure the path to your Python interpreter like that within .vscode/settings.json.

"python.pythonPath": "c:/Users/d_col/Code/Git/mqttwarn/.venv/Scripts/python.exe"

With kind regards, Andreas.

[1] https://github.com/jpmens/mqttwarn/blob/cbcc18a71091eb8661aeca4fce9d0b7a51def986/doc/sandbox.rst#using-vscode [2] https://code.visualstudio.com/docs/python/environments#_manually-specify-an-interpreter

amotl avatar Jun 19 '21 23:06 amotl

Dear @psyciknz,

apologies for letting this slip for so long. We just merged #539, which should improve VSCode support on Linux and macOS already. We will be happy about any report if that also works better on Windows now.

With kind regards, Andreas.

/cc @symonjim


P.S.: I also just found https://donjayamanne.github.io/pythonVSCodeDocs/docs/python-path/, where different details are outlined how to launch VSCode. I would be interested if this may work well now:

git clone https://github.com/jpmens/mqttwarn
cd mqttwarn
python -m venv .venv
.venv/Scripts/activate
pip install --editable=.[test] --upgrade
code .

amotl avatar Aug 21 '22 21:08 amotl