pythonimportjupyter-notebookamazon-sagemaker

Import custom modules in Amazon Sagemaker Jupyter notebook


I want to import a custom module in my jupyter notebook in Sagemaker. Trying the import from Untitled1.ipynb I have tried two different structures. The first one is:

enter image description here

Inside "package folder" there were the files "cross_validation.py" and "init.py". The followings commands have been tried:

from package import cross_validation
import package.cross_validation

The second one is

emak

and I have coded import cross_validation

In both cases I get no error at all when importing, but I can't use the class inside the module because I receive the error name Class_X is not defined

I also have restarted the notebook, just in case and it still not working. How could I make it?


Solution

  • You can add a __init__.py file to your package directory to make it a Python package. Then you will be import the modules from the package inside your Jupyter notebook

    /home/ec2-user/SageMaker
        -- Notebook.ipynb 
        -- mypackage
            -- __init__.py
            -- mymodule.py
    

    Contents of Notebook.ipynb

    from mypackage.mymodule import SomeClass, SomeOtherClass
    

    For more details, see https://docs.python.org/3/tutorial/modules.html#packages

    Thanks for using Amazon SageMaker!