Skip to content

[BUG] dash doesn't get imported when a file named "org.py", "dash.py", or "test.py" with specific content is present in the current directory // "AttributeError: module 'dash' has no attribute 'Dash'" #1143

Closed
mstoelzle/solving-occlusion
#28

Description

@cannatag

Describe your context

dash (1.9.1)
dash-core-components (1.8.1)
dash-html-components (1.0.2)
dash-renderer (1.2.4)
dash-table (4.6.1)

Describe the bug

If a file named org.py is present in the current directory with the following content:

import dash_core_components as dcc

then dash doesn't import and I get the following message:

>>> import dash
Dash was not successfully imported. Make sure you don't have a file named
'dash.py' in your current directory.

Expected behavior
dash should import without any error.

Additional info

  • The org.py is never imported
  • If I rename the file to a different name dash get imported without any problem.
  • The problem is shown also with ``import dash_html_components as html```
  • The problem is shown either on Windows and in Linux
  • Tested with python3.4, python3.6, python3.8

Steps to replicate the problem on Linux

$ mkdir mytest
$ cd mytest
$ echo "import dash_core_components as dcc" > org.py
$ python3 -m venv venv
$ . venv/bin/activate
(venv) $ pip install dash
(venv) $ python
Python 3.4.6 (default, Mar 01 2017, 16:52:22) [GCC] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dash
Dash was not successfully imported. Make sure you don't have a file named
'dash.py' in your current directory.
(venv) $

if I rename the file the import works:

(venv) $ mv org.py othername.py
(venv) $ python
Python 3.4.6 (default, Mar 01 2017, 16:52:22) [GCC] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dash
>>>

Activity

  1. Charlenator commented on Jun 19, 2020

    @Charlenator

    I had the exact same issue with a file named "test.py"

  2. alexcjohnson commented on Jun 19, 2020

    @alexcjohnson
    Collaborator

    OK this one is very weird... it appears to come from the pickle module:

    # Jython has PyStringMap; it's a dict subclass with string keys
    try:
        from org.python.core import PyStringMap
    except ImportError:
        PyStringMap = None

    Which you can see if you raise an error inside org.py - it is trying to import it, to find org.python.core

    That seems like a terribly hacky bit of code to have living in the Python core, but we'll have to live with that part. This becomes a problem when org.py tries to import something that itself needs dash (in this case dash_core_components) and finds a partially-initialized module. I suppose we could put something unique very early in the dash __init__.py (something like __plotly_dash__ = True) and short-circuit the "Make sure you don't have a file named 'dash.py' in your current directory." errors if we find it.

    I don't see a problem with test.py but if we make a generic fix like ^^ it should cover any related oddities.

  3. karolprzyb commented on Jun 23, 2020

    @karolprzyb

    Exact same issue with test.py
    For some reason downgrading to dash 1.12.0 or lower also fixed the issue. I cannot for the life of me find why though.

  4. alexcjohnson commented on Jun 23, 2020

    @alexcjohnson
    Collaborator

    @meltbox360 can you try raising an exception at the top of test.py and post the traceback you see?

  5. karolprzyb commented on Jun 23, 2020

    @karolprzyb
    Traceback (most recent call last):
      File "C:/Users/xxx/PycharmProjects/plotGPS_OSM/Legacy/test6.py", line 14, in <module>
        import dash
      File "C:\Users\xxx\Anaconda3\envs\OSMNX and Dash\lib\site-packages\dash\__init__.py", line 1, in <module>
        from .dash import Dash, no_update  # noqa: F401
      File "C:\Users\xxx\Anaconda3\envs\OSMNX and Dash\lib\site-packages\dash\dash.py", line 15, in <module>
        from future.moves.urllib.parse import urlparse
      File "C:\Users\kprzybys\Anaconda3\envs\OSMNX and Dash\lib\site-packages\future\moves\__init__.py", line 8, in <module>
        import_top_level_modules()
      File "C:\Users\xxx\Anaconda3\envs\OSMNX and Dash\lib\site-packages\future\standard_library\__init__.py", line 810, in import_top_level_modules
        with exclude_local_folder_imports(*TOP_LEVEL_MODULES):
      File "C:\Users\xxx\Anaconda3\envs\OSMNX and Dash\lib\site-packages\future\standard_library\__init__.py", line 781, in __enter__
        module = __import__(m, level=0)
      File "C:\Users\xxx\PycharmProjects\plotGPS_OSM\Legacy\test.py", line 1, in <module>
        raise Exception("TEST")
    Exception: TEST
    
    Process finished with exit code 1
    
  6. karolprzyb commented on Jun 23, 2020

    @karolprzyb

    Good call. I think test is a reserved module name in python see standard_library __init__.py ~line 800 probably

    TOP_LEVEL_MODULES = ['builtins',
                         'copyreg',
                         'html',
                         'http',
                         'queue',
                         'reprlib',
                         'socketserver',
                         'test',
                         'tkinter',
                         'winreg',
                         'xmlrpc',
                         '_dummy_thread',
                         '_markupbase',
                         '_thread',
                        ]

    Still have no idea why this works with 1.12.0 and before though.
    I also have no idea if what I said above is true. Relatively new to python and haven't learned it too well yet.

  7. alexcjohnson commented on Jun 23, 2020

    @alexcjohnson
    Collaborator

    Thanks @meltbox360 that's perfect. Haha with exclude_local_folder_imports(*TOP_LEVEL_MODULES) obviously didn't accomplish its goal, as test is in your local folder 🤔 It's also puzzling to me why I don't see the same issue with test via future, maybe exclude_local_folder_imports does the right thing on Mac but not on Windows? Anyway normally it should be fine to make a module named test. There does seem to be a built-in test module but the docs strongly discourage its use so I have no idea why future would reference it. But this is indeed the same issue as org so should the same fix should work.

  8. amirdora commented on Jun 26, 2020

    @amirdora

    rename file test.py to something else

    for it was caused by my file name which i named "test.py".

  9. chriddyp commented on Aug 3, 2020

    @chriddyp
    Member

    For folks googling, I believe this error also appears in the form "AttributeError: module 'dash' has no attribute 'Dash'"

  10. added this to the OSS milestone on Aug 4, 2020
  11. changed the title [-][BUG] dash doesn't get imported when a file named "org.py" with specific content is present in the current directory[/-] [+][BUG] dash doesn't get imported when a file named "org.py" or "test.py" with specific content is present in the current directory[/+] on Aug 31, 2020
  12. changed the title [-][BUG] dash doesn't get imported when a file named "org.py" or "test.py" with specific content is present in the current directory[/-] [+][BUG] dash doesn't get imported when a file named "org.py", "dash.py", or "test.py" with specific content is present in the current directory[/+] on Oct 7, 2020
  13. changed the title [-][BUG] dash doesn't get imported when a file named "org.py", "dash.py", or "test.py" with specific content is present in the current directory[/-] [+][BUG] dash doesn't get imported when a file named "org.py", "dash.py", or "test.py" with specific content is present in the current directory // "AttributeError: module 'dash' has no attribute 'Dash'"[/+] on Oct 7, 2020
  14. eric-spitler commented on Oct 8, 2020

    @eric-spitler

    Tested Version = 1.16.3
    https://pypi.org/project/dash/1.16.3/

    In my case, there is no org.py, test.py, or dash.py, but my service does have test/__init__.py.

    When I rename test/ to tests/, the error goes away. There was also a separate, unrelated dependency that I was installing that included its own test package in its wheel, thus still allowing import test to do something, and therefore prevent import dash.

    Basically, for me to get functional I needed to ensure the application cannot perform import test, at which point import dash works.

  15. fohrloop commented on Nov 4, 2020

    @fohrloop

    Could this be prevented by just making dash to check if there is any test.py, or test folder, or org.py etc. in the folder where the application starts? And then raise and Exception with notification that your app will not work if you have files/folders named like this?

  16. shenshan commented on Nov 15, 2020

    @shenshan

    Same issue with my script named code.py

  17. added a commit that references this issue on Apr 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions