I'm making a python package that uses data files. I add data files in my setup.py like that:
from setuptools import setup, find_packages
setup(
name='foo',
version='1.0',
packages=find_packages(),
package_data={
'foo': ['test.txt'],
},
)
And the files are arranged like this:
/
foo/
__init__.py
foo_module.py
test.txt
setup.py
But the code like this
open('test.txt')
in foo_module.py stops working when I call it from outside the package. I believe that happens because I change my current working directory, and there is no file test.txt in my cwd. I suppose I can solve this problem by changing my current directory in the package code like this:
curdir = os.getcwd()
os.chdir(os.path.dirname(os.path.abspath(__file__)))
open('test.txt')
os.chdir(curdir)
But I'm not sure if this code is safe to use in multiprocessing processes beacuse it modifies cwd
. I could use locks, but I don't want the processes to wait for each other only because they change cwd
.
Is there a conventional, thread-safe way to access files added to the package in setup.py?