Home > Software engineering >  Setting a python script default when creating a new file
Setting a python script default when creating a new file

Time:11-11

I would like to set a default custom script whenever creating a new .py file or .ipynb file in Pycharm or VS Code.

I would like the default script to load looking as such:

import time
import datetime

start = time.time()
# Code here.

end = time.time()
print(str(datetime.timedelta(seconds=end - start)))

Is it possible to set default scripts in IDEs? Many Thanks.

CodePudding user response:

It is currently not possible to set a new file template. Maybe the code snippet will help you.

You can easily define your own snippets without any extension. To create or edit your own snippets, select User Snippets under File > Preferences (Code > Preferences on macOS), and then select the language (by enter image description here

Then change the open file to the following.

{
    "time":{
        "prefix": "time",
        "body": [
        "import time",
        "import datetime",
        "start = time.time()",
        "$1",
        "end = time.time()",
        "print(str(datetime.timedelta(seconds=end - start)))"],
        "description": "",
        "isFileTemplate": true
    }
}

Then enter time in the .py file to get the fragment.

enter image description here

More information about the code snippet can be found here.

  • Related