Prepare Python development environment for each project in Windows environment (VSCode + virtualEnvWrapper + Pylint)

Introduction

This article describes how to make multiple projects coexist in one development environment.

When working on multiple projects such as development and maintenance, multiple environments coexist. If you do not separate the environments, the development environment of one project may implicitly depend on the libraries prepared by another project. In order to prevent the problem that the code that worked in the development environment does not work in the execution environment due to the implicit dependence on this library, we will introduce a virtual environment (virtualEnvWrapper) that separates the library for each project.

In order to implement efficiently on a project with separate libraries, we will introduce linter (Pylint) that will point out syntax errors in real time. Register the execution operation of the implemented code as a task, and complete the implementation and operation check on Visual Stuide Code (VS Code).

Premise

This article assumes the following environment using Windows, and it is assumed that all of the following are installed.

Since linter is used in common in all development environments, install it in the Python environment of the system.

Preparing the virtual environment

Python virtual execution environment (virtualenvwrapper)

virtualEnvWrapper is a tool that can prepare the installation environment of the library used in Python in parallel. The image of the introduced virtual environment is in "VirtualEnv environments" of Virtualenv and virtualenvwrapper for Python. It is listed. Create multiple virtual projects in Python on the OS and install any library in them.

Before preparing the virtual environment, install Pylint which can be used regardless of the virtual environment.

command prompt


> pip install virtualenvwrapper-win
> pip install pylint

Creating a virtual execution environment

Next, prepare a project. Give the virtual environment name to <virtualEnvName> and create it.

If the creation is successful, the name of the virtual environment will be displayed at the prompt.

command prompt


>mkvirtualenv <virtualEnvName>
(<virtualEnvName>)>

The virtual environment is created in C: \\ Users \ <username> \ Envs \ A folder called <virtualEnvName> will be created.

If multiple environments are prepared, they will exist in parallel.

Pylint

Python settings

In VS Code, go to Basic Settings-> User Settings and specify pyLint for Linter.

python


{
...
	//-------- Python configuration --------

...
	// The linter to use
	"python.linter": "pyLint",
...
}

Pylint settings

Generate the pylint configuration file .pylintrc directly under the project in the folder where you want to create the project.

>pylint --generate-rcfile > .pylintrc

Specify the path where the library of the virtual environment exists in pylint. There is also an explanation in the help for the pylint command.

--init-hook=<code>  Python code to execute, usually for sys.path
                        manipulation such as pygtk.require().

As explained, write the Python code in ʻinit-hook`.

.pylintrc


init-hook="import sys; sys.path.append('module') ; sys.path.append('C:\\Users\<username>\\Envs\\<virtualEnvName>\\Lib\\site-packages')"

Enter the Windows account name in <username> and the virtual environment name mentioned above in <virtulEnvName>. The path is specified by sys.path.append ('C: \\ Users \ <username> \\ Envs \\ <virtualEnvName> \\ Lib \\ site-packages').

You can check if it works with the added settings with pylint <filename> .py. After pylint passes, start Visual Studio Code and check if a syntax error is pointed out.

VSCode

Set up a task

To minimize window switching during development Makes it possible to implement and execute everything with VS Code.

Here, the execution task is registered in VS Code. Please refer to the official Integrate with External Tools via Tasks for the description of the tasks.

Start tasks.json with Ctrl + Shift + p-> task runner configuration. The tasks to be executed in the virtual environment are described as follows.

tasks.json


{
	"version": "0.1.0",

	// The command is tsc. Assumes that tsc has been installed using npm install -g typescript
	"command": "cmd",

	// The command is a shell script
	"isShellCommand": true,

	// Show the output window only if unrecognized errors occur.
	"showOutput": "always",

	// args is the HelloWorld program to compile.
	"args": ["/c"],

	// use the standard tsc problem matcher to find compile problems
	// in the output.
	// "problemMatcher": "$tsc",
    "tasks":    [
        {
            "taskName"  :   "run",
            "suppressTaskName": true,
            "isBuildCommand":  true,
            "args"      :   ["workon <virtualEnvName> & python ${file}"]
        }
    ]
}

The intention of the setting is as follows.

--Start with the build command shortcut Ctrl + Shift + b --Start every time you run the command prompt with cmd / C --Concatenate commands executed by tasks with&and execute them on the same task. --Launch the virtualized environment <virtualEnvName> --Execute the Python code of the current file with $ {file}

Executing a task

Create the following sample program and execute it with Ctrl + Shift + b. If you can see "Hello world!" In the output field, you're done.

hello.py


print("Hello world!")

Development is now complete on VS Code.

bonus

Set key binding assignments

Select File-> Preferences-> Keyboard shortcuts. The left pane shows the default key bindings, and the right pane shows the user-configurable empty key bindings.

You can start the task by registering the following in the empty key binding.

[
    {
        "key": "ctrl+shift+r",
        "command": "workbench.action.tasks.runTask"
    }
]

To execute it, press ctrl + shift + r to start the command palette->" run ". "run" is the "taskName" registered in tasks.json.

Recommended Posts

Prepare Python development environment for each project in Windows environment (VSCode + virtualEnvWrapper + Pylint)
How to set the development environment for each project with VSCode + Python extension + Miniconda
Python project environment construction procedure (for windows)
virtualenvwrapper in windows environment
Development environment in Python
[For organizing] Python development environment
Check the operation of Python for .NET in each environment
Building a development environment for Android apps-creating Android apps in Python
Prepare Python development environment with Mac + Windows + VisualStudio Code (Windows version)
Prepare Python development environment with Mac + Windows + VisualStudio Code (Mac version)
Prepare Python development environment on Ubuntu
Prepare your first Python development environment
[Python3] Development environment construction << Windows edition >>
Python development environment options for May 2020
Emacs settings for Python development environment
Install Python development environment on Windows 10
Prepare Python development environment with Atom
Create a Python environment for professionals in VS Code on Windows
Create a Python execution environment for Windows with VScode + Remote WSL
Example of building python development environment on windows (wsl2, vscode, pipenv)
Python development environment for macOS using venv 2016
Using venv in Windows + Docker environment [Python]
How to prepare Python development environment [Mac]
Python3 + venv + VSCode + macOS development environment construction
VScode environment construction (Windows10, Python, C ++, C, Git)
Prepare the development environment for keyhac for Mac
Building a Python development environment for AI development
Python garbled in Windows + Git Bash environment
Windows + gVim + Poetry python development environment construction
Prepare a Python virtual environment for your project with venv with VS Code
Switch the module to be loaded for each execution environment in Python
[Python] Reason for dtype "int32" in Numpy (Windows environment) (as of September 2020)
Prepare the development environment for Python on AWS Cloud9 (pip install & time change)
Introduction to Python Let's prepare the development environment
Python environment construction (Anaconda + VSCode) @ Windows10 [January 2020 version]
python windows environment
Install Python 3.5.1 + numpy + scipy + α in Windows environment
Image Processing with Python Environment Setup for Windows
Build a local development environment with WSL + Docker Desktop for Windows + docker-lambda + Python
Create a Python virtual development environment on Windows
How to prepare an environment with different python version and package for each project with pyenv-virtualenv on Amazon Linux
Build a GVim-based Python development environment on Windows 10 (3) GVim8.0 & Python3.6
Build an interactive environment for machine learning in Python
Directory structure for test-driven development using pytest in python
Build a python environment for each directory with pyenv-virtualenv
Create a comfortable Python 3 (Anaconda) development environment on windows
Build a GVim-based Python development environment on Windows 10 (1) Installation
Procedure for building a CDK environment on Windows (Python)
Python development environment with Windows + Anaconda3 + Visual Studio Code
QGIS3 Python plugin development environment construction with VSCode (macOS)
Python development environment with Windows + Python + PipEnv + Visual Studio Code
AWS SDK for Python (Boto3) development in Visual Studio 2017
python memo (for myself): About the development environment virtualenv
Cross development environment (developing programs for windows on linux)
Whole type conversion for each dataframe column in python
python windows environment construction
Python development environment construction
About Python development environment
virtual environment in python
python2.7 development environment construction
Framework development in Python