Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[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
cannatag opened this issue Mar 3, 2020 · 12 comments · Fixed by #1493 or mstoelzle/solving-occlusion#28
Milestone

Comments

@cannatag
Copy link

cannatag commented Mar 3, 2020

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
>>>
@Charlenator
Copy link

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

@alexcjohnson
Copy link
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.

@karolprzyb
Copy link

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.

@alexcjohnson
Copy link
Collaborator

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

@karolprzyb
Copy link

karolprzyb commented Jun 23, 2020

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

@karolprzyb
Copy link

karolprzyb commented Jun 23, 2020

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.

@alexcjohnson
Copy link
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.

@amirdora
Copy link

rename file test.py to something else

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

@chriddyp
Copy link
Member

chriddyp commented Aug 3, 2020

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

@Marc-Andre-Rivet Marc-Andre-Rivet added this to the OSS milestone Aug 4, 2020
@chriddyp chriddyp 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 Aug 31, 2020
@chriddyp chriddyp 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 Oct 7, 2020
@chriddyp chriddyp 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'" Oct 7, 2020
@eric-spitler
Copy link

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.

@fohrloop
Copy link

fohrloop commented Nov 4, 2020

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?

@shenshan
Copy link

Same issue with my script named code.py

alexcjohnson added a commit to plotly/dash-core-components that referenced this issue Dec 10, 2020
alexcjohnson added a commit to plotly/dash-html-components that referenced this issue Dec 10, 2020
alexcjohnson added a commit to plotly/dash-table that referenced this issue Dec 10, 2020
alexcjohnson added a commit that referenced this issue Dec 10, 2020
alexcjohnson added a commit that referenced this issue Dec 10, 2020
alexcjohnson added a commit that referenced this issue Dec 10, 2020
alexcjohnson added a commit that referenced this issue Dec 10, 2020
alexcjohnson added a commit that referenced this issue Dec 10, 2020
rpkyle added a commit to plotly/dash-html-components that referenced this issue Apr 8, 2021
* Use authenticated Docker pulls (#167)

* Update CODEOWNERS

* Bump dot-prop from 4.2.0 to 4.2.1

Bumps [dot-prop](https://github.com/sindresorhus/dot-prop) from 4.2.0 to 4.2.1.
- [Release notes](https://github.com/sindresorhus/dot-prop/releases)
- [Commits](sindresorhus/dot-prop@v4.2.0...v4.2.1)

Signed-off-by: dependabot[bot] <support@github.com>

* improve dash import test
see plotly/dash#1143

* changelog for import bug fix

* update extract-attributes script for latest MDN page structure
and provide an error message if we see the page structure change
in the future

* description for setProps - to reduce warnings

* use dash loosen-testing-reqs branch and fix linting

* Fix spelling

* back to dev branch of dash

* bump ci images versions

* Update config.yml

* update component gen for MDN update Jan 2021

* error message if element count changes from expectation

* add reference to MDN PR with the math & svg addition

* bump to v1.1.2 (#176)

* Remove context reference from CircleCI (#175)

* fix ObjectEl data prop

* add deprecation notes on some elements, and fix for a few more changes to MDN

* update tests, and add tests for custom docs and ObjectEl

* move percy to py37 and separate out finalize step

* more test cleanup

* one more try to combine percy snapshots

* changelog for #178

* Add `allow` prop to html.Iframe

* Add `referrerPolicy` prop and update tests

* Add `referrerPolicy` prop and update tests

* Updated changelog  Fixes #77

* Update CHANGELOG.md

* Bump elliptic from 6.5.3 to 6.5.4

Bumps [elliptic](https://github.com/indutny/elliptic) from 6.5.3 to 6.5.4.
- [Release notes](https://github.com/indutny/elliptic/releases)
- [Commits](indutny/elliptic@v6.5.3...v6.5.4)

Signed-off-by: dependabot[bot] <support@github.com>

* update toolchain

* Update CI images & py37 -> py39

* Bump y18n from 4.0.0 to 4.0.1

Bumps [y18n](https://github.com/yargs/y18n) from 4.0.0 to 4.0.1.
- [Release notes](https://github.com/yargs/y18n/releases)
- [Changelog](https://github.com/yargs/y18n/blob/master/CHANGELOG.md)
- [Commits](https://github.com/yargs/y18n/commits)

Signed-off-by: dependabot[bot] <support@github.com>

* remove css

* version bump 1.1.3 (#184)

* version bump to 1.1.3

* update changelog

* artifacts

Co-authored-by: Ryan Patrick Kyle <rpkyle@users.noreply.github.com>
Co-authored-by: Marc-André Rivet <Marc-Andre-Rivet@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: alexcjohnson <alex@plot.ly>
Co-authored-by: Alex Johnson <johnson.alex.c@gmail.com>
Co-authored-by: John Bampton <jbampton@gmail.com>
Co-authored-by: Marc-André Rivet <marc-andre.rivet@plotly.com>
Co-authored-by: Ann Marie Ward <amward@fastmail.us>
HammadTheOne pushed a commit to HammadTheOne/dash that referenced this issue May 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
10 participants