Skip to content

Commit 3dddc69

Browse files
author
Kraig Brockschmidt
committed
Document .env files and pytest-cov caveat
#1483 #1489
1 parent 43db20e commit 3dddc69

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

docs/python/debugging.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Area: python
44
TOCTitle: Debugging
55
ContentId: 3d9e6bcf-eae8-4c94-b857-89225b5c4ab5
66
PageTitle: Debugging Python with Visual Studio Code
7-
DateApproved: 02/12/2018
7+
DateApproved: 03/14/2018
88
MetaDescription: Debugging Python with Visual Studio Code
99
MetaSocialImage: images/tutorial/social.png
1010
---
@@ -115,7 +115,7 @@ Sets optional environment variables for the debugger process beyond system envir
115115

116116
### `envFile`
117117

118-
Optional path to a file that contains environment variable definitions.
118+
Optional path to a file that contains environment variable definitions. See [Configuring Python environments - environment variable definitions file](environments.md#environment-variable-definitions-file).
119119

120120
## Debugging specific app types
121121

docs/python/environments.md

+43-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Area: python
44
TOCTitle: Environments
55
ContentId: 8fe4ca8b-fc70-4216-86c7-2c11b6c14cc6
66
PageTitle: Configuring Python Environments in Visual Studio Code
7-
DateApproved: 02/14/2018
7+
DateApproved: 03/14/2018
88
MetaDescription: Configuring Python Environments in Visual Studio Code
99
MetaSocialImage: images/tutorial/social.png
1010
---
@@ -22,6 +22,8 @@ You can also [manually specify an interpreter](#manually-specifying-an-interpret
2222

2323
> **Tip:** If you create a new conda environment while VS Code is running, use the **Reload Window** command to refresh the environment list.
2424
25+
The extension also loads an [environment variable definitions file](#environment-variable-definitions-file) identified by the `python.envFile` setting. The default value of this setting is `${workspaceFolder}/.env`.
26+
2527
## Choosing an environment
2628

2729
By default, the Python extension relies on the first Python interpreter it finds in the path, but it's easy to switch between environments.
@@ -125,6 +127,46 @@ To use a Python interpreter that's installed in a virtual environment:
125127
2. Configure the same `python.pythonPath` variable in `launch.json`.
126128
3. Ensure that the the libraries and modules you plan on using for linting are installed within the virtual environment.
127129

130+
## Environment variable definitions file
131+
132+
An environment variable definitions file is a simple text file containing key-value pairs in the form of `environment_variable=value`, with `#` used to mark comments. Multi-line values are not supported.
133+
134+
By default, the Python extension loads a file named `.env` in the current workspace folder, as identified by the default value of the `python.envFile` setting (see [General settings](settings-reference.md#general-settings). You can change the `python.envFile` setting at any time to use a different definitions file.
135+
136+
A debug configuration also contains an `envFile` property that also defaults to the `.env` file in the current workspace. (See [Debugging - standard configuration and options](debugging.md#standard-configuration-and-options). This property allows you to easily set variables for debugging purposes that replace those used in the default `.env` file.
137+
138+
For example, when developing a web application you might want to easily switch between development and production servers. Instead of coding the different URLs and other settings into your application directly, you could use separate definitions files for each. For example:
139+
140+
**dev.env file**
141+
142+
```text
143+
# dev.env - development configuration
144+
145+
# API endpoint
146+
MYPROJECT_APIENDPOINT=https://my.domain.com/api/dev/
147+
148+
# Variables for the database
149+
MYPROJECT_DBURL=https://my.domain.com/db/dev
150+
MYPROJECT_DBUSER=devadmin
151+
MYPROJECT_DBPASSWORD=!dfka**213=
152+
```
153+
154+
**prod.env file**
155+
156+
```text
157+
# prod.env - production configuration
158+
159+
# API endpoint
160+
MYPROJECT_APIENDPOINT=https://my.domain.com/api/
161+
162+
# Variables for the database
163+
MYPROJECT_DBURL=https://my.domain.com/db/
164+
MYPROJECT_DBUSER=coreuser
165+
MYPROJECT_DBPASSWORD=kKKfa98*11@
166+
```
167+
168+
You can then set the `python.envFile` setting to `${workspaceFolder}/prod.env`, then set the `envFile` property in the debug configuration to `${workspaceFolder}/dev.env`.
169+
128170
## Python interpreter for debugging
129171

130172
By default, the debugger uses the same `python.pythonPath` setting as for other features of VS Code. Specifically, the value for `pythonPath` in the debugger settings simply refers to the main interpreter setting as follows:

docs/python/settings-reference.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Area: python
44
TOCTitle: Settings Reference
55
ContentId: d256dc5c-95e9-4c02-a82f-947bf34a3517
66
PageTitle: Settings Reference for Python
7-
DateApproved: 12/12/2017
7+
DateApproved: 03/14/2018
88
MetaDescription: Settings Reference for the Python extension in Visual Studio Code
99
MetaSocialImage: images/tutorial/social.png
1010
---
@@ -20,7 +20,7 @@ Refer to [User and workspace settings](/docs/getstarted/settings.md) to find our
2020
| --- | --- | --- |
2121
| python.pythonPath | `"python"` | Path to the python interpreter. |
2222
| python.venvPath | `""` | Path to folder with a list of Virtual Environments, such as `~/.pyenv`, `~/Envs`, `~/.virtualenvs`. |
23-
| python.envFile | `"${workspaceFolder}/.env"` | Absolute path to a file containing environment variable definitions. |
23+
| python.envFile | `"${workspaceFolder}/.env"` | Absolute path to a file containing environment variable definitions. See [Configuring Python environments - environment variable definitions file](environments.md#environment-variable-definitions-file). |
2424
| python.terminal.launchArgs | `[]` | Launch arguments given the Python interpreter when running a file. |
2525
| python.terminal.executeInFileDir | `false` | Indicates whether to run a file in the file's directory instead of the current folder. |
2626
| python.terminal.activateEnvironments | `true` | Indicates whether a selected virtual or conda environments is activated when using the **Python: Create Terminal** command or any other operation involving the terminal, such as the **Send Python File to Terminal** menu command. If `false`, skips activating virtual and conda environments before running the commands. |
@@ -162,7 +162,7 @@ Workspace symbols are symbols in C source code generated by the ctags tool (desc
162162
| --- | --- | --- | --- |
163163
| pyTestEnabled | `false` | Specifies whether pytest is enabled for unit testing. | [Unit testing](/docs/python/unit-testing.md) |
164164
| pyTestPath | `"py.test"` | Path to pytest. Use a full path if pytest is located outside the current environment. | [Unit testing](/docs/python/unit-testing.md) |
165-
| pyTestArgs | `[]` | Arguments to pass to PyTest, with each argument specified as an item in the array. | [Unit testing](/docs/python/unit-testing.md) |
165+
| pyTestArgs | `[]` | Arguments to pass to PyTest, with each argument specified as an item in the array. When debugging unit tests with pytest-cov installed, include `--no-cov` in these arguments. | [Unit testing](/docs/python/unit-testing.md) |
166166

167167
### Nose framework
168168

docs/python/unit-testing.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Area: python
44
TOCTitle: Unit Testing
55
ContentId: 9480bef3-4dfc-4671-a454-b9252567bc60
66
PageTitle: Unit Testing Python in Visual Studio Code
7-
DateApproved: 11/10/2017
7+
DateApproved: 03/14/2018
88
MetaDescription: Unit Testing Python in Visual Studio Code
99
MetaSocialImage: images/tutorial/social.png
1010
---
@@ -62,6 +62,9 @@ See [unittest command-line interface](https://docs.python.org/3/library/unittest
6262

6363
You can also configure pytest using a `pytest.ini` file as described on [PyTest Configuration](https://docs.pytest.org/en/latest/customize.html).
6464

65+
> **Note**
66+
> If you have the pytest-cov coverage module installed, VS Code doesn't stop at breakpoints while debugging because pytest-cov is using the same technique to access the code being run. To prevent this behavior, include `--no-cov` in `pyTestArgs` when debugging tests. (For more information, see [Debuggers and PyCharm](http://pytest-cov.readthedocs.io/en/latest/debuggers.html) in the pytest-cov documentation.)
67+
6568
### Nose configuration settings
6669

6770
| Setting<br/>(python.unitTest.) | Default | Description |

0 commit comments

Comments
 (0)