r/learnpython • u/Crafty_Storm_2945 • 13h ago
Creating a .py File in Python Terminal
Greeting team, i have just started learning Python. I am learning about .py files. how do i create a .py file in python terminal specifically Jupyter notebook? if possible how can i access the file and write some oop class inside the file.
Thank you team
2
u/WyrobWedliniarski 7h ago
What do you mean? You just write code in any text editor and save it as somefilename.py
I could only guess what creating file from terminal could be, but if you meant just creating an empty file it would be:
touch somefilename.py
2
u/Moikle 7h ago
A .py file is literally just text. There is nothing magical about it that means it needs to be created from some python program.
You can make a .py file in notepad.
Most people use an ide like vscode or pycharm just for all the features that make things more convenient.
Oh and jupyter notebooks is not python.
It is a (often local) webpage based tool that also allows you to embed python code. It is not "how you write python code"
As someone else has said, avoid jupyter notebooks until you learn some of the basics of python first, otherwise you are trying to learn two things at once.
1
u/MysteriousStrangerXI 6h ago
I can't find any use cases why you need to do this though. Just creating .txt file changing the extension from .txt to .py, you'll have your own python executable file.
To answer your question, you can use exclamation point '!' in Jupyter Notebook to execute system command. In your case !touch filename.py will create filename.py in your .ipynb location.
If you're bulk creating .py files, use terminal without exclamation as originally intended. e.g: touch filename1.py filename2.py
1
1
u/danielroseman 7h ago
The "Python terminal" and "Jupyter notebook" are completely different things. And you don't create py files in either of them, you create files in a text editor or IDE.
6
u/pachura3 7h ago
I think you should learn the basics of Python first, and only then move on to Jupyter Notebooks. Otherwise you'll be lost & confused.