brave-policeman-49804
04/17/2019, 5:19 PMx
in project root directory and I want to access this file from sub directories like with open('file')
and as such.hundreds-father-404
04/17/2019, 5:35 PM./pants
script?brave-policeman-49804
04/17/2019, 5:36 PMbrave-policeman-49804
04/17/2019, 5:38 PMlib/x
and I use .load('lib/x')
when running the goals from root dir, it works.hundreds-father-404
04/17/2019, 5:38 PMbrave-policeman-49804
04/17/2019, 5:41 PMgentle-wolf-58752
04/17/2019, 5:47 PMgentle-wolf-58752
04/17/2019, 5:50 PMbrave-policeman-49804
04/17/2019, 5:53 PMgentle-wolf-58752
04/17/2019, 6:23 PMhappy-kitchen-89482
04/17/2019, 8:39 PMresources()
target, and then load them using pkg_resources
(or pkgutil
on python3) instead of via direct filesystem operations. Then they will load no matter how they end up on the PYTHONPATH (loose files, or wrapped in an archive).happy-kitchen-89482
04/17/2019, 8:40 PMopen(...)
statements with pkgtutil
calls.happy-kitchen-89482
04/17/2019, 8:42 PM__path__
to find where your source root got expanded to, and then use the full path to that.brave-policeman-49804
04/18/2019, 2:33 AMenough-analyst-54434
04/19/2019, 3:20 PM__file__
attribute of the module calling load(...)
to calculate a relative path from. IE: if that module is at x/y/loader.py and the file to load is x/data/model
you could say something like:
$ tree /tmp/example/
/tmp/example/
βββ x
βββ data
β βββ model
βββ y
βββ loader.py
3 directories, 2 files
$ cat /tmp/example/x/data/model
toy
$ cat /tmp/example/x/y/loader.py
import os
_DATA_DIR = os.path.join(os.path.dirname(__file__), '..', 'data')
def load():
with open(os.path.join(_DATA_DIR, 'model')) as fp:
return fp.read()
if __name__ == '__main__':
print('Loaded model: {}'.format(load()))
$ PYTHONPATH=/tmp/example python3.7 -m x.y.loader
Loaded model: toy
brave-policeman-49804
04/21/2019, 7:32 AM.pex
, during the loading of the files, it searches for the file outside the .pex
file. One obvious workaround is keeping the executable files separate from the data files and refer to non-module
files with abs data file paths. Have I got this right?enough-analyst-54434
04/21/2019, 3:48 PMzip_safe=False
to the associated python_binary
target. That will cause the pex to unzip itself and run from the unzipped pex ensuring filesystem based access works.