Forum Discussion
How to run a pyspark code directly from a Github Repo?
- 1 year ago
Hi lchinelli,
Thank you for the follow-up.I have practically reproduced your scenario using modular, object-oriented PySpark code structured across multiple folders like models/ and utils/.
where modular, object-oriented PySpark code is executed across multiple folders (like models/ and utils/) using a main driver script.What I Did:
- Created a GitHub repository with the following folder structure:
/main.py /models/ └── cleaner.py └── validator.py /utils/ └── formatter.py- Each helper module (cleaner.py, validator.py, formatter.py) contains a class or function for a part of the logic (e.g., data cleaning, validation, formatting).
- In the Notebook, I used requests.get().text to dynamically fetch each .py file from GitHub raw URLs, then ran each with exec() to load the functions/classes into memory.
- Then, I fetched and ran main.py, which uses the imported modules to:
- Create a sample Spark DataFrame
- Apply cleaning and validation
- Format and return the final result
- The final output was printed using df.show() inside main.py.
Attached the screenshot below showing the successful execution and expected output:
If you have any further questions, please don't hesitate to contact us through the community. We are happy to assist you.
Best Regards,
Ganesh singamshetty.
Hello lchinelli,
Thank you for reaching out to the Microsoft Fabric Forum Community.
I’ve reproduced your scenario in Microsoft Fabric and achieved the desired outcome. You can run PySpark code directly from a GitHub repo by using a Fabric Notebook that dynamically fetches the script using a requests.get() call and exec() to run it. This notebook can then be triggered inside a Data Factory pipeline using a Notebook activity.
How It Works:
- Your .py file is stored in GitHub (public or private).
- The Fabric notebook reads and executes that code using the raw GitHub URL.
- A pipeline triggers the notebook and runs the code.
Example GitHub Code Used:
data = [
("Microsoft Fabric", 2025),
("Power BI", 2024),
("Synapse", 2023)
]
columns = ["Product", "Year"]
df = spark.createDataFrame(data, columns)
df.show()
Here’s a successful pipeline run in Microsoft Fabric using a notebook that fetches a PySpark script from GitHub:
If this information is helpful, please “Accept as solution” and give a "kudos" to assist other community members in resolving similar issues more efficiently.
Thank you.
- lchinelli1 year agoRegular Visitor
Is it possible to run code from another folders importing into a main.py file or in a main.ipynb? I said that because my code is OOP
- v-ssriganesh1 year agoCommunity Support
Hello lchinelli,
yes, it is possible to run modular, object-oriented PySpark code across multiple files/folders (just like in OOP projects), even within Microsoft Fabric Notebooks or from a main.py.
Thank you.- lchinelli1 year agoRegular Visitor
How?