Monday, June 10, 2024

What is a scratch file in PyCharm?

 As the name implies it is a scratch file and not attached to any project. It is extremely useful to run some temporary stuff before you plan on using in a project. It is saved to a separate location and indexed. The code on the scratch can be run and debugged. What is more it is color coded.

How do you work with a scratch file?

It is very easy as shown in the PyCharm IDE. From the main menu you can access this dropdown when you click File.


When you choose New Scratch File or use the shortcut Ctrl+Alt+Shift+Insert the next dropdown is displayed. 

There are lots of programs you can run as shown. These are all functional and bring in great value to the IDE.

Herein, for demo I shall create a python file and test run it. When I choose a Python code, it creates a file scratch.py and saves it to the Scratches directory. I write this simple code in the scratch.py file,

print("Scratchy") as shown.


Now you can run this file and the interpreter picks it up. After highlighting the scratch.py file in the Scratches folder, with a right click you display this dropdown.


In the drop-down, you click Run 'Scratch'. The code gets run and you see the result of running the code.
You can rerun the code using the control shown with the horizontal arrow insert. There is a main run control in the main menu and it will not run the code in Scratch folder.


You can rename the file. You need to choose the file and right click. From the drop-down click Refactor and rename the file.

In addition to fully functional files show above, you can also use the Scratch Buffers. These can not be run as they are just text files. You need to create a scratch buffer to store these(also indexed and rotated). It is an excellent feature to keep codes, code scratches, and anything else you want to come back to.

How to create a Scratch buffer file?

Ctrl+Shift+A brings up a window. In the window start type in "New Scratch.." and you get some choices. Choose New Scratch Buffer and a buffer1.txt is created wherein you can keep your notes. These files are also stored in the Scratches node.












No comments: