python debugger does not work with files containing relative import

Answered

I have a pandas.py 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/pandas.py
Traceback (most recent call last):
File "/Users/floriandejax/PycharmProjects/pythonProject/timeserio/timeserio/batches/chunked/pandas.py", 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. 

2 comments
Comment actions Permalink

My debug config:

 

0
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 pycharm-support@jetbrains.com? How do you execute pandas.py 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 pandas.py. As the docs say https://docs.python.org/3.6/tutorial/modules.html#intra-package-references

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.

0

Please sign in to leave a comment.