I made a Python package that is a static site generator with a command line interface to build the site and perform other operations. I would like to add a CLI command that generates a new project that is used to create a site. For example, running the command mypkg new would create a directory with the following content:
I made an example package to test copying the files from within the package. The structure of the package is shown below. The static directory contains the files that I want to copy to the user’s working directory.
This seems to be doing what I need. But I have a few questions:
The importlib.resources.files creates a Traversable so what is the proper way to handle that as a path? In my example, casting it as a string seems to work but is there a better approach?
Instead of creating a directory then copying a file into the directory, is there a way to copy the entire directory including its contents?
Will the static directory be included with the package when I publish it to PyPI or do I need to explicitly include it using the pyproject.toml file?
Building a wheel, and installing that wheel in a fresh new virtual environment is probably your best way to verify that.
I recommend to play around with the importlib.resources API until you find what fits best your use case. I do not recall that it allows copy whole directory but maybe it does.
Verified that the static directory is included with the package after installing it with pip install . in a virtual environment. So it looks like I don’t need to explicitly include it in the pyproject file. I guess it works because it’s already in the src directory as a subpackage.
What files get automatically included in the wheel (or the sdist) is entirely dependent on the build backend. I see that your build backend is hatch/hatchling, and for this build backend the relevant part of its documentation seems to be “file selection”.
Just to back up a second, have you heard of cookiecutter? It seems like it does everything you want with this tool so you don’t have to reinvent the wheel.
If you have heard of it, can you explain what you want your tool to do that cookiecutter can’t do?
Yes, I am aware of cookiecutter. But I don’t want to rely on an external package just to copy files. I can use standard Python features to do this which I demonstrated in the example above.