This article is for building a development environment for "Python + Pipenv + VS Code".
It's written by an engineer with a year of experience in Python, so you may not be able to understand the culture of Python, but please be aware of that.
Package management is required when managing source code as a team. Creating the same operating environment reduces communication costs.
Such a natural thing. .. I think that.
Package management is commonplace for me because I've only done app development, but Python isn't limited to app development and is also used for data science and small task automation, so there's a need for package management. I noticed that the perception of is different for each developer.
Also, I think that the fact that the standard package manager does not exist in the Python world (I think) also causes the image of package management to vary from developer to developer.
In the case of javascript, there are npm and yarn, and I think that it is a culture of javascript to start writing code assuming package management. On the other hand, Python has pip as a well-known package manager, but to me who has used npm, pip feels very poor.
I recently introduced pipenv to my team and found it to work, so I'll write how to build a Python project environment using pipenv.
If you want a base project with pipenv configuration right away, you can find it on github.
If you have already installed Pipenv on global pip, you can immediately try from cloning the above repository to running unit tests. The installation method is as follows.
git clone https://github.com/michiharu/PythonProjectStarterKit.git
cd PythonProjectStarterKit
pipenv install --dev --pre
pipenv run test
Vanilla vs Anaconda
It seems that the standard installer Python is called "Vanilla Python". We will assume that Python will be installed using the Official Page Installer.
Anaconda is convenient, but my opinion is that it doesn't make much sense of the execution environment. If you are an app developer, you need to understand the minimum configuration of the Python execution environment.
It seems good to know Vanilla for application development and Anaconda for data science. The following articles were very helpful for building a Python environment.
Python environment construction various --Windows
When reading this article and rebuilding the Python environment, it is recommended that you output the library you have been using to a text file.
pip freeze > requirememts.txt
From the Python official page, select "Files> Windows x86-64 web-based installer" and select Download the installer.
When you run the installer, there is a checkbox "Do you want to add Path to python in your system environment variables?" Be sure to check before installing.
Open a command prompt and do the following:
python --version
The procedure for using pipenv is explained below.
You can use it with the following command.
pip install pipenv
When the installation is complete, hit the command pipenv
to see a list of commands that can be executed with pipenv.
--user
option to install with user privileges. I see an article that you can use it by passing Path to the user environment variable with an option, but even if you try several times, if you install with the --user
option, pass Path to the system environment variable Without it, you couldn't use the pipenv command. I understand that pip install pipenv is also installed with user privileges, as Python itself is installed with user privileges by default. In other words, I'm guessing that the --user
option is a valid option if you have Python installed for system-wide use. If you have the correct knowledge, please comment.The default behavior of pipenv is to create a virtual environment resource in .virtualenvs
in the USER directory, separate from the project directory.
This is inconvenient because it cannot be accessed immediately when checking the Lib of the virtual environment, so enable the setting to create a virtual environment in the project in the system environment variable settings.
PIPENV_VENV_IN_PROJECT=true
The above is the environment construction in preparation for creating a project.
If you set .vscode
, VS Code may display the dialog" Do you want to install? ". We do not recommend installing from this dialog.
The reason is that the Pipfile will not be updated because the automatically executed scripts will be installed globally using pip instead of pipenv unless you use the package addition method described below. Install Linter and Formatter with pipenv.
settings.json
fileIn order to share Linter and Formatter settings with vscode, the following settings are required in .vscode/settings.json
.
{
"python.venvPath": ".venv",
"python.pythonPath": ".venv\\Scripts\\python.exe",
"python.envFile": "${workspaceFolder}/.env",
"python.linting.enabled": true,
"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": true,
"python.linting.mypyEnabled": true,
"python.formatting.provider": "black",
"python.linting.flake8Args": ["--ignore=E501,W503"],
"editor.formatOnSave": true
}
The following are possible with the above settings.
.env
fileIn order to add environment variables when executing python, write the following in the .env file. The location is directly under the project.
PYTHONPATH=".\src;"
PYTHONPATH adds a place to look for modules when running python. You can write multiple items separated by semicolons.
Formatter selected black. black himself claims to be "Black is the uncompromising Python code formatter." According to
The settings have been thoroughly discussed about the format, so just use it. Let's stop worrying about the format anymore? It saves time and, more importantly, puts energy into it.
... apparently ...
Therefore, if there is a rule conflict with flake8, black will be given priority. That is, let flake8 ignore the conflicting rules.
python.linting.flake8Args
is set to ignore some rules of flake8 to avoid rule conflict with black.
For example, --ignore = E501
is a setting to ignore the flake8 rule of up to 79 characters per line (black is up to 88 characters per line). I think there were some other rules that conflicted with black other than the one set as a sample here, but please add it each time you get a flake8 error related to the format.
Execute the following command. The Python version can be set more finely. If the corresponding version of Python is not installed, an error will occur. In that case, please install from the standard installer.
pipenv --python 3
When you execute the command, the following files will be added to the project directory.
pipenv install [package]
pipenv install --dev [package]
pipenv install --dev mypy flake8 black isort --pre
--dev
is an option to manage as a development package. --pre
is an option that allows the installation of pre-released packages. black is described on the official page as "NOTE: This is a beta product", and if there is no pre option, an error will occur indicating that the update of Pipfile.lock failed.
If the cloned project has Pipfile
, generate .venv
with the following command and install the required modules there.
//Pipfile[packages]Add the package described in
pipenv install
//Pipfile[dev-packages]Add the package described in
pipenv install --dev --pre
You can install the package from requirements.txt
using the argument r.
pipenv install -r ./requirements.txt
I think it's a culture regardless of language to put the source code in the src
directory.
The directory structure under src is as follows.
src
├── package
│ ├── foo_module.py
│ ├── :
│ └── xxx.py
└── tests
├── __init__.py
├── test_foo_module.py
├── :
└── test_xxx.py
To import foo_module.py from test_foo_modules.py, write as follows.
import package.foo_module
The test can be run with the following command:
//For batch execution
pipenv run python -m unittest discover src -v
If you do not place __init__.py
in the tests directory, an error will occur because the test code cannot be found from unittest with the above command.
In addition, frequently used commands can be registered as scripts. Add the following items to the Pipfile.
[scripts]
test = "python -m unittest discover tests -v"
isort = "python -m isort -m 3 ."
With this setting, you can test with the following command.
pipenv run test
The second added isot is a formatter specializing in imports. The black introduced so far does not touch import, so the format of import is left to iceplant.
pipenv run isort
That's all for the procedure for building the environment for a Python project.
Recommended Posts