In my setup.py build command, I create a pickle file that I'd like to be available at runtime.
I do this by subclassing from the setuptools.command.build_py.build_py and overriding the run
command:
class my_command(build_py):
def run(self):
target_dir = os.path.join(self.build_lib, 'my_package/data')
self.mkpath(target_dir)
with open(os.path.join(target_dir, 'my_file.pkl'), 'wb') as f:
pickle.dump({'foo': 'bar'}, f)
and then including it as a custom class:
setup(
...,
cmdclass={
'build_py': my_command,
},
test_suite='my_package.tests',
)
This works fine, it's available and I'm able to retrieve it at runtime in my package by using:
my_pickle = pkg_resources.resource_filename(
'my_package',
'data/my_file.pkl'
)
But when I run tests python setup.py test
, they are not executed in the build directory but in the source directory and pkg_resources.resource_filename
points to a file that does not exist.
How can I get my tests to run in the build directory, not in the package source directory?
Solution is to use this subclass of the test command, which ensures that the tests are run in a build directory. This is already the default for Python 3 when
use_2to3
is enabled, I just had to take out theif
statement in the subclass.