Code Style. YAML
Use this page to configure formatting options for YAML files. When you change these settings, the Preview pane shows how this will affect your code.
Tabs and Indents
In this field, specify the number of spaces to be inserted for each indent level.
Keep indents on empty lines
If this checkbox is selected, PyCharm retains indents on empty lines as if they contained some code. If the checkbox is cleared, PyCharm deletes spaces on empty lines.
Indent sequence value
If selected, YAML sequence values are indented relative to the parent key item.
Wrapping and braces
In this tab, customize the exceptions, brace placement and alignment options that PyCharm will apply to various code constructs on reformatting the source code. Check the results in the Preview pane.
Alignment takes precedence over indentation options.
In this field, specify multiple right margins. You can leave a default value or enter the number of spaces for your margin. If you want to specify several margins, enter numbers separated by comma.
Keep when reformatting
Use the checkboxes to configure exceptions that PyCharm will make when reformatting the source code. For example, by default, the Line breaks checkbox is selected.
If your code contains lines that are shorter than a standard convention, you can convert them by disabling the Line breaks checkbox before reformatting.
Align values in maps
Use this list to specify how PyCharm should align key-value pairs. The available options are:
- Do not align : the attributes in sequential lines will be not aligned.
- On colon : the attributes in sequential lines will be aligned against the colon.
- On value : the attributes in sequential lines will be aligned against the value.
Use these options to specify formatting for the YAML sequence values:
-
Sequence on new line : if selected, each new sequence inside a nested sequences block is put on a separate line:
— — — nested sequences block
Otherwise, nested sequences will be kept on a single line:
— — — nested sequences block
Otherwise, they are kept on a single line:
Set from
The link appears in the upper-right corner of the page, when applicable. Click this link and choose the language to be used as the base for the current language code style.
To return to the initial set of code style settings and discard the changes, click Reset .
File templates
File templates are specifications of the default contents for new files that you create. Depending on the type of file you are creating, templates provide initial code and formatting expected in all files of that type (according to industry or language standards, your corporate policy, or for other reasons).
PyCharm provides predefined templates for all supported file types suggested when you create a new file. The set of suggested file types depends on the module and configuration, as well as the properties of your current location in the Project tool window. For example, PyCharm will not suggest creating a Python file outside of the Python source or test directories.
Manage and configure file templates
- Press Control+Alt+S to open the IDE settings and then select Editor | File and Code Templates .
In case of multiple projects, settings apply to the main project, which is the first in the list in the Project tree.
For more information, refer to File and Code Templates.
By default, the list of templates contains only predefined templates provided by PyCharm. Some of them are internal, which means they cannot be deleted or renamed. PyCharm shows the names of internal templates in bold. The names of templates that you modified, as well as custom templates that you created manually, are shown in blue.
The following procedures describe how to create file templates. Similar procedures can be used for creating include templates.
Create a new file template
- Press Control+Alt+S to open the IDE settings and then select Editor | File and Code Templates .
- Using the Scheme list, select the scope to which the file template apply:
- Default : configure file templates for the entire application. These templates are available in all projects that you open with the current IDE instance. Use them as your personal templates that you prefer regardless of the specific project. PyCharm stores global templates in the IDE configuration directory under fileTemplates .
- Project : configure file templates specific for the current project. These templates are available to everyone who works on this project. PyCharm stores them in the project folder under .idea/fileTemplates .
- On the Files tab, click and specify the template name, file extension, name of the resulting file, and body of the template.
- Click Apply to save the changes and close the dialog.
Copy an existing file template
- Press Control+Alt+S to open the IDE settings and then select Editor | File and Code Templates .
- On the Files tab, click and modify the name, file extension, and body of the template as necessary.
- Click Apply to save the changes and close the dialog.
Save a file as a template
- Open a file in the editor.
- Go to File | Save File as Template .
- In the Save File as Template dialog, specify the new template name and edit the body, if necessary.
- Click Apply to save the changes and close the dialog.
Syntax
File templates use the Velocity Template Language (VTL), which includes the following constructs:
- Plain text rendered as is.
- Variables that are replaced by their values. For example, $ inserts the name provided by the user when adding the file.
- Various directives, including #parse, #set , #if , and others.
Start typing $ or # to refer to completion suggestions for available variables and directives.
For more information, refer to the VTL reference guide.
The following example shows the default template for creating a Gherkin features file in PyCharm:
# Created by $
In this template, $ and $ are template variables. When you create a new Gherkin file, this template generates a feature file with contents similar to the following:
# Created by jetbrains at 25.06.2018 Feature: #Enter feature name here # Enter feature description here Scenario: # Enter scenario name here # Enter steps here
Use requirements.txt
PyCharm provides integration with the major means of requirements management and makes it possible to track the unsatisfied requirements in your projects and create a virtual environment based on the requirements.txt file .
Define requirements
- From the Tools menu, select Sync Python Requirements .
- In the opened dialog, specify the name of the requirements file. The recommended name for the requirements file is requirements.txt . When a file with this name is added to the root project directory, it is automatically detected by Python Integrated tools.
- Select the method of handling versions of the required libraries. The version numbers can be defined:
| Method | Example |
|---|---|
| Strong equality | Django==3.0.3 |
| Greater or equal | Django>=3.0.3 |
| Compatible version | Django~=3.0.3 |
| Policy | Action |
|---|---|
| Remove unused requirements | Deletes records that correspond to unused libraries and packages. |
| Modify base files | Allows modifications in the base requirements files (if any is referenced in the requirements.txt file). |
| Keep existing version specifier if it matches the current version | Leaves the version number unchanged if it satisfied the selected method versions handling. |

You can also run pip freeze > requirements.txt in the command line to generate a requirements.txt file for your project. See https://pip.pypa.io/en/stable/reference/pip_freeze/ for more details.
If the name of the requirements file differs from requirements.txt or when you have several requirements files in one project, you have to notify PyCharm about the requirements file you want to apply.
Configure the default requirements file
- Press Control+Alt+S to open the IDE settings and then select Tools | Python Integrated Tools .
- In the Package requirements file field, type the name of the requirements file or click the browse button and locate the desired file.
- Click OK to save the changes.
Though you can always run the Sync Python Requirements to update the requirements file, PyCharm provides quick fixes that enable populating this file.
Update a requirements file

- In an import statement of a Python file, click a package which is not yet imported. PyCharm suggests a quick-fix:
- Select and apply the suggested quick-fix. The package is added to the dependency management file.
PyCharm provides quick fixes and notifications related to the unsatisfied dependencies.
Install the required packages from requirements.txt

- When you open requirements.txt or a Python file on a project that contains requirements.txt , PyCharm checks whether all the packages specified in requirements.txt are installed for the current Python interpreter.
- If any packages are missing, a notification bar is displayed at the top of the editor. Click Install requirements .
The notification bar is displayed when the Unsatisfied package requirements inspection is enabled. You can enable it in the Settings | Editor | Inspections dialog.
You can also click Ignore requirements . In this case, you will be able to remove the packages from the list of the ignored packages.
Manage the ignored dependencies

- Press Control+Alt+S to open project Settings .
- Go to Editor | Inspections .
- In the list of the inspections, select Unsatisfied package requirements .
- Preview the list of the ignored requirements and click the Add icon () to add them.
How to Create Yaml File

Creating an YAML file requires a basic understanding of YAML and why it’s used as configuration files in Kubernetes, Ansible, CircleCI etc.
What is an YAML File?
YAML or ‘Yet Another Markup Language’ is a human-readable data serialization language. It is designed in order to be both human-readable and also efficient for parsing.
The YAML specification was written in 2006 by David Preshing, the creator of PHP Markdown, who wanted a universal markup language that was easy for humans to read and write, but still allowed for significant information density and reasonable balancing of tradeoffs between the machine time required to process it and the machine time required to produce it.
These are 3 ways to create a new YAML File.
- Get the YAML Configuration template file such as Ansible Play file template. Here is the nginx-deplymnet.yaml
- Use Text Editor or Online tool to create the YAML Syntax / Structure.
- Create a file from the YAML URL.
1. Using YAML Template
YAML is mostly used for the configuration files. So to avoid mistakes, find the right template for the YAML configuration for Kubernetes, Ansible or CircleCI.
Here is an example of the Kubernetes nginx configuration deployment file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.14.2 ports: - containerPort: 80
Use this YAML validator tool to validate the YAML
Once file data are validated, save the file with the extension of .yaml. This section helps to create valid YAML file.
2. Using Text Editor or Online YAML Tool
Open a YAML Formatter tool from the link below
Copy and Paste the YAML Data which is mentioned in Option 1 in the Input tool of the online tool.
Download this file using the download icon mentioned on the Right input area. It will download the YAML file.
This https://codebeautify.org/yaml-beautifier already supports the YAML validation. This will create a New YAML file and will allow downloading.
Now if you already have a YAML document or file, you can upload it to this tool and modify or delete a few objects, an array of the YAML and download the updated and valid YAML file.
This tool also help to create a YAML from text file. Upload text file using the upload file button and YAML will be automatically validated and beautified.
3. Create a file from the YAML URL
Developer needs to work with data and files resides on the cloud servers and nowadays 95% of web and apps are hosted on AWS, Azure, Google Cloud or Digital Ocean.
Developer can use remote URL to save and edit the YAML files.
Here are few samples of the YAML URL. Now click on these YAML samples.
Once this link is open, use save as functionality of the browser and save. It will generate YAML file and save it on your device.
How to create a YAML file in linux
echo "apiVersion: apps/v1" > config.yml
touch config.yml
I hope this article helps you to understand basic ways to make or create valid YAML files and beautify them.
