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.
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, set
Send Escape Sequence to
bfor ʻalt + ←
.
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.
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. ** **
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)
(Omitted)
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 -)"
fi
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!
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. ** **
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.
** 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
3.6.9/envs/python_trial
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 I will deal with variables and functions.
Recommended Posts