Python Poetry and VS Code (2024)

I’ve done this maybe five times so far and always forget how to do it. If you want to use the Poetry package manager with VS Code, I hope this helps you out! I followed the instructions by this guru I follow and they worked!

  1. Create a Github managed repository on your local drive (be sure to have a README.md).
  2. Open up VS Code.
  3. Open a Terminal.
  4. Within your root code directory type poetry init and follow the maze of menus.
  5. You need to make a new project subdirectory, call it myapp
  6. In your pyproject.toml change the 2nd line where it says name = <blah blah> to instead read:
    [tool.poetry]
    name = "myapp"
  7. Next, put a new file main.py into the new subdirectory
  8. In the main.py you can have a program like the following:
    if __name__ == "__main__":
    print("Happy Birthday!")
  9. Type poetry install and pray that you don’t get any really bad error messages.
  10. If this really works the right way, type poetry shell and you should be in the environment you created. I ran into an error around but it still seemed to work (this time).
  11. Type exit to get back to where you came from
  12. To learn a few more poetry commands:
    • poetry add [package-name]
    • poetry remove [package-name]
    • poetry self:update (to update poetry itself)

Wire it into VS Code

Type poetry env info which for me looks like:

(base) ➜  dit2024-llama-db git:(main) ✗ poetry env info

Virtualenv
Python:         3.10.13
Implementation: CPython
Path:           /Users/maeda/Documents/GitHub/dit2024-llama-db/.venv
Executable:     /Users/maeda/Documents/GitHub/dit2024-llama-db/.venv/bin/python
Valid:          True

System
Platform:   darwin
OS:         posix
Python:     3.10.13
Path:       /Users/maeda/.pyenv/versions/3.10.13
Executable: /Users/maeda/.pyenv/versions/3.10.13/bin/python3.10

Then we want to:

% poetry env info --path
/Users/maeda/Documents/GitHub/dit2024-llama-db/.venv

To then make it addable to your Python environment list on MacOS do:

% poetry env info --path | pbcopy

You then want to bring up the command palette to select your Python Interpreter, and add one manually:

Plop the path you’ve got in your clipboard into Enter interpreter path …

As a result, your terminal and subsequent Python notebooks will use this Poetry environment. Phew.

WAIT: That’s not true. You need to select the environment correctly as it will NOT recommend the right environment from the get go. Be sure to pick the one that’s in your .venv local to your ./ so you get the right stuff from poetry.