question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

__file__ should be avoided in library code

See original GitHub issue

We should be using pkg_resources (or importlib.resources if our min Python version is 3.7) instead of uses of __file__.

$ get grep '__file__' sklearn/
sklearn/__check_build/__init__.py:    local_dir = os.path.split(__file__)[0]
sklearn/datasets/_base.py:    module_path = dirname(__file__)
sklearn/datasets/_base.py:    module_path = dirname(__file__)
sklearn/datasets/_base.py:    module_path = dirname(__file__)
sklearn/datasets/_base.py:    module_path = dirname(__file__)
sklearn/datasets/_base.py:    module_path = dirname(__file__)
sklearn/datasets/_base.py:    base_dir = join(dirname(__file__), 'data/')
sklearn/datasets/_base.py:    with open(dirname(__file__) + '/descr/linnerud.rst') as f:
sklearn/datasets/_base.py:    module_path = dirname(__file__)
sklearn/datasets/_base.py:    module_path = join(dirname(__file__), "images")
sklearn/datasets/_california_housing.py:    module_path = dirname(__file__)
sklearn/datasets/_covtype.py:    module_path = dirname(__file__)
sklearn/datasets/_kddcup99.py:    module_path = dirname(__file__)
sklearn/datasets/_lfw.py:    module_path = dirname(__file__)
sklearn/datasets/_lfw.py:    module_path = dirname(__file__)
sklearn/datasets/_olivetti_faces.py:    module_path = dirname(__file__)
sklearn/datasets/_rcv1.py:    module_path = dirname(__file__)
sklearn/datasets/_twenty_newsgroups.py:    module_path = dirname(__file__)
sklearn/datasets/_twenty_newsgroups.py:    module_path = dirname(__file__)
sklearn/datasets/tests/test_openml.py:currdir = os.path.dirname(os.path.abspath(__file__))
sklearn/datasets/tests/test_svmlight_format.py:currdir = os.path.dirname(os.path.abspath(__file__))
sklearn/utils/__init__.py:    root = str(Path(__file__).parent.parent)  # sklearn package
sklearn/utils/_testing.py:        cwd = op.normpath(op.join(op.dirname(sklearn.__file__), '..'))

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
cmarmocommented, Jul 9, 2021

Hi @devpramod, I believe @jackzyliu is waiting for comments and reviews in #20297 . It would be nice if you comment there rather than starting a new pull request. Thanks!

1reaction
rthcommented, Jun 17, 2021

For future reference: see https://github.com/indygreg/PyOxidizer/issues/69 for more context why using __file__ is problematic.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Characters to Avoid in Filenames and Directories
Keep your filenames to a reasonable length and be sure they are under 31 characters. Most operating systems are case sensitive; always use...
Read more >
How to avoid including the same code when using C++ ...
I'm worried that when the library is compiled, and then linked to another project that also uses string and cout, the code for...
Read more >
File naming and structure - Research Data Management at ...
Avoid special characters or spaces in a file name; Use capitals and underscores instead of periods or spaces or slashes; Use date format...
Read more >
Code style and coding guidelines — C++ library for Named ...
File names should be all lower case. If the class name is a composite of several words, each word in a file name...
Read more >
Difference between Header file and Library - GeeksforGeeks
Header Files: The files that tell the compiler how to call some ... user can compile his/her code without using these libraries.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found