VSCode: Adding Poetry Python Interpreter
I’ve been trying out Python’s Poetry dependency management tool recently and I really like it, but couldn’t figure out how to get it setup as VSCode’s Python interpreter. In this blog post, we’ll learn how to do that.
One way to add the Python interpreter in VSCode is to press Cmd+Shift+p
and then type Python Interpreter
.
If you select the first result, you’ll see something like the following:
When I create a virtual environment directly it’ll usually appear on the list, but Poetry wasn’t. I went a-searching and came across a StackOverflow thread where people were experiencing the same problem.
I tried some of the suggestions, but they mostly didn’t work for me. I did, however, realise that I’d be able to add the interpreter manually if I knew its path, which led me to this PR.
This led me to the following command which gives us that answer:
poetry env info
Virtualenv
Python: 3.11.4
Implementation: CPython
Path: /Users/markhneedham/projects/duckdb-0.8/sentiment-analysis/.venv
Executable: /Users/markhneedham/projects/duckdb-0.8/sentiment-analysis/.venv/bin/python
Valid: True
System
Platform: darwin
OS: posix
Python: 3.11.4
Path: /opt/homebrew/opt/python@3.11/Frameworks/Python.framework/Versions/3.11
Executable: /opt/homebrew/opt/python@3.11/Frameworks/Python.framework/Versions/3.11/bin/python3.11
If we want to only get the path, we can pass in --path
:
poetry env info --path
/Users/markhneedham/projects/duckdb-0.8/sentiment-analysis/.venv
We can then pipe that to our clipboard:
poetry env info --path | pbcopy
And now we can go back to the Python interpreter window and paste in the result.
Job done!
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.