python debugger does not work with files containing relative import


I have a file that contains some relative import as below:

"""Generate batches from pandas DataFrame."""
from typing import Union

from .base import ChunkedBatchGenerator
from ... import ini
from ..single import row as single_row
from ..single import sequence as single_sequence

<source code>

if __name__ == '__main__':
<some code to debug>
# debugger consol
/Users/floriandejax/.local/share/virtualenvs/timeserio-J6m5QSav/bin/python /Users/floriandejax/PycharmProjects/pythonProject/timeserio/timeserio/batches/chunked/
Traceback (most recent call last):
File "/Users/floriandejax/PycharmProjects/pythonProject/timeserio/timeserio/batches/chunked/", line 4, in <module>
from .base import ChunkedBatchGenerator
ModuleNotFoundError: No module named '__main__.base'; '__main__' is not a package
Process finished with exit code 1

Below is my project structure:

I set my chunked folder as Sources Root but it did not solve the issue. 

Comment actions Permalink

My debug config:


Comment actions Permalink

Hi, it seems to be a config for a different file. Would you mind sending the simplified project to PyCharm support at How do you execute outside of PyCharm? I mean running a file with a relative import is doomed to fail as

from .base import ChunkedBatchGenerator

... is basically

from __main__.base import ChunkedBatchGenerator

... and __main__ here is As the docs say

Note that relative imports are based on the name of the current module. Since the name of the main module is always "__main__", modules intended for use as the main module of a Python application must always use absolute imports.


Please sign in to leave a comment.