You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Error Handling]: ImportError while importing test module (Error-Handling Messaging & Exercises) #2275
Good day. I first offer the obligatory "I'm not GitHub-savvy" apology. I did look for an answer as prescribed here, following steps in subheading Opening an Issue. But I have not seen anything. I've seen some fixes on Stack Overflow. But they make no sense to me. Please don't crush me if I'm doing this wrong.
I'm working on a new linux install. On my previous installation, I ran pytest successfully many times (28 to be precise). With this new installation, I've run it once successfully on exercise Twelve Days of Christmas. It's giving me this error now:
$ /home/dan/.local/bin/pytest meetup_test.py
============================= test session starts ==============================
platform linux -- Python 3.8.5, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: /home/dan/exercism/python/meetup
collected 0 items / 1 error
==================================== ERRORS ====================================
_______________________ ERROR collecting meetup_test.py ________________________
ImportError while importing test module '/home/dan/exercism/python/meetup/meetup_test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.8/importlib/__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
meetup_test.py:4: in <module>
from meetup import meetup, MeetupDayException
E ImportError: cannot import name 'MeetupDayException' from 'meetup' (/home/dan/exercism/python/meetup/meetup.py)
=========================== short test summary info ============================
ERROR meetup_test.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.09s ===============================
$
I tried removing the meetup directory and re-downloading from the CLI. I get this error no matter what the contents of meetup.py are. So, I'm guessing that I have some issue either with they way pytest is configured or with my meetup_pytest file.
Hi, Norbert. Thank you for the reply. Here is my code.
from calendar import day_name, Calendar
from datetime import date
def meetup(year, month, week, day_of_week):
desired_day = [day for day in day_name].index(day_of_week)
desired_week = [which for which in ['last', '1st', '2nd', '3rd', '4th', '5th', 'teenth']].index(week)
cal = Calendar()
cal = list(cal.monthdays2calendar(year, month))
if desired_week == 6:
for week in cal:
for day in week:
if day[0] in range(13, 20) and day[1] == desired_day:
return date(year, month, week[desired_day][0])
if desired_week == 0:
cal = cal[::-1] # Reverse the calendar weeks if last __day desired
i = 1
for week in cal:
if week[desired_day][0] != 0 and i == desired_week or desired_week == 0:
return date(year, month, week[desired_day][0])
elif week[desired_day][0] != 0:
i = i + 1
I see the line you're referring to for the import error. Normally, I fill in the function definitions as provided in the .py file for the exercise. Am I supposed to create a MeetupDayException?
Since @NobbZ is in CET, I'm picking this up. (I'm in PST). Apologies - the test file for this exercise appears to have been updated since you last worked on/ran it, and it is indeed expecting you to define a MeetupDayException class.
To get around this (temporarily) you can add the following to the top of your meetup_test.py file, directly below the line that says from meetup import meetup, MeetupDayException, and remove MeetupDayException from the import line, so that the two look like this:
That way, you can run and work on any tests that might be failing right now for you. When I ran your code, it looks as though 7 of the 90 tests have failed -- mostly for ValueErrors where the day is out of range for the month.
To fully pass all the tests without the test file modification, you will need to make a custom MeetupDayException Class by subclassing Exception. Something along the lines of this:
classMeetupDayException(Exception):
"""Exception raised when the meetup day is not valid or in range.. Attributes: day -- the day that is causing the error message -- explanation of the error """def__init__(self, day, message="Isn't valid as a proposed meetup date. Please try another."):
self.day=dayself.message=messagesuper().__init__(self.message)
defmeetup(year, month, week, day_of_week):
try:
meetup=meetup_check(year, month, week, day_of_week)
exceptExceptionaserr:
raiseMeetupDayException(f'{day_of_week}, week {week}, in {month}, of {year}')
As you can see, I've made a function that is named meetup() that calls a function called meetup_check() (which is what I renamed your meetup() function to). You probably want to be more targeted in the errors you catch/raise -- so you can raise the exception specifically/directly as ValueErrors or IndexErrors get caught within your code -- but this was quicker for me to demonstrate.
For more detail on how to create/customize and handle errors, take a look at the docs here.
Please let us know if this works for you, and if you have any additional questions or issues.
OK... I think I get it. I was able to mend my code to get pytest working with both the modifications to the pytest file you suggested and also after adding the MeetupDayException class. And I was able to weed out those ValueErrors too. =)
This issue has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
changed the title [-][V2] ImportError while importing test module[/-][+][Error Handling]: ImportError while importing test module (Error-Handling Messaging & Exercises)[/+]on May 21, 2022
Activity
NobbZ commentedon Nov 27, 2020
Please also show us the content of your
meetup.py. But currently I'd say it is missing a definition for theMeetupDayException.\cc @exercism/python
dan9731 commentedon Nov 28, 2020
Hi, Norbert. Thank you for the reply. Here is my code.
I see the line you're referring to for the import error. Normally, I fill in the function definitions as provided in the .py file for the exercise. Am I supposed to create a MeetupDayException?
Thank you,
Dan.
BethanyG commentedon Nov 28, 2020
Hi @dan9731 👋
Since @NobbZ is in CET, I'm picking this up. (I'm in PST). Apologies - the test file for this exercise appears to have been updated since you last worked on/ran it, and it is indeed expecting you to define a
MeetupDayExceptionclass.To get around this (temporarily) you can add the following to the top of your
meetup_test.pyfile, directly below the line that saysfrom meetup import meetup, MeetupDayException, and removeMeetupDayExceptionfrom the import line, so that the two look like this:That way, you can run and work on any tests that might be failing right now for you. When I ran your code, it looks as though 7 of the 90 tests have failed -- mostly for
ValueErrorswhere the day is out of range for the month.To fully pass all the tests without the test file modification, you will need to make a custom
MeetupDayExceptionClass by subclassingException. Something along the lines of this:As you can see, I've made a function that is named
meetup()that calls a function calledmeetup_check()(which is what I renamed yourmeetup()function to). You probably want to be more targeted in the errors you catch/raise -- so you can raise the exception specifically/directly asValueErrorsorIndexErrorsget caught within your code -- but this was quicker for me to demonstrate.For more detail on how to create/customize and handle errors, take a look at the docs here.
Please let us know if this works for you, and if you have any additional questions or issues.
Best,
Bethany
dan9731 commentedon Nov 28, 2020
OK... I think I get it. I was able to mend my code to get pytest working with both the modifications to the pytest file you suggested and also after adding the MeetupDayException class. And I was able to weed out those ValueErrors too. =)
This was very helpful.
Thank you so much, @BethanyG !
cmccandless commentedon Nov 28, 2020
@iHiD this issue may be moved to exercism/python
cmccandless commentedon Nov 30, 2020
@BethanyG I would like to leave this open as a reference issue until we've made the changes we talked about.
github-actions commentedon Dec 22, 2020
This issue has been automatically marked as
abandonedbecause it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.BethanyG commentedon Dec 22, 2020
Going to comment here since we still have changes that require this to be open. Removing the [abandoned] tag.
[-]ImportError while importing test module[/-][+][V2] ImportError while importing test module[/+][-][V2] ImportError while importing test module[/-][+][Error Handling]: ImportError while importing test module (Error-Handling Messaging & Exercises)[/+]