1

I have the same situation like here, here, here, and here. I followed the advice given in those questions and I still get the FileNotFoundError.

--tree--

.
MANIFEST.in
setup.py
...
--src
   |--__init__.py
   |--mypkg
        |--__init__.py
        |--module_1.py  # dosth_1() in module_1  needs somedata.json
        ...
   |--data
        |--__init__.py
        |--somedata.json
        |--someotherdata.txt
        ...

--setup.py--

packages=["mypkg"],
package_dir={"": "src"},
package_data={"mypkg": ["data/*.txt", "data/*.json"]},

--after removing the old build--

(venv)$ python3 -m build
...
copying src/mypkg/somedata.json -> .../src/mypkg/data
...
adding 'mypkg/data/somedata.json'
...

--next--

  • upload the mypkg to PyPI
  • pip install mypkg (within a different vEnv)

--open python interpreter--

>>from mypkg import module_1
>>res = module_1.dosth_1()

--throws error--

... File "/usr/lib/python3.8/importlib/resources.py", line 82, in _check_location raise FileNotFoundError(f'Package has no location {package!r}') FileNotFoundError: Package has no location <module 'mypkg.data' (namespace)>

So I'm really confused why this happens. I took care of the init.py, I did the setup.py as advertised, and if I understand the build comments correctly the data file is added to the build. Still the installed package has no access to the data. What's wrong here?

Ollie
  • 189
  • 1
  • 13
  • Did the data files actually get installed with `pip`? I.e., are they in `site-packages/mypkg/data`? – MattDMo Jul 18 '21 at 18:21
  • @MattDMo Yes, I can see all of them. – Ollie Jul 18 '21 at 18:32
  • `pip build` makes a .tar.gz file, you can open it in standard archive tools to see what's inside. `pip wheel` makes a wheel file which is a zip file, again, archive tools can inspect it, but you may need to change the extension. – David Jones Jul 14 '22 at 19:42

1 Answers1

2

Solved it by changing the setup.py from:

packages=["mypkg"],
package_dir={"": "src"},
package_data={"mypkg": ["data/*.txt", "data/*.json"]},

to:

packages=["mypkg"],
include_package_data=True,
package_dir={"": "src"},

To me this seems obscure. It actually makes me wonder if the package_data={...} is maybe broken.

Ollie
  • 189
  • 1
  • 13