Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us at FabCon Vienna from September 15-18, 2025, for the ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM. Get registered

Reply
Anonymous
Not applicable

Import notebook from .py file

I have notebook code that has been exported as .py file and is formatted like this:

 

# Fabric notebook source

# METADATA ********************

# META {
# META   "kernel_info": {
# META     "name": "synapse_pyspark"
# META   },
# META   "dependencies": {
# META     "lakehouse": {
# META       "default_lakehouse": "<Lakehouse ID>",
# META       "default_lakehouse_name": "Metadata",
# META       "default_lakehouse_workspace_id": "<Workspace ID>"
# META     }
# META   }
# META }

# MARKDOWN ********************

# # Import Core Code

# CELL ********************

%run "./00 - Connections"

 

If I import this, it just puts all this code into a single cell in a new notebook (which is obviously not what I want). Am I doing something wrong? It would be an enormous waste of my time to reformat all of this manually. Any help is much appreciated!

1 ACCEPTED SOLUTION

Hi @Anonymous,

Thanks for your response! You’re right when exporting a Fabric notebook as a .py file and re-importing it, Fabric does not automatically split it into multiple cells. Currently, Fabric only maintains cell structure when working with .ipynb files.

If you need to frequently transfer notebooks, I recommend working with .ipynb instead of .py for seamless import/export.

I agree that Fabric should ideally recognize its own .py format properly. If this is an important workflow for you, I encourage you to share your feedback and vote for this idea on the Power BI Ideas Forum:🔗 Home, This helps the Microsoft team prioritize feature enhancements based on user demand.

If this helps, then please Accept it as a solution and dropping a "Kudos" so other members can find it more easily.
Thank you.

View solution in original post

3 REPLIES 3
v-ssriganesh
Community Support
Community Support

Hi @Anonymous,

Thank you for reaching out to the Microsoft Fabric Community.

I understand that you are facing an issue were importing a .py file into a notebook results in all the code being placed into a single cell.

You can modify the .py script to include markers or comments that can be recognized by the notebook environment to split the code into different cells automatically.
                                                                                 (OR)

Another approach is to import the .py file as a module in the notebook. This allows you to call functions and classes defined in the .py file without having to reformat the entire script. Here are the steps:

  1. Create a Python file containing the code you want to import.
  2. Save the Python file with a .py extension.
  3. Move the Python file to the same directory as your Jupyter notebook.
  4. Import the Python file as a module in your Jupyter notebook using the import statement.

I hope these suggestions assist you in resolving the issue. If they do, please accept it as a solution and give a "Kudos" to help other members find it more easily.

Thank you.

Anonymous
Not applicable

Thanks for your reply!

 

I don't want the code as module.

 

I know I can modify the code to be recognized correctly as several notebook cells, but that's exactly what I hoped to avoid. Also, as far as I can tell, if I download a notebook as .py, and upload the result, it also gets put into a single cell. So Fabric doesn't even recognize its own formatting and only the .ipynb-format is correctly recognized upon import. Or am I missing something?

Hi @Anonymous,

Thanks for your response! You’re right when exporting a Fabric notebook as a .py file and re-importing it, Fabric does not automatically split it into multiple cells. Currently, Fabric only maintains cell structure when working with .ipynb files.

If you need to frequently transfer notebooks, I recommend working with .ipynb instead of .py for seamless import/export.

I agree that Fabric should ideally recognize its own .py format properly. If this is an important workflow for you, I encourage you to share your feedback and vote for this idea on the Power BI Ideas Forum:🔗 Home, This helps the Microsoft team prioritize feature enhancements based on user demand.

If this helps, then please Accept it as a solution and dropping a "Kudos" so other members can find it more easily.
Thank you.

Helpful resources

Announcements
May FBC25 Carousel

Fabric Monthly Update - May 2025

Check out the May 2025 Fabric update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.