Use poetry to create templated Python projects

Question:

Having struggled with Python package management, I have come to like Poetry. I am (mostly) able to use it without issues and installing packages is working well for me.

However, I find myself repeating the same pattern over and over again:

poetry new my_new_package
cd my_new_package
poetry add numpy pandas matplotlib rich ipython black scikit-learn scipy mypy
rm README.rst
touch README.md
git init 

I.e., there are a few packages that I always want installed, I always want to run git init, and I prefer the .md readme over the .rst one.

Oh, and I also change python = "^3.10" to python = "~3.10" in the pyproject.toml.

My wish is that I can do something like poetry from template pyproject.toml instead of all of the above. Now I get that, if I just run poetry install pyproject.toml with the pyproject.toml file from above, poetry would install all packages. But it would not create the folder structure, the readme, the git folders, etc.

Question: Is there a way to achieve what I want? Ideally, I could also have a dynamic project name, e.g. poetry from template pyproject.toml my_other_project. Is this possible with poetry? Or am I just using the wrong tool?

Thanks in advance!

Asked By: Thomas

||

Answers:

This kind of flexibility is out of scope for Poetry. Use cookiecutter instead.

Answered By: finswimmer

If you are on Windows, a simple cmd file can be used:

@echo off

SETLOCAL EnableExtensions EnableDelayedExpansion

REM if 0 args show usage
@IF {%1} == {} (
    ECHO Usage %0 args ... - creates projects with my custom settings
    SET ERRORLEVEL=99
    pause
    GOTO :EOF
)

poetry new %1
cd %1
poetry add numpy pandas matplotlib rich ipython black scikit-learn scipy mypy
rm README.rst
touch README.md
git init

Fixing up
python = "^3.10" to python = "~3.10" in the pyproject.toml.
is left as an exercise for the student. 🙂

Hint: sed or awk can be used for this sort of thing.

Note: Script ignores any arguments after the 1st one.

Answered By: Andrew Dennison