Sharing VS Code Settings for Python Projects¶
Introduction¶
Setting norms for developer tasks on a team project is crucial for maintaining code consistency and readability. Visual Studio Code (VS Code) allows us to manage distinct user and workspace settings. This flexibility ensures that while each developer can maintain their custom settings, the team adheres to the shared settings that ensure consistency across the project, especially when working with Python.
How We Share VS Code Settings Across Python Projects¶
Workspace Settings¶
Remote Development with VS Code¶
Info
When developing on remote servers, such as AWS, you will encounter a "Remote" configuration in your VS Code settings, similar to the workspace settings you use locally. This "Remote" configuration serves the same purpose, allowing you to enforce consistent coding standards and best practices across your team. Despite the different context, the functionality remains identical, ensuring a seamless and efficient development experience regardless of your code's location.
Workspace settings in VS Code are stored in a .vscode/settings.json
file within the project directory. These settings are shared among all
team members and include configurations essential for maintaining coding
standards and best practices in Python. Workspace settings override user
settings.
Example of .vscode/settings.json
¶
{
"python.pythonPath": "venv/bin/python",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.linting.flake8Enabled": true,
"python.formatting.provider": "black",
"editor.formatOnSave": true,
"files.exclude": {
"**/__pycache__": true,
"**/*.pyc": true
}
}
In the example above, the settings ensure:
- The correct Python interpreter is used.
- Linting is enabled using Pylint and Flake8.
- Code is formatted using Black on save.
- Certain files and directories (like __pycache__
and .pyc
files)
are excluded from the file explorer.
Location and Management¶
- The workspace settings file is located under the
.vscode
folder in your project’s root directory. - You can edit the settings directly via the Preferences: Open Workspace Settings (JSON) command in the Command Palette.
User Settings¶
User settings in VS Code are specific to an individual developer's environment and are stored outside the project directory. These settings allow each developer to customize their workspace without affecting the shared project settings.
How to Access User Settings¶
- Open VS Code.
- Navigate to
File
>Preferences
>Settings
(or use the shortcutCtrl + ,
on Windows/Linux orCmd + ,
on Mac). - In the Settings tab, ensure you are in the
User
settings mode.
Example of Customizing User Settings¶
{
"workbench.colorTheme": "Visual Studio Dark",
"editor.fontSize": 14,
"editor.fontFamily": "Fira Code, monospace"
}
In the example above, the settings customize:
- The color theme of the editor.
- The font size and font family for the editor.
Settings Precedence¶
VS Code allows configurations to be overridden at multiple levels. The settings precedence determines which configuration will ultimately apply when there are conflicts. Here is the order of precedence:
- Default settings: The default, unconfigured setting values.
- User settings: Apply globally to all VS Code instances.
- Remote settings: Apply to a remote machine opened by a user.
- Workspace settings: Apply to the open folder or workspace.
- Workspace Folder settings: Apply to a specific folder of a multi-root workspace.
- Language-specific default settings: Default values specific to a language, contributed by extensions.
- Language-specific user settings: User settings, but specific to a language.
- Language-specific remote settings: Remote settings, but specific to a language.
- Language-specific workspace settings: Workspace settings, but specific to a language.
- Language-specific workspace folder settings: Workspace folder settings, but specific to a language.
- Policy settings: Set by the system administrator and always override other setting values.
From this list, it is clear that workspace settings override user settings.
Practical Example¶
If a setting is configured at multiple levels, the most specific scope will apply. For instance:
- User settings (
settings.json
in the user profile):
- Workspace settings (
.vscode/settings.json
in the project):
In this scenario:
- The editor.formatOnSave setting will be
true
for the workspace, overriding the user's global setting offalse
. - The editor.fontSize will be
16
for the workspace, overriding the user's global setting of14
.
Implications for Our Project¶
Given the settings precedence, our project uses the
.vscode/settings.json
file to enforce consistent coding standards.
This file falls under workspace settings, which override user
settings.
Steps to Follow¶
- Clone the Repository: Ensure you clone the repository that
includes the
.vscode/settings.json
file.
-
Do Not Ignore
.vscode/settings.json
: Make sure the.vscode/settings.json
file is not included in the.gitignore
and is shared across the team via GitHub. -
Customize User Settings: Personalize your VS Code experience by adding your custom settings to the user settings file without altering the workspace settings.
Conclusion¶
Using distinct user and workspace settings in VS Code allows us to balance personal preferences with team-wide coding standards. By following this approach, we maintain code consistency and readability across the project while providing the flexibility for individual customization.
This approach helps maintain code consistency and readability across the team, ensuring that everyone adheres to the same standards while allowing for personal customization where it doesn't impact the project's coding practices.