1

I tried to add a TXT file into my python package, load it by pkg_resources, and do some other stuff with data after that. The structure of my python package is following:

package_name
  |-- requirements.txt
  |-- setup.py
  |-- MANIFEST.in
  |-- package
    |-- __init__.py
    |-- name
      |-- __init__.py
      |-- my_file.py
      |-- test
        |-- __init__.py
        |-- test_1.py
      |-- data
        |-- MY_DATA.txt

The content of ~/package_name/package/name/my_file.py:

import pkg_resources

DATA_PATH = 'package.name.data'
MY_DATA_PATH = pkg_resources.resource_filename(DATA_PATH, 'MY_DATA.txt')


def do_some_stuff_with_data(data_path=MY_DATA_PATH):
    ...

When I run tests on my local machine, the code runs without any problem. However, when I try to push it into git repository via CI/CD, tests fail on the error below:

/tmp/testenv/lib/python3.7/site-packages/package/name/test/test_name.py:13: in <module>
    from package.name.my_file import (
/tmp/testenv/lib/python3.7/site-packages/package/name/my_file.py:28: in <module>
    MY_DATA_PATH = pkg_resources.resource_filename(DATA_PATH, 'MY_DATA.txt')
/tmp/testenv/lib/python3.7/site-packages/pkg_resources/__init__.py:1206: in resource_filename
    return get_provider(package_or_requirement).get_resource_filename(
/tmp/testenv/lib/python3.7/site-packages/pkg_resources/__init__.py:437: in get_provider
    return _find_adapter(_provider_factories, loader)(module)
/tmp/testenv/lib/python3.7/site-packages/pkg_resources/__init__.py:1452: in __init__
    self.module_path = os.path.dirname(getattr(module, '__file__', ''))
/tmp/testenv/lib/python3.7/posixpath.py:156: in dirname
    p = os.fspath(p)
E   TypeError: expected str, bytes or os.PathLike object, not NoneType

I know, that data path is written correctly. I checked the typos for several times. I also thought that the error could have been caused by not adding data path to MANIFEST.in file. However, the addition did not solve the problem.

Do you have any idea what I am missing?

Jaroslav Bezděk
  • 6,967
  • 6
  • 29
  • 46
  • Namespace packages are permitted to have their `__file__` set to `None`: see https://www.python.org/dev/peps/pep-0420/ and https://stackoverflow.com/q/1675734/1256452. (I'm not sure how useful this is here since in Python 3.7 everything is supposed to "just work", but that's why this is a comment, not an answer.) – torek Aug 23 '19 at 07:24

1 Answers1

4

If there is anyone with the same problem, I have solved the issue by adding an __init__.py file into ~/package_name/package/name/data/ folder.

Jaroslav Bezděk
  • 6,967
  • 6
  • 29
  • 46
  • 1
    Thank you from the bottom of my heart. I have no idea how you worked that one out, but great job. For anyone who comes after me, I needed only an empty `__init__.py` file in my `package` directory. – Matthew Walker Jul 28 '20 at 05:18