[Tutor] import issues
dn
PythonList at DancesWithMice.info
Tue Jan 9 19:01:33 EST 2024
On 10/01/24 12:01, Mats Wichmann wrote:
> On 1/9/24 15:29, dn via Tutor wrote:
>> On 10/01/24 07:06, James Hartley wrote:
>>> I admit that I have not internalized how Python's import system
>>> works yet.
>>> Given the following directory structure:
>>>
>>> +-project
>>> |
>>> +-src
>>> | |
>>> | +-__init__.py
>>> | +-foo.py
>>> |
>>> +-tests
>>> |
>>> +-test_foo.py
>>>
>>> Currently, __init__.py is empty. What needs to be placed in __init__.py
>>
>> Nothing - it's not relevant to this discussion.
>>
>>
>>> and test_foo.py in order for pytest to execute all tests in test_foo.py?
>>> pytest is being executed while in the project directory. With:
>>>
>>> from foo import Foo
>>>
>>> --or--
>>>
>>> from src.foo import Foo
>>>
>>> defined in test_foo.py, pytest reports "ModuleNotFoundError: No module
>>> named 'src'".
>>>
>>> Any and all comments would be appreciated. Any links to online sources
>>> would also be appreciated. Thanks!
>>
>> I use the latter, except:
>>
>> - using PyCharm's auto-testing feature. However, 'translating' those
>> commands back to manual-typing, would be something like:
>>
>> python -m pytest /path/project/tests/test_test_foo.py --no-header
>> --no-summary -q
>>
>> - prepending "python -m"
>> which configures the sys.path to include current/project directory
>> (and hence the "src.foo" relative-addressing) which likely accounts
>> for the issues outlined (above)
>>
>>
>> Web.Refs:
>> Specifics:
>> https://docs.pytest.org/en/7.4.x/explanation/pythonpath.html#pytest-vs-python-m-pytest
>> and the rest of the online-manual (although I have to say, even after
>> years of use, I find it somewhat overwhelming)
>>
>> Somewhat easier is the 'book of words', Brian Okken's:
>> https://www.amazon.com.au/Python-Testing-pytest-Effective-Scalable/dp/1680508601/ref=sr_1_1?keywords=brian+okken&qid=1704838720&sr=8-1
>> (disclaimer: there are other book-sellers. No commission claimed)
>>
>> Coursera on-line training: (amongst many 'Python' courses)
>> Testing and Debugging Python
>> https://www.coursera.org/projects/testing-and-debugging-python
>> - is a $paid project approach
>> Possibly slightly off (your) topic is LearnQuest's 'TDD
>> Specialisation':
>> https://www.coursera.org/learn/test-driven-development-overview
>> (which I think can be "audited" for $free)
>> NB have not completed either, and thus can't comment on quality, nor
>> degree of pytest-content.
>
> Lotsa good stuff here, too:
>
> https://www.obeythetestinggoat.com/
OK, now you've made me feel bad!
Having broadened the OP's scope, this book/web-site should indeed have
been mentioned!
I met Harry at a London-PUG meeting. Wow, must be a solid 15 years back
- started playing with pythonanywhere.com when they were still setting
it up/converting from the idea of a Python-based 'better Excel'...
(Harry has since moved-on to work on other things)
The testing-goat has a lot of grey in his beard these days. However,
what other tome covers Python+testing+pytest?
[have a 'gathering' on Friday*, to talk about 'testing'. Thus, will be
interested in any and all comments, advice, sources...]
IIRC the main project-example was web/Django, which some Python-folk may
find unnecessarily complicated/unfamiliar.
* 1500 NZDT so not a particularly-friendly hour for folk in EUR/UK
https://www.meetup.com/nzpug-auckland/events/298338908/
part of our Vacation Exception Handlers gatherings (cf formal PUG-meetings)
NB it is summer, as well as the festive/holiday-period down-under.
https://danceswithmice.info/Python/2024/VacExcHndlrs.html.
--
Regards,
=dn
More information about the Tutor
mailing list