"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.
Download and install each from the official website.
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
Remote --Containers Install
.1.
folder: Dockerfile and docker-compose.yamlAutopep8
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/
build
and up -d
.docker-compose.yaml
version: "3.8"
services:
python3.9:
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/root/
working_dir: /root/
tty: true
(* 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
.
Select the icon like the image below at the bottom left
<img width="400" height=auto" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/499855/9754337c-e7b3-b782-f2ed-2fdd86a7c0df.png ">
<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 with the extended function AREPL
that you have introduced.
.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 ">
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.
It will be neatly arranged with option + shift + F
.
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 ">
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