0

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?

Ilya Peterov
  • 1,975
  • 1
  • 16
  • 33
  • why don't you just open the file with the absolute path instead? – eugecm Nov 29 '15 at 03:18
  • @eugecm I'll need to know the absolute path, which may be different because of os, python version, whether I'm using virtualenv and maybe something else. – Ilya Peterov Nov 29 '15 at 03:20
  • see http://stackoverflow.com/questions/247770/retrieving-python-module-path on how to get the absolute path of the module, doesn't matter where it is, you can always use `__file__` to find out where it's installed – eugecm Nov 29 '15 at 03:21
  • @eugecm I see now. Not very bright of me, I already used `os.path.dirname(os.path.abspath(__file__))` to get abs path to the module, could have figured it out, sorry. – Ilya Peterov Nov 29 '15 at 03:26

0 Answers0