I have tried all the suggestions above and can not find a solution for a really simple case where I want to pytest a cli script that has no .py extension.
Why does spec.loader.exec_module() raise a IsADirectoryError on "."?
I have two files in a directory:
% ls
cli_with_no_dot_py test_cli_with_no_dot_py.py
% cat cli_with_no_dot_py
#!/usr/bin/env python3
cool_thing = True
print("cool_thing is", cool_thing)
% ./cli_with_no_dot_py
cool_thing is True
% cat test_cli_with_no_dot_py.py
#!/usr/bin/env python3
from importlib.util import spec_from_loader, module_from_spec
from importlib.machinery import SourceFileLoader
spec = spec_from_loader("cli_with_no_dot_py",
SourceFileLoader("cli_with_no_dot_py", "."))
print("spec:", spec)
cli_with_no_dot_py = module_from_spec(spec)
print("cli_with_no_dot_py):", cli_with_no_dot_py)
print("dir(cli_with_no_dot_py:", dir(cli_with_no_dot_py))
print("spec.loader:", spec.loader)
spec.loader.exec_module(cli_with_no_dot_py) # !!! IsADirectoryError !!!
def test_cool_thing():
print("cli_with_no_dot_py.cool_thing is", cli_with_no_dot_py.cool_thing)
assert cli_with_no_dot_py.cool_thing
% pytest
======================================================================== test session starts ========================================================================
platform darwin -- Python 3.8.2, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: /Users/cclauss/Python/pytest_a_cli_tool
collected 0 items / 1 error
============================================================================== ERRORS ===============================================================================
____________________________________________________________ ERROR collecting test_cli_with_no_dot_py.py ____________________________________________________________
test_cli_with_no_dot_py.py:11: in <module>
spec.loader.exec_module(cli_with_no_dot_py)
<frozen importlib._bootstrap_external>:779: in exec_module
???
<frozen importlib._bootstrap_external>:915: in get_code
???
<frozen importlib._bootstrap_external>:972: in get_data
???
E IsADirectoryError: [Errno 21] Is a directory: '.'
-------------------------------------------------------------------------- Captured stdout --------------------------------------------------------------------------
spec: ModuleSpec(name='cli_with_no_dot_py', loader=<_frozen_importlib_external.SourceFileLoader object at 0x10e106f70>, origin='.')
cli_with_no_dot_py: <module 'cli_with_no_dot_py' from '.'>
dir(cli_with_no_dot_py): ['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
spec.loader: <_frozen_importlib_external.SourceFileLoader object at 0x10e106f70>
====================================================================== short test summary info ======================================================================
ERROR test_cli_with_no_dot_py.py - IsADirectoryError: [Errno 21] Is a directory: '.'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
========================================================================= 1 error in 0.09s ==========================================================================
pyextension? – Esteban Küber Apr 8 '10 at 16:52pyshextension... – osa Jul 11 '14 at 21:43ConfigParser. wiki.python.org/moin/ConfigParserExamples – Chemical Programmer Nov 23 '15 at 7:53