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

When fetching code from a server, Brython can't handle namespace package #2426

Open
shlomil opened this issue Apr 26, 2024 · 3 comments
Open

Comments

@shlomil
Copy link

shlomil commented Apr 26, 2024

As of Python 3.3 package no longer require __init__.py . A namespace package will be created.
When you fetch code from the server with Brython using import, namespace package will not be detected.

Steps to reproduce:

# in commandline:
> mkdir namespace_mod
> echo "def some_func(): return 'some func'" > namespace_mod/some_module.py
> echo "from namespace_mod.some_module import some_func;print(some_func())" > import_ns_mod.py
> python3 import_ns_mod.py 
some func

Now try to run in Brython, adding an HTML with

<script id="main" type="text/python" src="import_ns_mod.py"></script>
# This will try to import and finally throw ModuleNotFoundError

if you convert the package to a regular package by adding an empty __init__.py :

> touch namespace_mod/__init__.py

In this case Brython will work.

@shlomil shlomil changed the title When fetching code form server, Brython can't handle namespace package When fetching code from a server, Brython can't handle namespace package May 6, 2024
@PierreQuentel
Copy link
Contributor

I have read PEP 420 and I'm afraid that it can't be supported by Brython.

In CPython, if a script is in a directory, and this directory has a subdirectory "subdir" (even empty), PEP 420 allows import subdir, making subdir a namespace package. The import machinery supports this by detecting that there is a subdirectory "subdir".

In Brython it is impossible to detect if a url corresponds to a directory. An Ajax request to the url (eg host/script_dir/subdir) might return the directory listing, or the content of a file index.html if it exists, or return an error message, depending on the server configuration.

Unless there is a solution that I don't see at the moment, we will have to add this feature to the list of differences between standard Python and Brython.

@shlomil
Copy link
Author

shlomil commented May 7, 2024

I thought it might be tricky or impossible. So this bug should be delth through documentation I guess.
Thanks you Pierre!

@shlomil
Copy link
Author

shlomil commented May 16, 2024

Now that I think about it, another fix to the problem may be to provide an additional option to __BRYTHON__.brython() which will provide the structure of the filesystem at given search path. The option may be a string containing the output of find . -name *.py and will provide hints to the import system about the file existence and location. This could also avoid other issues i.e when you try to fetch "mymod.py" and get a 404 before trying and finally succeeding to fetch mymod/__init__.py, for example. It could speed up the execution in such cases.
I think that implementing that will pave the way to implement namespace packages in Brython.

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