Poetry Python Dependency Management
Reference: Poetry CLI
pipx setup for CLI management​
-
Install pipx for global CLI availability Reference: pipx-in-pipx
pip install pipx-in-pipx -
If the
pipx-in-pipxnot working directly-
Install pipx globally first
pip install pipx -
Install pipx inside pipx
pipx install pipx -
Ensure pipx path in environment variables
pipx ensurepath -
Uninstall pipx in global environment, one inside pipx should be enough
pip uninstall pipx -y
-
Poetry Setup​
-
Install using pipx to have the CLI available globally
pipx install poetry -
Make poetry to create virtualenv inside project
poetry config virtualenvs.in-project true
Poetry Management​
Setup Poetry project​
-
Setup Poetry project using the below command
poetry init -
Provide the required details asked by poetry CLI to create pyproject.toml file
Create or Activate virtual environment​
Virtual environment will also get activated by default while running other commands like 'poetry install'
poetry shell
Install dependencies​
-
Install all dependencies
poetry install -
Install with groups
poetry install with dev -
Sync environment with lock file
poetry install --sync
Update dependencies​
-
Update all dependencies
poetry update -
Update specific dependencies
poetry update requests toml
Install new dependencies​
-
Install latest version
poetry add requests pendulum -
Install and allow minor version update
(Allow >=2.0.5, <3.0.0 versions)poetry add pendulum@^2.0.5 -
Install and allow bugfix version update
(Allow >=2.0.5, <2.1.0 versions)poetry add pendulum@~2.0.5 -
Install without upperbound
(Allow >=2.0.5 versions, without upper bound)poetry add "pendulum>=2.0.5" -
Install only specified version
(Allow only 2.0.5 version)poetry add pendulum==2.0.5 -
Add git dependency
poetry add git+[https://github.com/sdispater/pendulum.git](https://github.com/sdispater/pendulum.git) -
Add local dependency
poetry add ../my-package/dist/my_package-0.1.0.whl -
Add dependency to a group. The below command will add pytest to dev dependencies
poetry add pytest -G dev
Uninstall dependencies​
-
Uninstall a dependency
poetry remove pendulum -
Uninstall a dependency from a specific group
poetry remove pytest --group dev
List dependencies​
-
List all dependencies
poetry show -
List all dependencies with branching
poetry show --tree -
List specific dependency
poetry show pendulum
Build Project​
poetry build
Publish Project​
poetry publish
Internal PyPI​
Internal pypi server can be hosted in pc via pypiserver package
Setup Internal Server (Server Side)​
-
Install
pypiserverin pipxpipx install pypiserver -
Setup a folder to act as pypi server
Recommendation: Create a sub directorypackages -
Run the server.
pypi-server run -p 8080 -P . -a . --hash-algo=sha256 packages-p 8080: Port to be used to serve
-P . -a .: No password file and no authentication
--hash-algo=sha256: hash algo to generate links
packages: path to publish packages
Publish to internal server via poetry (Client Side)​
-
Configure the internal PyPI URL in poetry
poetry config repositories.internal_pypi http:/localhost:8080 -
Publish to internal PyPI
poetry publish -r internal_pypi