Python formatter "black" stopped working? - python

Can't open an issue on github atm because I get a 500 error for 30minutes now when trying to create the issue.
VS Code Version: 1.59.0
OS Version: Windows 10 Professional 64bit 21H2 19043.1151
Steps to Reproduce:
Clean install of VS Code
Clean install of Python 3.9.6
pip install black (version 21.7b0)
Install VS Code extension "Python"
in VS Code set formatter to "black"
open project folder
open test.py file from project
save unformatted file with CTRL+S --> "Formatting with black" appears in the bottom corner but nothing happens
user settings.json
{
"python.formatting.provider": "black",
"editor.formatOnSave": true
}
workspace settings.json
{
"python.formatting.provider": "black"
}
It works when I call "black" via CMD --> "black test.py"
reformatted test.py
All done! ✨ 🍰 ✨
1 file reformatted.
Already tried reinstalling VS Code / Python and restarting the PC???

Related

Run active python file in active terminal or specific location pointing terminal with keybinding

In VsCode I am trying to find how i can :
Run ACTIVE python file in ACTIVE integrated terminal with shift+enter
Note: I already have a key binding to "Run active file in active
terminal" but i does not include the python interpreter path/keyword
before launching the command so it is just opening the file)
I know there are multiple ways to run python file in terminal (F5 for debug etc...) and "Shift+Enter" works quite well if you do not have to deal with location of the running file.
In this case when "Shift+Enter", I would like to "cd my/project/folder/where/my/file/is" and then "python3 myFileToRun.py", so having a terminal opened at the file's location will allow me to quickly hit the shortcut without any locations issues.
I see 3 steps to respect:
open terminal -> (?cmd to launch terminal in vscode and place in editor mode)
cd to file's folder location -> (cd C:/complete/path/to/file)
run python file (python3 myPythonFile.py)
Here is an example of what i have tried:
keybindings.json
{
"key": "shift+enter",
"command": "-python.execSelectionInTerminal",
"when": "editorTextFocus && terminalFindFocused && terminalProcessSupported && editorLangId == 'python'"
},
Python: Run Python File command calls python with absolute paths for both an interpreter and script. It does not perform cd though, instead running from the project's default location.
It's the same as the Run icon visible when you have .py file opened with Python extension installed. You can reassign default Shift-Enter command (Python: Run Selection/Line in Python Terminal) or you can create a new binding, e.g. Ctrl+Enter and copy When from the Shift-Enter one, whichever suits you more.

VSCode Code Runner not following settings.json

I set my settings.json to Anaconda python 3.6.10 but Code runner still runs the code in 3.7.6. I used OS to check and in fact, that is true. I then used terminal to run my file and it is Python 3.6.10. How can I change my Code Runner to run at whatever version I set in the .vscode settings.json file?
"code-runner.executorMap": {
"python": "$pythonPath -u $fullFileName",
},
With this it should follow the anaconda settings in VS Code.

running python script as exe

I wrote a basic script in python 3.7 that does what I need. However it needs to run on another person's computer. I want to run this as an exe then just change the icon logo.
I have installed py2exe (I believe). Below is the python script:
pip install py2exe
import os
os.startfile(r"\\ComputerName\c$\users\UserName\desktop\Lullaby wav.wav")
I have another file, that looks like this (basing this off this thread Can I somehow "compile" a python script to work on PC without Python installed? ):
import sys
from distutils.core import setup
import py2exe
entry_point = sys.argv[1]
sys.argv.pop()
sys.argv.append('py2exe')
sys.argv.append('-q')
opts = {
'py2exe': {
'compressed': 1,
'optimize': 2,
'bundle_files': 1
}
}
setup(console=[entry_point], options=opts, zipfile=None)
I then open up cmd and try to use the compile.py file on myscript per the instructions but get an erro:
File "", line 1
python compile.py covid.pyw
^
You can use PyInstaller. Which is inbuilt, if not you can install it. Just very simple lines in cmd to create an exe file and also you can add icon using -i(I think so).
Visit this to learn about it from realpython which I love to read about python tutorials : https://realpython.com/pyinstaller-python/
By the way you can use python 3.8.6 or 3.9 which is the updated version of python 3.7.
Make sure your device is up to date, that could be the problem, also pyinstaller is a better, more modern alternative

VS code not loading Python Interpreter

Here is my VS code infor:
Version: 1.43.2 (user setup)
Commit: 0ba0ca52957102ca3527cf479571617f0de6ed50
Date: 2020-03-24T07:38:38.248Z
Electron: 7.1.11
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0
OS: Windows_NT x64 10.0.17763
My python path is here:
/auto/energymdl2/anaconda3/envs/commod_py3_20200921/bin/python
But VS codes keeps saying Select Python Interpreter in the status bar even after I selected.
But when I try to select, it says it is already being selected. At moment, due to this, I am unable to use Python linter Flake8.
When I check the console log. I got the following.
Error Python Extension: 2020-09-23 05:02:01: Failed to parse interpreter information for /auto/energymdl2/anaconda3/envs/commod_py3_20200921/bin/python,/home/test/.vscode-server/extensions/ms-python.python-2020.5.86806/pythonFiles/pyvsc-run-isolated.py,/home/test/.vscode-server/extensions/ms-python.python-2020.5.86806/pythonFiles/interpreterInfo.py stderr: Error in sitecustomize; set PYTHONVERBOSE for traceback:ModuleNotFoundError: No module named 'sdlc'
But when I go into Python and import sdlc. I am able to.
Python/iPython path:
/auto/energymdl2/anaconda3/envs/commod_py3_20200921/bin/python
Here is what I have in my setting.json
{
"python.linting.pycodestyleEnabled": false,
"python.linting.flake8Enabled": true,
"python.linting.enabled": true,
"python.linting.flake8Args": [
"--ignore=W605",
"--ignore=W503",
"--ignore=W605",
"--max-line-length=120"
],
"python.linting.banditEnabled": false,
"python.pythonPath": "/auto/energymdl2/anaconda3/envs/commod_py3_20200921/bin/python",
// "python.condaPath": "/auto/energymdl2/anaconda3/envs/commod_py3_20200727/bin/python"
}
It turned out that there is some logic goes into $your_conda_env_path/lib/python3.6/site-packages/sitecustomize.py that imports user-defined packages in working repo. But the working directory env variable was not set properly. I created a .env file under repo root dir and added relevant env variables. It resolved the issue.
So in fact all the conda envs were detected. It is just when it tries to load them, it hits ModuleNotFoundError.

How do I change pipenv/virtualenv location to work with Portable VS Code and project on thumb drive?

On Windows 10, I'm running VS Code 1.48.2 and Python 3, both installed on a thumb drive. I added an F:\Programs\VS Code\data folder with user-data andextensions. My project directory is F:\MyProject and it has a Pipfile. My project directory has a .env file and .venv\ so that pipenv will install to the local .venv\. What else do I need to get this environment working? Thanks!
.env file:
PYTHONPATH="F:\\Python\\Python38\\python.exe"
PYTHONHOME="F:\\MyProject\\.venv\\Scripts"
PIPENV_VENV_IN_PROJECT=True
F:\MyProject\.vscode\settings.json file:
{
"python.pythonPath": "f:\\MyProject\\.venv\\Scripts\\python.exe"
}
Although, when I used Preferences: Open Settings, it opened my F:\Programs\VS Code\data\user-data\settings.json file, though the paths look right (to me):
{
"editor.suggestSelection": "first",
"vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
"python.languageServer": "Microsoft",
"python.pythonPath": "${workspacefolder}/.venv/Scripts/python.exe",
"python.venvPath": "${workspacefolder}/.venv",
"python.venvFolders": [
".venv",
"${workspacefolder}/.venv"
]
}
Used the following steps to setup:
ps > cd F:\MyProject
ps > F:\Programs\VS Code\code.exe .
[In vscode integrated terminal]
PS F:\MyProject> py -m venv --system-site-packages .venv
PS F:\MyProject> pipenv shell
Loading .env environment variables…
Warning: Your Pipfile requires python_version 3.8, but you are using unknown (F:\M\.venv\S\python.exe).
$ pipenv --rm and rebuilding the virtual environment may resolve the issue.
$ pipenv check will surely fail.
Launching subshell in virtual environment…
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS F:\MyProject> ls .\.venv\Scripts\python.exe
Directory: F:\MyProject\.venv\Scripts
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 9/6/20 3:04 PM 532040 python.exe
PS F:\MyProject> python where
Python path configuration:
PYTHONHOME = 'F:\MyProject\.venv\Scripts'
PYTHONPATH = 'F:\Python\Python38\python.exe'
program name = 'F:\Python\Python38\python.exe'
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = 'F:\\Python\\Python38\\python.exe'
sys.base_prefix = 'F:\\MyProject\\.venv\\Scripts'
sys.base_exec_prefix = 'F:\\MyProject\\.venv\\Scripts'
sys.executable = 'F:\\MyProject\\.venv\\Scripts\\python.exe'
sys.prefix = 'F:\\MyProject\\.venv\\Scripts'
sys.exec_prefix = 'F:\\MyProject\\.venv\\Scripts'
sys.path = [
'F:\\Python\\Python38\\python.exe',
'F:\\Python\\Python38\\python38.zip',
'F:\\MyProject\\.venv\\Scripts\\DLLs',
'F:\\MyProject\\.venv\\Scripts\\lib',
'F:\\Python\\Python38',
]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
Current thread 0x000020d4 (most recent call first):
<no Python frame>
PS F:\MyProject>
According to your description, you could refer to the following steps to use the virtual environment:
Open this project in VSCode.
According to the information you provided, there is already a virtual environment ".venv" in your project, so there will be a ".venv" folder after opening. like this:
Click "select Python Interpreter" in the lower left corner to select the ".venv" virtual environment:
VSCode has selected the virtual environment, and then we open a new terminal console through the shortcut key "Ctrl+Shift+`", VSCode will automatically enter the current virtual environment:
Use pip to install the module "PySimpleGui": (pip install PySimpleGui)
When we run the python script, VSCode showed that the module PySimpleGui could not be found. We found "Lib\site-packages" in the ".venv" folder of the project and changed "PySimpleGUI" to "PySimpleGui":
The python script runs successfully in the virtual environment:

Categories