Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embed resource files in final package at a given location #2267

Closed
kasteph opened this issue Apr 5, 2020 · 11 comments
Closed

Embed resource files in final package at a given location #2267

kasteph opened this issue Apr 5, 2020 · 11 comments
Labels
area/build-system Related to PEP 517 packaging (see poetry-core) area/core Related to the poetry-core library kind/feature Feature requests/implementations status/triage This issue needs to be triaged

Comments

@kasteph
Copy link
Member

kasteph commented Apr 5, 2020

From @theunkn0wn1

The feature I would like to see out of this is to be able to embed resource files into the wheel (and subsequently the installed package) at an arbitrary location.

For instance, in my project i would like to embed the pyproject.toml into the installed sources so the library may access this resource at runtime. I wish to do this to access the version string contained in the pyproject.toml.
Presently, even if the tool.poetry.include directive is used the file does not end up in the installation directory and is consequently not usable to the installed application.

My application has the structure

/
/pyproject.toml
/src/...
/test/...

What I would like to do is copy, at build time, /pyproject.toml to /src/pyproject.toml so it may be included in the wheel and usable a resource via importlib.resources.

import importlib.resources as resources
import toml
import src  # root application package (renames are planned)
package_meta = toml.loads(resources.read_text(src, "pyproject.toml"))
src.__version__ = package_meta['tool']['poetry']['version']

_Originally posted by @theunkn0wn1 in https://github.com/python-poetry/core/pull/6#issuecomment-608017525_
@kasteph kasteph added area/build-system Related to PEP 517 packaging (see poetry-core) kind/feature Feature requests/implementations area/core Related to the poetry-core library labels Apr 5, 2020
@finswimmer
Copy link
Member

Hello @theunkn0wn1,

maybe there is a use case for your feature request. But the one you are showing isn't.

The correct way to get access to the metadata of a package during runtime is to use importlib.metadata (or importlib_metadata for python <3.8)

import importlib.metadata

__version__ = importlib.metadata.version("mypackage")

So could you imagine other use cases?

fin swimmer

@kasteph kasteph added the status/triage This issue needs to be triaged label Apr 13, 2020
@dmyersturnbull
Copy link

@finswimmer @stephsamson Is there no current way to include resource files that are not in a package?

I'd like to include a changelog.md so it can be summarized when the package runs.

This is a more obscure use case, but I also have a self-hosting code that needs to store its own config setup, including its own pyproject.toml.

@dclong
Copy link

dclong commented Aug 18, 2020

What about including a JAR file?

@ToddG
Copy link

ToddG commented Jan 8, 2021

What about including a schema.json to be used by jsonschema? Sure, as a work-around, I can embed the schema into a string... But what if I want to include image resources in my package? etc..

@sinoroc
Copy link

sinoroc commented Jan 8, 2021

Resource files (in short: file that is not a Python file) that you wish to install and access at run time have to be part of an importable package. They can then be accessed with importlib.resources from Python's standard library. There are many examples around...

@gitonthescene
Copy link

Just to be explicit about this. When I changed my access to a file from using open().read() to using importlib.resources.read_text() running poetry build did package the file into the built wheel.

@sinoroc
Copy link

sinoroc commented Feb 14, 2021

I think this thread sums up the full situation about including and/or excluding things of the sdist and/or wheel: #3380

@zoj613
Copy link

zoj613 commented Feb 28, 2021

I would love this feature. I have a package with a C extension where I would like to the copy the folder of header files inside the wheel and sdist during build time. Currently I have no way of doing this. I don't want to keep the header files in the python package folder since they don't belong there.

@theunkn0wn1
Copy link
Contributor

I would love this feature. I have a package with a C extension where I would like to the copy the folder of header files inside the wheel and sdist during build time. Currently I have no way of doing this. I don't want to keep the header files in the python package folder since they don't belong there.

Funnily enough that was my initial inpetus to request this feature: compiled modules.

I have two projects which have generated files, which I do not wish to place in-line with the non-generated sources as they don't belong there.
importlib.resources helps in places where we have static files embedded into the wheel which are not generated at build time, or manually generated before building the wheel. They do not resolve the problem of embedding resources into the wheel that are not present within the package's filesystem to begin with.

@finswimmer
Copy link
Member

Closing this as a duplicate of #4583

@finswimmer finswimmer closed this as not planned Won't fix, can't repro, duplicate, stale Nov 27, 2022
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/build-system Related to PEP 517 packaging (see poetry-core) area/core Related to the poetry-core library kind/feature Feature requests/implementations status/triage This issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

9 participants