[Python] Cannot import name 'function' from 'module.py' - same directory, another function...

Discussão em 'Python' iniciado por Stack, Março 11, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Jupyter Notebook - we'll call it main, and I'm running it piecemeal in Jupyter Lab 3.0.14 using a Python 3 environment.

    Within this notebook, I'm trying to import custom functions from a module, we'll call this one module.py, which is in the same directory as main.

    It looks something like this:

    //main

    import sys
    import os
    from module import foo, bar


    //module.py

    def foo():
    return

    def bar():
    return


    When I run the import statements in main, it throws an error

    ImportError: cannot import name 'bar' from 'module' (/module.py)


    If I run

    from module import foo


    or

    from module import *


    It's fine (although calling the functions will throw an error that the function is not defined). In fact, it will import foo even if there's no function named foo in module.py. It won't import any of my own functions by name. I feel like I must be missing something fundamental, here...

    Any help would be greatly appreciated!!

    Continue reading...

Compartilhe esta Página