Please HELP! Issue downloading as Python file from Jupyter Notebook - the py script is blank?

Hi, i’m a student and really need help on this basic topic for a one day assignment. Trying to download appropriate .py file from Jupyter notebook. Any help would be appreciated, please!

I’ve corrected all the errors in Jupyter Notebook. However, when I go to download it as a Python file, I get the following (screenshot below). It is a .py file, but the entire script is missing from it. Currently, I am stuck on how to get the appropriate file where the full script is there. Can someone please advise?

I also attached a screenshot of my Jupyter Notebook, which I accessed through Anaconda Navigator. I’m on Windows/PC.

.py file

My Jupyter Notebook script

How are you downloading it? Is this via File -> Save and Export Notebooks As... -> Executable Script?

It’s possible you just need to save your notebook with a specific name rather than Untitled1. Although I don’t see why that’d be the case.

I get the file as… File → Download as → .py (Python). I’ve also tried it under several different Notebook names and it still show the same result.

I have Python 3.12 (64 bit) install on my computer if that makes any difference.

The screenshot of your terminal [1] looks like you ran the file, rather than viewing it in a text editor. How are you trying to open it?


  1. by the way, it’s better to share formatted text, rather than screenshots ↩︎

This is what the file looks like when I download it. It’s a python file. I just open the file as it is.

image

It’s possible that when you open the file, your computer will simply run the script by default (in other words, it defaults to python.exe as the program to open .py files). Try opening it in a text editor.

Don’t click on “Open file”; that’s runs it.

Click on “Show in folder” (the open folder icon) and then, in the File Explorer, right-click on the file and click “Open with…” and pick the editor that you’re going to use.

Ok, when I open in Notepad i get this (below), which looks correct. But i’m supposed to submit the assignment as a .py file, so I just wanna make sure i’m not missing anything?


#!/usr/bin/env python
# coding: utf-8

# In[1]:


import platform
vers = platform.python_version()
assert vers[0] == '3', "You must use Python 3, "+vers+" is not acceptable"
print("Python 3 confirmed.")

num_cakes=input("How many cakes would you like to make?")
recipe_mult=12/int(num_cakes)
sweet_butter=str(125*recipe_mult)+"g sweet butter"
sugar=str(225*recipe_mult)+"g sugar"
eggs=str(max(1,round(recipe_mult)))+" eggs"
vanilla=str(recipe_mult)+" tsp vanilla extract"
chocolate=str(recipe_mult)+" tsp chocolate extract"
flour=str(225*recipe_mult)+"g flour"
salt=str(0.5*recipe_mult)+" tsp salt"
chips=str(200*recipe_mult)+"g chocolate chips"
print(sweet_butter) 
print(sugar) 
print(eggs) 
print(vanilla) 
print(chocolate) 
print(flour) 
print(salt) 
print(chips)

Thanks, I’ll try that, but I there’s no editing needed for this. I did it all in Jupyter Notebook.

Well the good news is that you’ve already run the file several times (when you were just clicking on it), and if that’s the output you’re looking for then you’re all set.

I can’t speak for any coding standards that your course might expect though. Maybe they want a certain style or specific implementation.

Okay, thank you. I think i got it. Appreciate the help!!

for i in range(10):
    print((9-i)*' '+['^','/_%s\\'%['|_'*~-i,'|/o\|_','|_\_/_|_'][i%6//4*i%3]][i>0])

[1]


  1. Adapted from here ↩︎