vscode-python: (Black) Formatter & Pytest fail with .env in Python

Environment data

VSCode: Most recent non-insider version. Python: 3.7 Black: Most recent.

Expected behaviour

Black is registered as a formatter. Code is formatted.

Actual behaviour

Nothing happens.

Steps to reproduce:

Working:

  1. Register black as the formatter of choice.
  2. Create a directory with the following structure:
root
- project
  - file_to_be_formatted.py
  1. Add some code into file_to_be_formatted.py that has formatting issues.
  2. Run black with alt + shift + f. It formats the code!

Failing:

  1. Register black as the formatter of choice.
  2. Create a directory with the following structure:
root
- .env (This contains "PYTHONPATH=./project" to make properly configure package detection, etc.)
- project
  - file_to_be_formatted.py
  1. Add some code into file_to_be_formatted.py that has formatting issues.
  2. Run black with alt + shift + f. It does not format the code

So having a .env file will silently make black fail. Running black from the command line on file_to_be_formatted.py does still work

I follow this: https://github.com/microsoft/python-language-server/blob/master/TROUBLESHOOTING.md#unresolved-import-warnings

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16

Most upvoted comments

I upvoted all of the above. I’ll restructured my project to

.vscode
setup.py
benchit/
   format.py
   dtypes.py uses `import format` 
tests/
   test.py uses `import dtypes`
  • I have no .env and no additional settings in settings.json.
  • I have jedi disabled.

With this setup VSCode:

  • properly discovers and runs tests under the Test tab
  • properly formats with black
  • has proper auto completion and detection of my package
  • i can properly install the package with pip install -e . from the project root