[Python] Google Colab custom module has no attribute

Discussão em 'Python' iniciado por Stack, Julho 25, 2025.

  1. Stack

    Stack Membro Participativo

    What I am trying to do is to define a function in a custom module on Google Colab. I created a *.py file to define the function. It is

    /content/gdrive/My Drive/Colab/myfunc.py


    Then I created a *.ipynb file at the same folder. It is

    /content/gdrive/My Drive/Colab/test.ipynb


    In myfunc.py, the code is

    def somefunc(a)
    return a*2


    In test.ipynb, the code is

    cd /content/gdrive/My\ Drive/Colab
    import myfunc
    myfunc.somefunc(2)


    Then it occurs error "AttributeError: module 'myfunc' has no attribute 'somefunc'"

    I also used

    dir(myfunc)


    in test.ipynb, which shows

    ['__builtins__',
    '__cached__',
    '__doc__',
    '__file__',
    '__loader__',
    '__name__',
    '__package__',
    '__spec__']


    My function 'somefunc' is not in the attributes!!!

    This problem only occurs on Colab. I tried it on my computer and an online Jupyter notebooks. They are both working correctly.

    PS: No matter whether I put an empty __init__.py file in the same folder or not, the problem always exists.

    Continue reading...

Compartilhe esta Página