Python template file header and Pycharm

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

The above two lines of code are common in many Python programs, why should add two lines of code do?

The first line of action:

On the windows, there is no first line will not affect (Windows more extension to determine the file type), when a file is executed on Linux  ./test.py  form, it is necessary to add interpreter path information, informed the manner in which this file.

This is to prevent the user is not installed in the default python / usr / bin in the path, when the system sees this line will be set to search an installation path env python, and then calls the interpreter program in the corresponding path to complete the operation.

Other forms of the first line are

1 #!/usr/bin/python 

If the user does not have the python interpreter installed in / usr / bin / directory, an error occurs, it is necessary to change the directory.

The second line of action:

Specify the encoding is utf-8, so that the interpreter to read the source code in this encoded form. Because Python2 default is ASCII, it does not support Chinese, using Chinese error.

In python3 use utf-8 encoding form has been the default, so the second line of python3 is ignored, there are the same.

In summary

In order to be compatible across platforms and encoded form Python2, we generally have in the beginning of the file add these two lines of code.


 

Create a template in Pycharm in:

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

The above two lines of code are common in many Python programs, why should add two lines of code do?

The first line of action:

On the windows, there is no first line will not affect (Windows more extension to determine the file type), when a file is executed on Linux  ./test.py  form, it is necessary to add interpreter path information, informed the manner in which this file.

This is to prevent the user is not installed in the default python / usr / bin in the path, when the system sees this line will be set to search an installation path env python, and then calls the interpreter program in the corresponding path to complete the operation.

Other forms of the first line are

1 #!/usr/bin/python 

If the user does not have the python interpreter installed in / usr / bin / directory, an error occurs, it is necessary to change the directory.

The second line of action:

Specify the encoding is utf-8, so that the interpreter to read the source code in this encoded form. Because Python2 default is ASCII, it does not support Chinese, using Chinese error.

In python3 use utf-8 encoding form has been the default, so the second line of python3 is ignored, there are the same.

In summary

In order to be compatible across platforms and encoded form Python2, we generally have in the beginning of the file add these two lines of code.


 

Create a template in Pycharm in:

File - Settings - Editor - files and code templates, and then choose Python Scripts, enter the above two lines of code to the right, and then point to confirm it. It will automatically add two lines of code later when a new Python file Pycharm in.

 

 Modify Encoding:

Pycham lower right corner of the main screen (if you already have the second-line comments, then it will not be able to select other encoding forms)

 

 

Guess you like

Origin www.cnblogs.com/zhangyanlong/p/11297815.html