Environment construction for those who want to study python easily with VS Code (for Mac)

Introduction

"Now, let's study Python" When I started investigating to start by thinking about creating an environment, "Homebrew ..." "Check Path ..." "Default version ..." Am I the only one who feels that there are many branches that try to break the hearts of beginners?

I think there are various ways, but I simply create an environment using Docker and Visual Studio Code (VSCode) extensions. I felt that it would be easy and hassle-free, so I will keep a record of it.

If you have more recommendations, I would appreciate it if you could comment.

Installation

Download and install each from the official website.

Docker Desktop

Visual Studio Code

Docker is a service that allows you to create virtual machines. You need to create an account when using this for the first time.

This article is very easy to understand for installing VS Code. Procedure to install Visual Studio Code on MacOS

Put extensions in VS Code

  1. Open VS Code.
  2. From the extension Remote --Containers Install .

Prepare directories and files

  1. Create a new folder
  2. Create a folder named .devcontainer
  3. Create a file called devcontainer.json in it
  4. Create two files in the 1. folder: Dockerfile and docker-compose.yaml

Write various configuration files

  1. Dockerfile The python version is specified as 3.9. Autopep8 for auto-formatting source code in VS Code, Also include the code analysis tool pylint.

Dockerfile


FROM python:3.9
USER root

RUN apt-get update && apt-get -y install locales git wget unzip vim && \
    localedef -f UTF-8 -i ja_JP ja_JP.UTF-8
ENV LANG ja_JP.UTF-8
ENV LANGUAGE ja_JP:ja
ENV LC_ALL ja_JP.UTF-8
ENV TZ JST-9
ENV TERM xterm

ARG DEBIAN_FRONTEND=noninteractive

RUN pip install -U pip && \
    pip install -U autopep8 && \
    pip install -U pylint

VOLUME /root/

  1. docker-compose.yaml Later VS Code will look at this file and do build and up -d.

docker-compose.yaml


version: "3.8"
services: 
  python3.9:
    build: 
      context: .
      dockerfile: Dockerfile
    volumes:
      - .:/root/
    working_dir: /root/
    tty: true
  1. devcontainer.json You can use the environment that can be done above without this file, You need this file to easily get into your environment from VS Code.

(* Please put this file in the .devcontainer folder) (You can switch the hidden file display from the Finder with command + shift +. (Period))

devcontainer.json


{
	"name": "Python3.9",

	"dockerComposeFile": [
		"../docker-compose.yml"
	],

	"service": "python3.9",

	"workspaceFolder": "/root/",

	"settings": { 
		"terminal.integrated.shell.linux": null
	},

	"extensions": [
		"ms-ceintl.vscode-language-pack-ja",
		"ms-python.python",
		"oderwat.indent-rainbow",
		"almenon.arepl"
	]
}

The following extensions are automatically installed in extensions.

Create a virtual environment from VS Code

1. Open Remote

Select the icon like the image below at the bottom left

2. Select "Open folder in Container"

<img width="400" height=auto" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/499855/9754337c-e7b3-b782-f2ed-2fdd86a7c0df.png ">

3. Select the folder you created above

4. Virtual machine preparation begins

<img width="400" height=auto" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/499855/6bedfa82-c6f9-8091-c968-22aca232c62c.png "> If you click "show log", you can see that commands such as docker-compose up -d are actually executed, so if you want to see what is happening behind the scenes, you should see it.

When the environment is built successfully, the screen below will be displayed. (I also include themes and icon extensions, so the appearance may differ.) <img width="600" height=auto" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/499855/dfa97777-6c0c-390d-758c-d789bdfaf8c6.png ">

Check if Python works

Check with the extended function AREPL that you have introduced.

1. Create a .py file and start AREPL

Here I created a file named test.py. After making it, click the cat-like icon as shown in the image. <img width="400" height=auto" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/499855/e63da46e-bc25-b28a-ba8f-288d99ff7ff7.png ">

2. Write the source code

Anything is fine, so let's write the source code. You can see that variables and print statements are displayed in real time while writing.

I'm trying to print a variable that I haven't intentionally defined in the image. It is in a state where it is underlined in the editor for the time being, but The AREPL screen already tells me not defined. <img width="600" height=auto" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/499855/cedb9629-f3a9-2cf5-f5d6-c3402e667c60.png ">

As a caveat, AREPL is not suitable for very heavy processing. For example, if the range of the above for statement exceeds 10000, the reflection will be delayed considerably, as a matter of course. Even if you reduce the number of ranges in that state, it will not be reflected until the previous execution is completed.

Recommended for those who want to see the processing contents as soon as possible with a really simple and light program, If not, you might not want to use AREPL.

3. Try using automatic shaping

It will be neatly arranged with option + shift + F.

4. Try running the program

You can run the program with F5. However, it is also troublesome to select "Python File" every time you are asked the type of debugging as shown below.

<img width="500" height=auto" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/499855/dd546f3b-2868-c981-3d3b-018a188cb8a7.png ">

So, in a hidden folder called .vscode, create a debug configuration file called launch.json. Select "Create launch.json file" from the display below and select "Python File", and the file will be created automatically. <img width="400" height=auto" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/499855/f4bd64b5-4eaf-5c60-e573-9ffc3cf0d3ee.png ">

Just press F5 this time The execution result is displayed on the console screen at the bottom. <img width="400" height=auto" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/499855/527ffb9d-0322-10d4-045b-dc9ecafecb89.png ">

in conclusion

VS Code has other useful debugging features that I can't cover. No matter what you install or break in your virtual environment It is attractive to be able to easily reproduce the same environment without affecting the actual environment. If you are new to creating a Python environment and are reading this, please give it a try.

Recommended Posts

Environment construction for those who want to study python easily with VS Code (for Mac)
Python environment construction 2016 for those who aim to be data scientists
[Short sentence] easygui for those who want to use a simple GUI with Python very easily
For those who want to learn Excel VBA and get started with Python
The first step of machine learning ~ For those who want to implement with python ~
For those who want to start machine learning with TensorFlow2
Reference reference for those who want to code in Rhinoceros / Grasshopper
Install python with mac vs code
Environment construction procedure for those who are not familiar with the python version control system
Python techniques for those who want to get rid of beginners
How to build Python and Jupyter execution environment with VS Code
I was addicted to creating a Python venv environment with VS Code
Steps to create a Python virtual environment with VS Code on Windows
Python (Windows 10) Virtual Environment / Package with VS Code
Use Python in Anaconda environment with VS Code
From Python environment construction to virtual environment construction with anaconda
Make your Python environment "easy" with VS Code
Build a python execution environment with VS Code
I want to create a nice Python development environment for my new Mac
Join Azure Using Go ~ For those who want to start and know Azure with Go ~
Python local development environment construction template [Flask / Django / Jupyter with Docker + VS Code]
Mac environment construction Python
VS Code settings for developing in Python with completion
Anxible points for those who want to introduce Ansible
I want to AWS Lambda with Python on Mac!
For those who are new to programming but have decided to analyze data with Python
I tried to build an environment for machine learning with Python (Mac OS X)
Python with VS Code (Windows 10)
Debug Python with VS Code
Python3 environment construction (for beginners)
Ansible environment construction For Mac
Prepare Python development environment with Mac + Windows + VisualStudio Code (Windows version)
Prepare Python development environment with Mac + Windows + VisualStudio Code (Mac version)
I want to be able to run Python in VS Code
Build a Python environment with WSL + Pyenv + Jupyter + VS Code
Execute C ++ functions from Python with Pybind11 (for Windows & Visual Studio Code people) Environment construction
For those who want to display images side by side as soon as possible with Python's matplotlib
I want to set up a GUI development environment with Python or Golang on Mac
Create a Python environment for professionals in VS Code on Windows
PyPI registration steps for those who want to make a PyPI debut
Get started with Python! ~ ① Environment construction ~
[Windows 10] [Development environment construction] How to resolve when unresolved import'django.xxx' python (unresolved-import) occurs in VS Code
I checked Mac Python environment construction
Python environment construction memo on Mac
Try to create a python environment with Visual Studio Code & WSL
Environment construction of python3.8 on mac
Loose articles for those who want to start natural language processing
Python3 environment construction with pyenv-virtualenv (CentOS 7.3)
Easily post to twitter with Python 3
pytorch @ python3.8 environment construction with pipenv
I want to debug with Python
A memo for those who want quick socket communication with netcat
Create a simple Python development environment with VS Code and Docker
[2020 version for beginners] Recommended study method for those who want to become an AI engineer by themselves
Build jupyter notebook environment with Visual Studio Code (VS Code) Mac version
How to debug a Python program by remotely connecting to a Docker container in WSL2 environment with VS Code
Build an environment to execute C ++ functions from Python with Pybind11 (for Windows & Visual Studio Code people)
Things to watch out for when creating a Python environment on a Mac
I analyzed Airbnb data for those who want to stay in Amsterdam
Environment maintenance made with Docker (I want to post-process GrADS in Python
I tried to build a Mac Python development environment with pythonz + direnv