Let's get along with Python # 0 (Environment construction)


This article is a re-edited version of what I posted to Qiita: Team with the goal of getting in-house infrastructure engineers to get along with programming through Python. My goal is to read O'Reilly's "Let Python do the boring things" so that you can understand the content. .. ~~, but the author hasn't read this book yet. ~~ Also, please note that the author dislikes mouse operation, so there are many biased thoughts.


For our convenience, we assume that everyone is using a Mac. I'm sorry for Windows. You can do something similar with Ubuntu on Windows. maybe.

Environmental preparation

First, let's prepare the environment for coding and executing Python.


The console is important for some programming. If you have a personal philosophy, skip this section. If you go to zsh or fish, it's out of the author's domain. ~~ Because it's a checkmate when the same shell cannot be used at the ssh destination in the first place. ~~

You can use the standard Mac terminal, but It's kind of convenient, so let's use iTerm2. Download and install from the download page. Download the .zip, unzip it and put it in your application folder.


(From now on, all Console``` Terminal ʻiTerm will be based on this ʻiTerm2.)

Put it in the application folder and start it. From here on, keyboard operation is basically prioritized over mouse operation. Launch spotlight with the shortcut (⌘ + space or Ctrl + space). ](Https://support.apple.com/ja-jp/guide/mac-help/mh26783/mac) When it starts, enter ʻiterm` to start iTerm2. If you are using Alfread, please start it nicely. I don't know because I don't use it. Also, at the beginning, the index may not be in time and you may not be able to start. In that case, give up and double-click.

It is convenient to do it first, so let's set the word movement. Here is also a shortcut. Focus on iTerm and press ⌘ +, to open Preference.

Profile> Keys> + click


Click to Set to focus, then press ʻalt (option) + → and select Send Escape Sequence from ʻAction from the select box below, and enter f in the ʻEscfield to set. .. Similarly, setSend Escape Sequence to bfor ʻalt + ←.

067edab3-f129-667a-d251-c2e4378e98b8.png fcbe49e6-f8d8-a90c-da97-369dd1d064fa.png

You can now move word by word on the CLI. You will find this convenience someday.

In addition to this, k-nishigaki only sets Log save. It's convenient by default, iTerm2. If you want to set more than this, check it yourself and set it. You can do it if you are greedy.

Editor (VS Code) setup

The editor is important. ** If you have other philosophies, please skip it. ** ** If you have iTerm2 installed, you can (probably) use Vim. You can use it normally in Terminal even if you don't put it in. But I won't talk about Vim here. I can't explain it because it's too deep, so I'll leave it to other fierce men. It will come out as much as you want.

If you are an Emacs sect, just go ahead with Emacs. As an aside, the author was originally Emacs, so when I type a little, I do something like Emacs. ~~ Ctrl + f, b, a, k, n are iron plates, aren't they? ~~

Quiet talk break.

The author referred to ↓ to learn more about Vim.

https://qiita.com/hachi8833/items/7beeee825c11f7437f54 https://qiita.com/nyantera/items/4bf29ca6f11bc797a9cb

That's it for Vim.

From here on, use Visual Studio Code. ](Https://azure.microsoft.com/ja-jp/products/visual-studio-code/) Download it for free now. Since it is a zip, the installation method is the same as iTerm2. It's easy to understand. After installing it, start it with spotlight as well. ** Mouse operation takes away your precious time every few seconds. ** ** ** The act of repeating every day, even for a few seconds, is a lot of loss if accumulated. ** ** If you repeat it a few times, just type vs and vs code will start.

By the way ~~ It is necessary to avoid trouble in the following explanation ~~ Set so that VS Code can be started from the console. When you open VS Code, press F1, and when you open the command palette, just type Shell. Then, I think that the command Shell Command: Install'code' command in PATH is selected. Don't worry, just click on it.


Then you will get a notification of a successful atmosphere. Get out.


It seems that the console is still running here, so in that case, focus the window on the console and press ⌘ + q once to exit. It's the spirit of restarting before you have trouble with something.

Now back to VS Code. If you inadvertently exit, start VS Code again. Of course, let's start it from the keyboard with spotlight.

Once started, click on the square on the left and enter python in the Search field. Then install the top Python. This is enough.


In addition, the condition of a good editor personally is

--Fast startup --Fast operation --A lot of plugins --And Plugin install is easy

is. If you walk through a few editors, you'll find your ultimate editor. The author is not a programmer, so ** I'm not particular about it at all. ** **

Setup of pyenv + virtual env

There are various sectarian conflicts (↓), but here we will proceed with pyenv + virtualenv. https://qiita.com/ganariya/items/1bf870275bad7b5ab506

** In addition, everything from here onward is my personal opinion, but it is very important personally. ** ** The most difficult point for programming beginners is to set up a programming language execution environment. In a little more detail, ** I tried to set it up, but the procedure loses reproducibility **. The nicest thing about the program is the ** reproducibility that you get the same results if you run it in the same environment . ( Extreme theory) Ultimately, the euphoria of ** where and in any environment you get the same results ** is important. ( It is my theory) For that purpose, prepare an environment that is ** very easy to set up ** and ** easy to reproduce on other than your own device **.

Well, everything is written here, so it is reprinted. https://qiita.com/niwak2/items/5490607be32202ce1314

pyenv install

On Mac, there is a handy tool called Homebrew ~~ which required sudo a while ago and was rather troublesome ~~. Convenient things should be used in a convenient way.

Open the console and enter ↓. The $ mark at the beginning of the line means "general user (non-root user)", but don't worry about it, just enter the word after it and press Enter. The character string will flow freely, but don't be afraid. They are all messages to you, "I'm doing this right now." ** Dialogue with the machine has already begun. ** **

$ brew install pyenv

Maybe you will get a different message than ↓.

$ brew install pyenv
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 4 taps (mackerelio/mackerel-agent, homebrew/cask-versions, homebrew/core and homebrew/cask).
==> New Formulae
(Omitted below)


The important thing is the message output towards the end. It is perfect if a message like "Sucess!" Is displayed there. That's a congratulations message from the machine. Let's receive it obediently.

virtualenv install

Let's install virutalenv as well as pyenv. The name of the package to install is pyenv-virutalenv. There seems to be no mistake.

$ brew install pyenv-virtualenv

Again, it's sloppy, but it's communication from the machine. Let's take it all.


Here, I advocate ** magic ** that is common in programming and environment construction. This isn't really a magic, it's an act necessary for dialogue with machines, but ** I'll give it up because it's about three times as much as this article **. sorry.

Vim is a hassle, so let's do it with VS Code. Open a console and type ↓ and press Enter. Maybe VS Code will start. I'm sorry if it doesn't start. Good luck and open ~ / .bash_profile.

Note that this is an important tip for shell operations, but you can complete the word after it (if possible) by pressing Tab while typing. Try typing code ~ / .bash_ and press Tab. You'll probably get code ~ / .bash_profile all at once. This is called "tab completion". Also, if there are multiple completion candidates, pressing Tab twice in a row will output all the candidates. ** What you should remember is how to use the command, not the full text. ** If you forget a detailed command, you can remember it with tab completion. Please remember that tabs are also useful for Google Japanese input. Let's hit it repeatedly.

Quiet talk break. Enter ↓ on the console.

$ code ~/.bash_profile

I think VS Code will start. Start it up. Perhaps the various described files will open, but ignore it and copy and paste ↓ to the empty space at the bottom of the file. ~~ This is a hassle, so you can copy it. ~~

## Set path for pyenv
export PYENV_ROOT="${HOME}/.pyenv"
if [ -d "${PYENV_ROOT}" ]; then
    export PATH=${PYENV_ROOT}/bin:$PATH
    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"

After writing, save it with ⌘ + s. The act of saving is also a keyboard shortcut. ** Mouse operation is taking your time. ** **

Hello, world!!

The first step in programming is Hello, world!

First "coding"

Now that you've come this far, you're ready to program in Python. As a final preparation, create a folder on your desktop with the name python_trial. This can also be done on the console. Open the console and type ↓.

$ mkdir ~/Desktop/python_trial

This will create a folder on your desktop named python_trial. (~ Represents home folder. It's okay if you don't understand, but if you're interested, link Please check ahead.)

When you're done, open VS Code and press ⌘ + n. ** Use the keyboard as much as possible. ** ** ** Mouse operation will hurt your precious time. ** **

You will see ʻUntitled-1` in a new tab. This is where your wonderful programming life begins. : tada:


First, it's often useful to make it clear that the file you're about to write is Python code, so let's save it all at once without having to write anything. Save is ⌘ + s. ** Recognize mouse operation as a waste of time. ** ** It doesn't matter what name you save, but here we'll use the convention as hello_world.py. Note that ** the important thing is the extension, not the file name. ** ** Editor basically determines files by extension. Save the .py extension without breaking it to indicate that this is a file that contains Python code. Save it to the desktop python_trial you created earlier. please.


After saving, just use VS Code to copy the ↓ code ** Never copy and pace ** Let's do everything by hand. This is the author's personal opinion, but ** hand-striking is much more efficient than copying. ** **

Note that as soon as you hit ( or something, () is automatically entered while you are hitting, you may move the cursor between (). This is a nice "complementary feature" of Editor. Engineers hate troublesome things. I don't even like hitting () in a row. It is a function that automatically inputs it. Don't feel bad, let's use it positively. Make friends with the machine.

print("hello world!")

When you finish typing, save it with ⌘ + s. ** Again, mouse operation is a waste of time. ** **

First "code execution"

After saving, open the console this time. You can open spotlight and type ʻiterm, but if you have iTerm2 running, you can also use ⌘ + Tab` to focus on iTerm2. ** Select with mouse? You are poisoned by the GUI! Let go of that mouse now! !! ** **

Enter ↓ at the console. Let's press Tab a lot. Complementation should work.

$ cd ~/Desktop/python_trial/

This is a command that means change the current directory to the folder~ / Desktop / python_trial /. Command is a technique that is inseparably related to programming. Let's use it without fear.

pyenv settings

** Now, here is where the first pyenv we prepared comes in! ** **

Execute the following command on the console.

$ pyenv install 3.6.9
(Probably a display like ↓ will be displayed)
python-build: use [email protected] from homebrew
python-build: use readline from homebrew
Downloading Python-3.6.9.tar.xz...
-> https://www.python.org/ftp/python/3.6.9/Python-3.6.9.tar.xz
Installing Python-3.6.9...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.6.9 to /Users/k-nishigaki/.pyenv/versions/3.6.9

This is the command to install python v.3.6.9 into a Python-wide environment management application called pyenv. (* The reason for 3.6.9 is that it was the latest at this point) It takes a lot of time. When you're done, run the following command.

$ pyenv virtualenv 3.6.9 python_trial

Then, python v.3.6.9 is set to pyenv with the name python_trial. Let's check.

$ pyenv versions | grep python_trial

It seems that it is set perfectly. But this is still not enough. Next, type the ↓ command.

$ pyenv local python_trial
(python_trial) hogehoge$

When you run it, you'll see the word (python_trial) at the beginning of the console. It may not be attached. If you don't have it, let's go as it is.

This is the true value of pyenv + virtualenv. This means that the current folder is under the control of the environment python v.3.6.9 / python_tiral. I'm sure you'll understand this usefulness later, but at this point, if you realize that you can use python v.3.6.9 in your current folder: + 1. Now let's make sure that python v.3.6.9 is working. Execute the ↓ command.

$ python --version
Python 3.6.9

You know that it's exactly Python 3.6.9. Now let's run the python script we just coded. Execute ↓ on the console.

$ python hello_world.py
hello, world!

It should have been displayed like this.


You might wonder, "Is this all you have prepared so far?" But now you have "a technology that lets you output the stringhello, world!To the console wherever Python 3.6.9 can run". This is a basic technology that is familiar to many programming. You have just acquired the great skills of programming and program execution. If you have come this far, you should be able to learn many programming skills in the future.

Next time preview

Next time I will deal with variables and functions.

Recommended Posts

Let's get along with Python # 0 (Environment construction)
Get started with Python! ~ ① Environment construction ~
Python3 environment construction with pyenv-virtualenv (CentOS 7.3)
pytorch @ python3.8 environment construction with pipenv
Python environment construction
Environment construction (python)
python environment construction
Python --Environment construction
Python environment construction
python environment construction
[Blender x Python] Let's get started with Blender Python !!
[Python] Get environment variables
homebrew python environment construction
Python development environment construction
Python environment with docker-compose
python2.7 development environment construction
Get date with python
Mac environment construction Python
Virtual environment with Python 3.6
Python environment construction @ Win7
Python hand play (let's get started with AtCoder?)
Poetry-virtualenv environment construction with python of centos-sclo-rh ~ Notes
First python ① Environment construction with pythonbrew & Hello World !!
From Python environment construction to virtual environment construction with anaconda
Get a quick Python development environment with Poetry
[Cloud102] # 1 Let's get started with Python (Part 2 Jupyter Notebook Construction AWS Edition)
Get started with Python in 30 minutes! Development environment construction & learn basic grammar
Get country code with python
Virtual environment construction with Docker + Flask (Python) + Jupyter notebook
Install Python environment with Anaconda
Get Twitter timeline with python
Manage python environment with virtualenv
Let's run Excel with Python
[Python] OpenCV environment construction with Docker (cv2.imshow () also works)
Get Youtube data with python
Python environment construction (Windows10 + Emacs)
CI environment construction ~ Python edition ~
[Cloud102] # 1 Let's get started with Python (Part 3 Jupyter Notebook Construction GCP Cloud Shell Edition)
Build python3 environment with ubuntu 16.04
ML environment construction with Miniconda
Python environment construction For Mac
Anaconda3 python environment construction procedure
Prepare python3 environment with Docker
Build python environment with direnv
Python3 environment construction (for beginners)
Python environment construction and TensorFlow
Web application made with Python3.4 + Django (Part.1 Environment construction)
Let's write python with cinema4d.
Get thread ID with python
QGIS3 Python plugin development environment construction with VSCode (macOS)
Python environment construction under Windows7 environment
[MEMO] [Development environment construction] Python
Get started with Python! ~ ② Grammar ~
Get stock price with Python
Let's build git-cat with Python
Environment construction of python2 & 3 (OSX)
Get Alembic information with Python
OpenJTalk on Windows10 (Speak Japanese with Python from environment construction)
[LDAP environment construction: 7] Add / search / change / delete users with Python
Get a clean Python development environment with pyenv + pipx + Poetry
Environment construction of python and opencv