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.
Yes, I’m using hatchling for the build backend but I haven’t had to explicitly declare anything in the pyproject file to include data files in the package. So I guess hatchling automatically includes the files as long as they are in the src/package directory.
Probably. I do not know what the exact logic is. I also seem to recall that hatch tries to follow what’s in the .gitignore file if there is one (if you tell git to ignore something, then hatchling will also ignore it and this file will not be in the distribution files). But I am not sure, you really should check the documentation I linked.