Tuesday, May 10, 2022

vscode+Poetry+Python

vscode + Python + Virtual Environments

It's been forever since I've posted anything here.  Today's topic is vscode + Python + Virtual Environments.  I think it is always good to start with who this is for and who it is not.  If you've been doing all these things for years, I probably don't have any exciting insights for you.  If you've been doing it for a while and are looking for some process improvements hopefully you've come to the right place.

What if you are a student somewhere? I think it is never to early to pick-up good habits, however you have to balance the fact that most class projects are designed to be disposable.  


What Are We Using

This post is based on the following configuration from most critical to least
  • vscode -- one of the most ubiquitous IDE around 
    • With Pylance, Python
  • Poetry -- a reasonable choice, examples may depend on it
  • linux -- paths and some commands may be unique to linux
  • Python 3.8 -- not really critical


Loading Environments

Here I'm assuming you are already using virtual environments.   If not I'll have an upcoming post about why you should.  If you don't have any virtual environments skip this section. 

The magic is the lower right corner, this shows the currently configured interpreter.  
This shows it selected and the dialog to pick an existing interpreter or add a new one.

Which is great for me because I've already added the interpreters I want.  It is nice that this is independent of the other settings, particularly if you want to test in multiple environments. Note this will only show up if the active file being edited is a Python file. 

So how do you add your environments?  The first thing is to find where the environment you want is.  Let's assume you have a poetry environment in the current directory

If you start a poetry shell you'll see something like the following
:~/brewst2/brewst$ poetry shell
:~/.cache/pypoetry/virtualenvs/brewst-k5XL8EyT-py3.8
:~/brewst2/brewst$ ~/.cache/pypoetry/virtualenvs/brewst-k5XL8EyT-py3.8/bin/activate
(brewst-k5XL8EyT-py3.8)
What we need is the the directory activate is in.  In specific the interpreter we will add is 
~/.cache/pypoetry/virtualenvs/brewst-k5XL8EyT-py3.8/bin/python

This one thing I like about Poetry.  They often do a good job of showing you how a simple step (like poetry shell) can be substituted with a more useful alternative (activate).  vscode doesn't want the activate, but it does want the Python version linked in the same directory.
So, once you have all the interpreters you want loaded your ready for the next step.

Debugging

We want to use the IDE to set breakpoints and run programs.   However, what if your program has command line arguments.  This is were launch.json comes in.  You can open it in a variety of ways.  The easiest is probably have a Python file open the select from the top menu Run->Open Configurations.  

What you need to do is add the arguments your function expects from the command line.  What you need to add to your launch.json is the args you want.  The format is "args":["arg1","value1",...] For example as shown below

{
"args":[
"-build",
"0",
"-run_mode",
"python",
"-update_mode",
"quick",
"-pipeline",
"pipeline_file_load_test.py"
],
}

For more details see Debugging configurations for Python apps in Visual Studio Code


Note there is also an option to specify a python interpreter in the launch.json file.  In my experience this configuration seems to lose out to the selected interpreter.  That isn't what you'd want and for that reason I'd recommend not setting the interpreter in the launch.json.