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).
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.
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
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
In VS Code, go to Basic Settings-> User
Settings and specify pyLint
for Linter.
python
{
...
//-------- Python configuration --------
...
// The linter to use
"python.linter": "pyLint",
...
}
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
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}
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.
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
.