- Set up a project
Poetry is the current best choice for Python application dependency and environment management. It uses two files
to manage dependencies instead of
Set up a project
Make sure you properly setup pyenv first. Add the following to
export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH" if command -v pyenv 1>/dev/null 2>&1; then eval "$(pyenv init -)" fi
$ pyenv global 3.7.4
to set the global default Python version to 3.7.4.
Run the following command to install
$ curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python
Now, to make sure
poetry use the right Python version, add an alias to
alias poetry="python3 $HOME/.poetry/bin/poetry"
Create a new project
$ poetry new <new_project_name>
It will create a directory
<new_project_name> in the current directory.
Go inside and type
tree (assuming you have
tree installed, on MacOS run
brew install tree) you will see
. ├── README.rst ├── <new_project_name> │ └── __init__.py ├── pyproject.toml └── tests ├── __init__.py └── test_<new_project_name>.py
with a prefilled
[tool.poetry] name = "<new_project_name>" version = "0.1.0" description = "" authors = ["Your name <your email>"] [tool.poetry.dependencies] python = "^3.7" [tool.poetry.dev-dependencies] pytest = "^5.2" [build-system] requires = ["poetry>=0.12"] build-backend = "poetry.masonry.api"
"Your name <your email>" from your
$ git config --global user.email <email>
to change the git email address if you need to.
Now, we can add a package, for example,
$ poetry add flask
And a virtual environment will be created automatically at
/Users/<username>/Library/Caches/pypoetry/virtualenvs/ by default, and
flask will be installed there.
Check the lock file for detailed subdependencies.
Activate that virtual environment in shell
Run the following command to activate the virtual environment.
$ poetry shell
Set the interpreter in VSCode
As of June 2020, the Python extension in VSCode doesn’t support automatic discovery of poetry environments. Refer to this issue.
The current workaround is to add the path into VSCode setting
Now we can see the virtual environments created by
poetry in the list in VSCode.
Check the official docs for poetry commands.