jupyterlab import a custom module

0

How do I import a custom module in jupyterlab sagemaker i have a file wr.py located inside the folder shared. I have the init.py properly placed.

I have a notebook n.ipynb at the root that contains the folder shared.

on the notebook

from shared.wr import fns

Does not work.

There is not way i can make os.getcwd() match the correct folder wher my notebooks and fiels are

  • I am using Glue

asked 2 months ago53 views
1 Answer
0

In order to import custom modules in JupyterLab on Amazon SageMaker. Firstly make sure that you have similar directory Structure:

root_folder/
│
├── n.ipynb
│
└── shared/
    ├── __init__.py
    └── wr.py

Here are a few approaches you can try to import your custom module:

1)Modify the Python path: At the beginning of your notebook, add the following code:

import sys
sys.path.append('/home/sagemaker-user')

This adds the root directory of your SageMaker environment to the Python path. After this, you should be able to import your module using:

from shared.wr import fns
  1. Alternatively ,add the following code to n.ipynb
import sys

# Add current directory to path
import os
current_dir = os.getcwd()
if current_dir not in sys.path:
    sys.path.append(current_dir)

# Import module
from shared.wr import fns
profile picture
answered 2 months ago
AWS
SUPPORT ENGINEER
revised 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions