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

dynamic import of module fails from readonly location #124168

Open
thiri42 opened this issue Sep 17, 2024 · 1 comment
Open

dynamic import of module fails from readonly location #124168

thiri42 opened this issue Sep 17, 2024 · 1 comment
Labels
pending The issue will be closed if no feedback is provided topic-importlib type-bug An unexpected behavior, bug, or error

Comments

@thiri42
Copy link

thiri42 commented Sep 17, 2024

Bug report

Bug description:

# Source https://medium.com/@Doug-Creates/dynamically-import-a-module-by-full-path-in-python-bbdf4815153e
# Function to dynamically import a module given its full path
def import_module_from_path(module_name, path):
    # Create a module spec from the given path
    spec = importlib.util.spec_from_file_location(module_name, path)

    # Load the module from the created spec
    module = importlib.util.module_from_spec(spec)

    # Execute the module to make its attributes accessible
    spec.loader.exec_module(module)

    # Return the imported module
    return module

The problem is that the method exec_module tries to write the pyc file and throws an Exception when the permission for the module path is readonly.

I would suggest to check the write permission before creating the pyc file.

CPython versions tested on:

3.11

Operating systems tested on:

Linux

@thiri42 thiri42 added the type-bug An unexpected behavior, bug, or error label Sep 17, 2024
@graingert
Copy link
Contributor

@thiri42 can you show the full traceback? importlib has code to defend against this case

_bootstrap._verbose_message('could not create {!r}: {!r}', path,

@graingert graingert added the pending The issue will be closed if no feedback is provided label Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending The issue will be closed if no feedback is provided topic-importlib type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants