I'm using VSCode, but I didn't know which plugin (extension) to set to set up the Python development environment.
When I put in my own package (module), I got angry with " Can't Resolve ~ "
...
But lately, I've come to understand which plugin to use and how to configure it.
I hope it helps people who are having trouble building a Python development environment like me in the past.
--Premise
--Good if you have written Python even a little
--What is Type Hint? You can set it even if you don't know it
--Installing VS Code
--using zsh
--How to check: echo $ SHELL
→ / bin / zsh
is displayed
--Purpose / Goal
--Python code can be executed on Mac
--VSCode allows you to set up plugins for Python
--Warning by name resolution, method candidates, and Type Hint
--Target person
--Mac, Python beginner
--People who suddenly have to develop with Python
--People who want to develop using the complementary function
--Sample code
- BotamochiRice/dev_python_on_vscode
Let's set up the Python environment. Install Anyenv, Pyenv and Virtualenv.
This keeps the standard Python environment on your Mac clean.
(** It seems that Pipenv is getting a lot of attention in the streets **. I will try it next time)
terminal
$ brew install anyenv
$ anyenv install pyenv
$ echo 'eval "$(anyenv init -)"' >> ~/.zshrc
$ anyenv install --init #This is PYENV_It passes through the environment variables of ROOT and PATH
$ echo $PYENV_ROOT
~/.anyenv/envs/pyenv
$ brew install pyenv-virtualenv
Let's try creating a virtual environment for Python.
terminal
$ pyenv install 3.8.6 # 3.8.6 Python installed
$ pyenv virtualenv 3.8.6 Sandbox # 3.8.Create a virtual environment called Sandbox based on 6
$ mkdir -p ~/Documents/playground/python && cd "$_" #Create a sample directory and move to it
$ pyenv local Sandbox #This directory uses Python in the Sandbox environment
$ pyenv version #Check the virtual environment you are currently using
Sandbox
The required plugins are as follows. Please install from the installation palette of VS Code extensions.
ms-python.python
--Providing Python Language Server
--Basic complementary plugins provided by Microsoft. This alone is weakmagicstack.magicpython
--Syntax highlighting pluginvisualstudioexptteam.vscodeintellicode
--Complementary plug-in with AI engine provided by Microsoftms-pyright.pyright
--A big favorite. Plugins that can fully benefit from Type Hint
--Perform variable type inference and warn if it is different from Type Hintformulahendry.code-runner
--You can execute the open file from the triangle mark on the upper right.Mainly sets PYTHONPATH. By telling the plug-in the package (package) you made, you will be able to fully demonstrate the power of the complementary function.
In this project, the folder structure is as follows.
Also, in this article, we will develop in a virtual environment called Sandbox
, which was used in the installation method of pyenv.
Python: Select Interpreter
to selectSandbox
in this example)Python Language Server
First of all, we will introduce flake8 as Linter (static code check tool; it will check if it is based on code conventions etc.).
terminal
pip install flake8
Next, write the settings to .vscode/settings.json
.
The important thing here is the item python.envFile
and the .env
file. Tell the Python Language Server where the "root directory of the Python program" is.
$ {workspaceFolder}
indicates the directory open in VS Code.
json:.vscode/settings.json
{
"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": true,
"python.linting.enabled": true,
"python.linting.lintOnSave": true,
"python.envFile": "${workspaceFolder}/.env"
}
Write the actual root directory in .env
.
(* When app / was named src /, I was reading my own package without setting PYTHONPATH. Does VS Code put src / in PATH by default?)
.env
PYTHONPATH=app/
Settings for running Python with a debug tool. You can execute using breakpoints and watches as shown in the screenshot below, so be sure to set it.
json:.vscode/launch.json
{
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Python: Main",
"type": "python",
"request": "launch",
"program": "${workspaceRoot}/app/main.py",
"console": "integratedTerminal"
}
]
}
Pyright
Type inference and static analysis are powerful. In the image below, Pyright warns you that the type of the first argument is incorrect. Python Language Server alone does not display this warning.
Set the root directory of the program for name resolution of your own package.
pyrightcofig.json
{
"executionEnvironments": [
{
"root": "app",
"pythonVersion": "3.8"
}
]
}
Code Runner
It runs the currently open file with one click or Control + Option + N (macOS). Since the execution time is also displayed, it seems that it can be used for program optimization.
Make the setting "App directory is the root directory of Python execution ( $ workspaceRoot / app
), and execute it in the virtual environment of Sandbox ( $ pythonPath
)".
json:.vscode/settings.json
{
"code-runner.executorMap": {
"python": "export PYTHONPATH=$workspaceRoot/app && $pythonPath -B -u $fullFileName"
}
}
njpwerner.autodocstring
--Generates document formats for classes and functions like JavaDoc
--Reads Type HintPyCharm and Wing are famous for Python IDEs, but VS Code also has extensions. I think that it can be a powerful IDE if you write config firmly.
(It's annoying to have to write PYTHONPATH everywhere ...)
Microsoft has released a plugin called Pylance in beta, and I hope that development with VS Code will be more convenient. I am.
Also, I wrote it in the spirit of * "Done is better than perfect." *, So please point out any parts that are difficult to understand. Also, if you have any other recommended settings, I would appreciate it if you could let me know in the comments.
Thank you for reading. If you find it useful, ** LGTM ** will be encouraging.
Recommended Posts