Hello, this is koki1992. This time, we will build the Python3 execution environment on a fully initialized Mac. Occasionally, beginners may be confused by the fact that the installation procedure is slightly different or the screen is slightly different depending on the OS version. Since the environment was built on the latest OS at the moment, I think that new students who are trying to challenge programming by buying a personal computer this year can challenge the environment construction without any confusion while reading this article. I will.
OS:Mojave 10.14.6 PC:MacBookAir 11-inch Early 2015
Perform the following operations while connected to the Internet. I look forward to working with you.
Work in the following order.
First, let's start the terminal. It is very convenient to use "Spotlight Search" when searching for applications and files. Try using a shortcut key. Press the command key and the space key at hand at the same time. Since my PC is a US keyboard, the arrangement is slightly different, but I prepared a photo.
Then, the following screen will appear. Type "Terminal" here and select the terminal.
Depending on the default settings, you should see a terminal screen with a black or white background.
Homebrew is a package management system for macOS. If you put this in, you can easily install software and packages. Let's do it right away.
First, click Safari and search for "brew".
Then you will see the Homebrew site at the top. Let's click this.
Once you've viewed the site, copy the script that exists below the "Install" section in the center of the page.
Paste this into the terminal. Then press the "Enter key".
On the screen that worked, I think the characters started running on the terminal. When I started the program, I was excited just by looking at this screen (laughs).
You may be asked to enter the password or press the enter key (RETURN key) on the way. Follow the letters in the terminal.
The installation will take some time.
.. .. .. Well, is it over?
There are 2 and 3 systems in Python. 3 is newer, but Python originally included in Mac is 2 series. Therefore, I would like to install 3 series.
Homebrew will do the job right here. To start Homebrew, enter "brew Honyara". In this case, from the terminal screen
brew install python3
Let's enter. Then, the characters will start running on the terminal as before. As before, I will obediently follow the occasional requests that appear on the terminal (such as pressing enter or entering a password) and wait for it to be fully installed.
.. .. .. Is it over?
On the terminal screen
python
I'll just type. Then python is started, but you will notice something strange. Isn't this the screen?
Python 2.7.16 (default, Nov 9 2019, 05:55:08)
[GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.32.4) (-macos10.15-objc-s on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
For some reason Python is 2.7.16. This means that Python, which was originally installed on your Mac, has been launched. This is a problem, so let's start Python 3 series. But first you have to get out of this weird screen (called interactive mode).
Calm down and type "exit ()".
Python 2.7.16 (default, Nov 9 2019, 05:55:08)
[GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.32.4) (-macos10.15-objc-s on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
You can return to the original screen by pressing the enter key. Then on the same screen
python3
Let's enter. It looks like this on my screen.
Python 3.7.7 (default, Mar 10 2020, 15:43:03)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
This time, I think I was able to start the version of Python 3.7.7.
Press "exit ()" again on the previous screen to return to the original.
That's fine, but let's build a Python 3 virtual environment.
You may think that it is troublesome to create a virtual environment, but there are various merits in building a virtual environment.
First, you can switch the version of the package or module during development for each virtual environment. In the process of enjoying programming from now on, you may read various codes on github etc. and try running them on your own PC. However, different programs may have different recommended package or module versions. It doesn't work unless it's an old version of the package or module, or it needs a new version of the package or module. Only one package or module with the same name can exist in one environment, and different versions of packages or modules cannot coexist. (Although Python was also entered as python3 when calling the 3rd system of python, it can be said that this is also a problem of the same system). In order to run a program that uses a different version of a package or module in the same environment, it may be necessary to reinstall the version of the package or module that matches the program each time, which is very difficult. It is a tedious and wasteful task. If you create a virtual environment for each program you want to run, the packages and modules installed in that virtual environment will be used, so if you switch the virtual environment for each program you want to run, you will be freed from the troublesome work described above. Probably. It also makes sense in this respect, because even if you make a fatal mistake in a virtual environment, you just have to discard the virtual environment itself and recreate another new virtual environment. If you build the environment directly in the original environment, this may not work. At first, I was building a python3 environment in the normal environment, but I had a hard time. We recommend that you build a virtual environment quietly!
I'm sorry, I derailed a little (; ´∀ `)
Let's build the environment. That said, you don't have to type very difficult commands. This time, I will return to my home directory (I will not explain the meaning of this word, so please search if you understand it). Probably everyone is in your home directory, but on the terminal,
cd
Just type and press enter. cd is an abbreviation for change directory.
Now that I've definitely returned to my home directory, I'd like to build a virtual environment here. This time we will create a virtual environment with the name py3venv. If you don't like it, you can give it a different name.
Enter the following and press the enter key.
python3 -m venv py3venv
Environment construction is completed in a few seconds to a dozen seconds. We will start the virtual environment from here. Enter the following: The point is to put a period at the beginning of the sentence and put a half-width space between them.
koki1992noMacBook-Air:~ koki1992$ . py3venv/bin/activate #Here's the point!
(py3venv) koki1992noMacBook-Air:~ koki1992$
Then, in my environment (py3venv) koki1992noMacBook-Air: ~ koki1992 $. In the previous environment, there was no (py3venv) part, right? If you can confirm this part in parentheses, you can say that the virtual environment construction was successful. Congratulations!
From the terminal
python
Enter. It is not "python3". Then
(py3venv) koki1992noMacBook-Air:~ koki1992$ python
Python 3.7.7 (default, Mar 10 2020, 15:43:03)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
The 2nd system Python was started earlier, but this time the 3rd system was started properly. In the original environment, there are two types of python, 2 series and 3 series, and they were separated by attaching the version name to "python3" as a painstaking measure. That's not necessary in a virtual environment. This is because in a virtual environment, "python" refers only to the 3rd system of python.
For example, suppose a school has "Taro Tanaka" and "Jiro Tanaka". If you call "Tanaka" at all school meetings, they will respond to the call. Therefore, at that school, we decided to call "Jiro Tanaka" "Jiro Tanaka" and "Taro Tanaka" just "Tanaka" at all school meetings. Is it like this? .. ..
Finally, let's close the virtual environment. First close the interactive mode with exit (), then type:
(py3venv) koki1992noMacBook-Air:~ koki1992$ deactivate
koki1992noMacBook-Air:~ koki1992$
(Py3venv) at the beginning of the previous line disappeared, and it returned to the state before starting the virtual environment. Thank you for your hard work. This concludes the explanation of the Python3 execution environment. If you have any mistakes or advice, I would appreciate it if you could comment. Thank you for watching until the end \ (^ o ^) /
Recommended Posts