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.

[Error Handling]: ImportError while importing test module (Error-Handling Messaging & Exercises)

See original GitHub issue

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.

Any assistance greatly appreciated.

Thank you so much. Dan.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
cmccandlesscommented, Nov 30, 2020

@BethanyG I would like to leave this open as a reference issue until we’ve made the changes we talked about.

1reaction
BethanyGcommented, 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 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:

from meetup import meetup

try:  
 from meetup import MeetupDayException  
except ImportError:  
 MeetupDayException = Exception

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:

class MeetupDayException(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 = day
        self.message = message
        super().__init__(self.message)



def meetup(year, month, week, day_of_week):
    try:
        meetup = meetup_check(year, month, week, day_of_week)
    except Exception as err:
        raise MeetupDayException(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.

Best, Bethany

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error handling when importing modules - python
Importing a module that does not exist will raise an ImportError exception. You can use this to define multiple levels of functionality based...
Read more >
[Ellen's Alien Game] Tests are Blocked from Running in Editor ...
We received the following error when we ran your code: ImportError while importing test module '.mnt.exercism-iteration.classes_test.py'.
Read more >
Exception and Error Handling in Python - DataCamp
Raise exceptions in Python and catch your errors today! ... ImportError is raised when you try to import a module that does not...
Read more >
Python Exceptions and Errors - PYnative
Learn Python Exceptions and Errors in depth. handling exception with ... ImportError, Raised when the imported module is not found.
Read more >
Lesson 13: Errors and Exception handling — Programming ...
A syntax error means you wrote something nonsensical, something the Python interpreter cannot understand. An example of a syntax error in English would...
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