VS Code settings for developing in Python with completion

Introduction

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

Enable Pyenv on Mac

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

Put plugins in VS Code and use completion

The required plugins are as follows. Please install from the installation palette of VS Code extensions.

Settings for each plugin

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.

  1. In VS Code, open the command palette with Command + Shift + P (macOS) / Control + Shift + P (Windows)
  2. Enter Python: Select Interpreter to select
  3. Select the environment of pyenv to use (Sandbox in this example)

Screen Shot 2020-11-07 at 19.11.18.png

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.

Screen Shot 2020-11-07 at 19.47.15.png

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.

Screen Shot 2020-11-07 at 20.04.49.png

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"
  }
}

Recommended plugins that make Python development explosive

Python Docstring Generator

Screen Shot 2020-11-07 at 20.15.37.png

finally

PyCharm 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

VS Code settings for developing in Python with completion
Settings when developing App Engine / Python apps with VS Code
Use Python in Anaconda environment with VS Code
Settings for Python coding in Visual Studio Code
Settings for getting started with MongoDB in python
Python with VS Code (Windows 10)
Debug Python with VS Code
Allow real-time code checking in Python development with VS Code
Revive symbol search in Python workspace with VS Code
Expose settings.json for efficient Python coding in VS Code
Install python with mac vs code
Insert Import statements needed for Python code completion in Neovim
Tips for developing apps with Azure Cosmos DB in Python
Create a Python environment for professionals in VS Code on Windows
Tips for dealing with binaries in Python
VS Code settings
(For myself) Put Flask in VS Code
Pass PYTHONPATH in 1 minute with VS Code
Process multiple lists with for in Python
Debug settings in virtual environment when using Pipenv with VS Code
Prepare a Python virtual environment for your project with venv with VS Code
Python (Windows 10) Virtual Environment / Package with VS Code
Debug with VS Code using boost python numpy
Logging settings for daily log rotation in python
[Introduction for beginners] Working with MySQL in Python
[Visual Studio Code] [Python] Tasks.json + problemMatcher settings for Python
Make your Python environment "easy" with VS Code
Build a python execution environment with VS Code
Tips for speeding up python code correctly with numba
Enable intellisense for external libraries with Pipenv + VS Code
Python code for k-means method in super simple case
Environment construction for those who want to study python easily with VS Code (for Mac)
Get country code with python
Scraping with selenium in Python
Working with LibreOffice in Python
Scraping with chromedriver in python
Debugging with pdb in Python
Write Spigot in VS Code
Search for strings in Python
Working with sounds in Python
Python code memo for yourself
Scraping with Selenium in Python
Techniques for sorting in Python
Scraping with Tor in Python
Tweet with image in Python
Combined with permutations in Python
Generate QR code in Python
[Python] Sample code for Python grammar
Character code learned in Python
About "for _ in range ():" in python
Document Python code with Doxygen
Let's run jupyter natively supported by VS Code with python3.8
Try Fortran with VS Code up to debug settings. [Win10]
Create a child account for connect with Stripe in Python
[For beginners] Summary of standard input in Python (with explanation)
(For myself) Flask_8 (Add / Edit / Delete in database with python)
Settings to debug the contents of the library with VS Code
The story that Python stopped working with VS Code (Windows 10)
Comfortable LaTeX with Windows Subsystem for Linux and VS Code
UpNext2 Development Record # 1 Build Python CI environment in VS Code
I want to be able to run Python in VS Code