'Python Poetry and Script Entrypoints
Im trying to use Poetry and the scripts option to run a script. Like so:
pyproject.toml
[tool.poetry.scripts]
xyz = "src.cli:main"
Folder layout
.
├── poetry.lock
├── pyproject.toml
├── run-book.txt
└── src
├── __init__.py
└── cli.py
I then perform an install like so:
❯ poetry install
Installing dependencies from lock file
No dependencies to install or update
If I then try and run the command its not found (?)
❯ xyz
zsh: command not found: xyz
Am i missing something here! Thanks,
Solution 1:[1]
Poetry is likely installing the script in your user local directory. On Ubuntu, for example, this is $HOME/.local/bin
. If that directory isn't in your path, your shell will not find the script.
A side note: It is generally a good idea to put a subdirectory with your package name in the src
directory. It's generally better to not have an __init__.py
in your src
directory. Also consider renaming cli.py
to __main__.py
. This will allow your package to be run as a script using python -m package_name
.
Solution 2:[2]
You did everything right beside not activating the virtual environment or running that alias (xyz
) via poerty run xyz
. One can activate the virtualenv via poetry shell
. afterwards xyz
should run from your shell.
ps: @jisrael18 is totally right. normally one would have another folder (which is your main python module) inside the src
folder.
.
??? src
? ??? pyproj
? ??? __init__.py
? ??? __main__.py
...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jisrael18 |
Solution 2 |