[fluiddyn] Use of __file__

  • From: Julien Salort <listes@xxxxxxxxx>
  • To: fluiddyn@xxxxxxxxxxxxx
  • Date: Tue, 8 May 2018 11:03:11 +0200

Hello,


A recent thread on python-ideas, e.g.

https://mail.python.org/pipermail/python-ideas/2018-May/050428.html

has pointed out that it is bad practise to use __file__ to infer the path of resources within a package.

We should use importlib_resources (python < 3.7) or importlib.resources (upcoming python 3.7) instead:

http://importlib-resources.readthedocs.io/en/latest/index.html

I notice that there are a few occurences of __file__ in fluiddyn:

% find fluiddyn -name "*.py" -exec grep -H __file__ {} \;
fluiddyn/fluiddoc/fluiddocset.py: os.path.join(os.path.dirname(pkg.__file__), "../doc")
fluiddyn/setup.py:here = os.path.abspath(os.path.dirname(__file__))
fluiddyn/fluiddyn/util/test/test_util.py: util.modification_date(os.path.dirname(__file__))
fluiddyn/fluiddyn/util/test/test_paramcontainer.py:xml_file = os.path.abspath(os.path.join(os.path.dirname(__file__), "file.xml"))
fluiddyn/fluiddyn/util/matlab2py/test/test_matlab2py.py:here = os.path.abspath(os.path.dirname(__file__))
fluiddyn/fluiddyn/io/test/test_ns3d.py:input_dir = os.path.join(os.path.dirname(__file__), "ns3d_files")
fluiddyn/fluiddyn/io/test/test_rdvision.py: os.path.join(os.path.dirname(__file__), "rdvision_files")

% find fluidlab -name "*.py" -exec grep -H __file__ {} \;
fluidlab/setup.py:here = os.path.abspath(os.path.dirname(__file__))
fluidlab/fluidlab/objects/pumps.py:here = os.path.abspath(os.path.dirname(__file__))

% find fluidimage -name "*.py" -exec grep -H __file__ {} \;
fluidimage/fluidimage/calcul/correl_pycuda.py:    kernel_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),
fluidimage/fluidimage/calcul/correl_pycuda.py:    kernel_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),
fluidimage/fluidimage/calibration/test_tsai_calibration.py:here = os.path.abspath(os.path.dirname(__file__))
fluidimage/fluidimage/calibration/test_direct_stereo_reconstruction.py:here = os.path.abspath(os.path.dirname(__file__))
fluidimage/fluidimage/topologies/test_piv.py:here = os.path.abspath(os.path.dirname(__file__))
fluidimage/fluidimage/topologies/test_preproc.py:here = os.path.abspath(os.path.dirname(__file__))
fluidimage/fluidimage/topologies/test_im2im.py:here = os.path.abspath(os.path.dirname(__file__))
fluidimage/fluidimage/works/piv/test_piv.py:here = os.path.abspath(os.path.dirname(__file__))
fluidimage/setup.py:here = os.path.abspath(os.path.dirname(__file__))


I am not sure that this is wrong enough to actually go over all those files and remove the use of __file__ at the moment, but it is good to know for future code that this is not the recommanded method.

What do you think ?


Bests,


Julien


Other related posts:

  • » [fluiddyn] Use of __file__ - Julien Salort