Python3 + venv + VSCode + macOS development environment construction

Purpose

Build a development environment like the title and make it easier to migrate.

environment

Requirements

--Develop only with Python3 --I want to use VS Code --I want you to do IntelliSense, Linting, Formatting with VS Code --I want to save the list of packages installed with pip and rebuild it in another environment. --I don't want to pollute the global environment with global pip

tool

I made a tool that meets the requirements. https://gist.github.com/KoheiKanagu/b3ba2bb68d36509b6cee054e077bd415

$ python3 pset.py -h
usage: pset.py [-h] [--init] [-i] [-s] [-j] [-c]

This script deitals : http://qiita.com/KoheiKanagu/items/752c64aeeb154970d22e

optional arguments:
  -h, --help     show this help message and exit
  --init         Initialize Python virtual environment
  -i, --install  Install packages from "requirements.txt"
  -s, --sync     Sync packages using of pip-compile and pip-sync
  -c, --code     Open VSCode at virtual environment

Movement of each argument

--init

--Use python3 -m venv to create a virtual environment named **. env **. --Install pip-tools -If there is no ** requirements.in **, create it and write pip-tools.

--install

--install If there is ** requirements.txt ** in the same directory, install the packages with pip install -r requirements.txt.

--sync

-Pip-compile ** requirements.in ** and pip-sync

--code

--source .env / bin / activate && code ./ to start VS Code that inherited the virtual environment.

Remarks

Formatting All you have to do is install ** autopep8 ** or something in your global environment.

Like Linting, which will be described later, formatter does not need to look at packages in the virtual environment, so even if it is installed globally, it will format properly.

Linting For example, if you use ** pylint **, even if you install it globally, IntelliSense will not be possible because it will not see the packages installed in the virtual environment.

So, if you install ** pylint ** in the virtual environment, it will look at the packages in the virtual environment, so you can use IntelliSense.

Similarly, jupyter can only find packages if it is installed in a virtual environment.

VS Code status

With the tool, Start VS Code like source .env / bin / activate && code ./.

Therefore, as you can see by having VSCode do pip freeze etc. on the terminal, VSCode has taken over the virtual environment.

Therefore, when the program is executed and executed from the terminal, it is executed in the virtual environment.

You can also run it in a virtual environment using Code Runner.

Explorer view

Since **. env ** appears in the explorer on the left side of the VS Code screen, the contents will be caught in the search, and Linter will be squeezed for various files such as html and css inside, so as follows Add to files.exclude.

Then it will be ignored and will not be displayed in Explorer, and Linter will not get angry.

Of course there is no problem in the virtual environment

"files.exclude": {
    "**/.git": true,
    "**/.svn": true,
    "**/.hg": true,
    "**/CVS": true,
    "**/.DS_Store": true,
    "**/.env/**": true
}

Package installation

Do source .env / bin / activate and then pip install.

However, since we are assuming the use of pip-compile and pip-sync, it is easier to write the necessary packages in ** requirements.in ** and do python3 pset.py --sync.

Note that if you --sync with nothing written in ** requirements.in **, all installed packages will be deleted by the pip-sync function.

tips

Support for Python 2.7

I think it can be done by linking with ** pyenv ** etc., but I personally don't need 2.7, so I haven't tried it.

Precautions when changing the directory name

When you create a virtual environment in such a ~ / MyProject / .env / * directory name, if you change the name to ~ / MyProject2 / .env / *, it will be source ~ / MyProject2 /. Even if you do env / bin / activate, you cannot move to the virtual environment.

In that case, delete .env and reset it.

Recommended Posts

Python3 + venv + VSCode + macOS development environment construction
Python development environment construction on macOS
QGIS3 Python plugin development environment construction with VSCode (macOS)
Python development environment construction
python2.7 development environment construction
Python development environment for macOS using venv 2016
[MEMO] [Development environment construction] Python
[Python3] Development environment construction << Windows edition >>
Emacs Python development environment construction memo
Python environment construction
Environment construction (python)
python environment construction
Python --Environment construction
Python environment construction
python environment construction
Python (anaconda) development environment construction procedure (SpringToolsSuites) _2020.4
Where VSCode + venv environment construction got stuck
VScode environment construction (Windows10, Python, C ++, C, Git)
Python and machine learning environment construction (macOS)
Windows + gVim + Poetry python development environment construction
python windows environment construction
homebrew python environment construction
About Python development environment
Development environment in Python
Mac environment construction Python
Python environment construction @ Win7
Python environment construction (Anaconda + VSCode) @ Windows10 [January 2020 version]
Python + Anaconda + Pycharm environment construction
Install Python venv --VSCode --GitHub integration environment on Mac
Django development environment construction memo
Notes on creating a python development environment on macOS Catalina
CI environment construction ~ Python edition ~
Anaconda3 python environment construction procedure
Google App Engine / Python development environment construction procedure (late 2014)
Python3 environment construction (for beginners)
Organize your Python development environment
Python environment construction and TensorFlow
[ev3dev × Python] Build ev3dev development environment
Python development environment construction 2020 [From Python installation to poetry introduction]
[Python] Django environment construction (pyenv + pyenv-virtualenv + Anaconda) for macOS
venv: Python virtual environment management
Python environment construction under Windows7 environment
Build a Python development environment using pyenv on MacOS
[For organizing] Python development environment
django project development environment construction
python standard virtual environment venv
Environment construction of python2 & 3 (OSX)
[MEMO] [Development environment construction] wine
python3.8 venv environment jupyter notebook
Construction of Python local development environment Part 2 (pyenv-virtualenv, pip usage)
Memo of python + numpy/scipy/pandas/matplotlib/jupyterlab environment construction on M1 macOS (as of 2020/12/24)
Create a simple Python development environment with VSCode & Docker Desktop
VScode environment construction (on Mac) & graph display in Python (@browser)
Environment construction of python and opencv
Python environment construction memo on Windows 10
Get started with Python! ~ ① Environment construction ~
Prepare Python development environment on Ubuntu
Anaconda python environment construction on Windows 10
Python + Unity Reinforcement learning environment construction
I checked Mac Python environment construction
[For beginners] Django -Development environment construction-