Repository: pandas-dev/pandas

Issue:
core\common.py:496: ModuleNotFoundError
In circumstances that I cannot reproduce in a small code example, I get the following error:

When looking at common.py, it seems that __main__ does indeed not exist. Does anybody know why and under what circumstances this is called?

```

_tcp\work\win-na-x64-release\py3\mre_venv3\lib\site-packages\pandas\core\base.py:61: in __str__

    return self.__unicode__()

_tcp\work\win-na-x64-release\py3\mre_venv3\lib\site-packages\pandas\core\frame.py:659: in __unicode__

    width, _ = console.get_console_size()

_tcp\work\win-na-x64-release\py3\mre_venv3\lib\site-packages\pandas\io\formats\console.py:68: in get_console_size

    if com.in_interactive_session():

_tcp\work\win-na-x64-release\py3\mre_venv3\lib\site-packages\pandas\core\common.py:503: in in_interactive_session

    return check_main()

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

 

    def check_main():

>       import __main__ as main

E       ModuleNotFoundError: No module named '__main__'

 

_tcp\work\win-na-x64-release\py3\mre_venv3\lib\site-packages\pandas\core\common.py:496: ModuleNotFoundError

```


Discussion:
I believe that's IPython-specific logic, but I'm 100% familiar with the rationale behind it.

We probably could `except` on `ModuleNotFoundError` in that check as well.

cc @jreback @TomAugspurger 
Can you try this on master? I don't even see the function from your traceback there so I assume you are working with an older copy of pandas (though `pd.show_versions()` would also be helpful)
I’m using pandas 0.23.4, which I think is the latest in anaconda. 

> On 10 Jan 2019, at 05:23, William Ayd <notifications@github.com> wrote:
> 
> Can you try this on master? I don't even see the function from your traceback there so I assume you are working with an older copy of pandas (though pd.show_versions() would also be helpful)
> 
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub, or mute the thread.

Under what circumstances is `__main__` not defined? If necessary, we can
but that import in a `try / except` block.

On Thu, Jan 10, 2019 at 2:18 AM Nicolas Dickreuter <notifications@github.com>
wrote:

> I’m using pandas 0.23.4, which I think is the latest in anaconda.
>
> > On 10 Jan 2019, at 05:23, William Ayd <notifications@github.com> wrote:
> >
> > Can you try this on master? I don't even see the function from your
> traceback there so I assume you are working with an older copy of pandas
> (though pd.show_versions() would also be helpful)
> >
> > —
> > You are receiving this because you authored the thread.
> > Reply to this email directly, view it on GitHub, or mute the thread.
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/pandas-dev/pandas/issues/24690#issuecomment-453008002>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/ABQHIslhsyZg4vc-U9Kb7z6Gq9Pq-FzQks5vBvdLgaJpZM4Z4hGD>
> .
>

In my case I’m running pytest. When I run it individually on the failing test it passes, but then I run it in conjunction with a pylint test, then I get this error. 

> On 10 Jan 2019, at 12:39, Tom Augspurger <notifications@github.com> wrote:
> 
> Under what circumstances is `__main__` not defined? If necessary, we can
> but that import in a `try / except` block.
> 
> On Thu, Jan 10, 2019 at 2:18 AM Nicolas Dickreuter <notifications@github.com>
> wrote:
> 
> > I’m using pandas 0.23.4, which I think is the latest in anaconda.
> >
> > > On 10 Jan 2019, at 05:23, William Ayd <notifications@github.com> wrote:
> > >
> > > Can you try this on master? I don't even see the function from your
> > traceback there so I assume you are working with an older copy of pandas
> > (though pd.show_versions() would also be helpful)
> > >
> > > —
> > > You are receiving this because you authored the thread.
> > > Reply to this email directly, view it on GitHub, or mute the thread.
> >
> > —
> > You are receiving this because you were mentioned.
> > Reply to this email directly, view it on GitHub
> > <https://github.com/pandas-dev/pandas/issues/24690#issuecomment-453008002>,
> > or mute the thread
> > <https://github.com/notifications/unsubscribe-auth/ABQHIslhsyZg4vc-U9Kb7z6Gq9Pq-FzQks5vBvdLgaJpZM4Z4hGD>
> > .
> >
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub, or mute the thread.

> but then I run it in conjunction with a pylint test, then I get this
error.

What do you mean by "pylint test"?

Does `__main__` not existing seem strange to anyone else? If it happen as
part of a normal workflow, then
it'd be fine to catch that exception. But it's abnormal, I'd like to not
catch it to avoid hiding a potentially real issue.

On Thu, Jan 10, 2019 at 7:37 AM Nicolas Dickreuter <notifications@github.com>
wrote:

> In my case I’m running pytest. When I run it individually on the failing
> test it passes, but then I run it in conjunction with a pylint test, then I
> get this error.
>
> > On 10 Jan 2019, at 12:39, Tom Augspurger <notifications@github.com>
> wrote:
> >
> > Under what circumstances is `__main__` not defined? If necessary, we can
> > but that import in a `try / except` block.
> >
> > On Thu, Jan 10, 2019 at 2:18 AM Nicolas Dickreuter <
> notifications@github.com>
> > wrote:
> >
> > > I’m using pandas 0.23.4, which I think is the latest in anaconda.
> > >
> > > > On 10 Jan 2019, at 05:23, William Ayd <notifications@github.com>
> wrote:
> > > >
> > > > Can you try this on master? I don't even see the function from your
> > > traceback there so I assume you are working with an older copy of
> pandas
> > > (though pd.show_versions() would also be helpful)
> > > >
> > > > —
> > > > You are receiving this because you authored the thread.
> > > > Reply to this email directly, view it on GitHub, or mute the thread.
> > >
> > > —
> > > You are receiving this because you were mentioned.
> > > Reply to this email directly, view it on GitHub
> > > <
> https://github.com/pandas-dev/pandas/issues/24690#issuecomment-453008002>,
> > > or mute the thread
> > > <
> https://github.com/notifications/unsubscribe-auth/ABQHIslhsyZg4vc-U9Kb7z6Gq9Pq-FzQks5vBvdLgaJpZM4Z4hGD
> >
> > > .
> > >
> > —
> > You are receiving this because you authored the thread.
> > Reply to this email directly, view it on GitHub, or mute the thread.
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/pandas-dev/pandas/issues/24690#issuecomment-453098232>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/ABQHIiyry9J8JBd3cKXSE38zj7QWNVXLks5vB0IogaJpZM4Z4hGD>
> .
>


Resolving patch:
diff --git a/pandas/io/formats/console.py b/pandas/io/formats/console.py
--- a/pandas/io/formats/console.py
+++ b/pandas/io/formats/console.py
@@ -95,7 +95,10 @@ def in_interactive_session():
     from pandas import get_option
 
     def check_main():
-        import __main__ as main
+        try:
+            import __main__ as main
+        except ModuleNotFoundError:
+            return get_option('mode.sim_interactive')
         return (not hasattr(main, '__file__') or
                 get_option('mode.sim_interactive'))
 


Test patch: