8000 Module import fails if a directory exists with same name · Issue #7229 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

Module import fails if a directory exists with same name #7229

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

Open
peterhinch opened this issue May 7, 2021 · 2 comments
Open

Module import fails if a directory exists with same name #7229

peterhinch opened this issue May 7, 2021 · 2 comments

Comments

@peterhinch
Copy link
Contributor

Tested under Unix and RP2.

To replicate create a directory (e.g. ~/test) containing foo.py:

def bar():
    print('bar')

Import behaves as expected. CPython behaves identically.:

MicroPython v1.14 on 2021-03-28; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> import foo
>>> dir(foo)
['__class__', '__name__', '__file__', 'bar']
>>> 

Create a subdirectory foo and repeat:

MicroPython v1.14 on 2021-03-28; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> import foo
>>> dir(foo)
['__class__', '__name__', '__path__']

CPython correctly imports the module:

[adminpete@capybara]: ~/test
$ python3
Python 3.8.5 (default, Jan 27 2021, 15:41:15) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo
>>> dir(foo)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'bar']
>>> 
@talbor49
Copy link
talbor49 commented May 7, 2021

Also interesting - notice how if we add a __init__.py file to foo/, both will act the same and import the directory(package).
It's almost like CPython has some heuristic that will make it skip an import option if it has no members.

Although I don't think this is a problem, and not a "failure" of module importing. These are just different ways to handle import ordering.

@peterhinch
Copy link
Contributor Author
peterhinch commented May 9, 2021

Another manifestation is this:

$ upython 
MicroPython v1.14-128-gca3d51f12-dirty on 2021-03-31; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> from foo import bar
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: no module named 'foo.bar'
>>> 

Again this works OK in CPython. It seems that MP assumes that the directory foo is a Python package, looks for bar in the package and fails.

I'm agnostic as to whether this issue is fixed or documented.

tannewt pushed a commit to tannewt/circuitpython that referenced this issue Nov 28, 2022
RP2040: have clock start high when SPI polarity high
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0