[Mac] Create a Python3 execution environment from the fully initialized state

Introduction

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.

Execution environment

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.

Outline of procedure

Work in the following order.

1. Start the terminal.

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. image.png

Then, the following screen will appear. Type "Terminal" here and select the terminal.

image.png

Depending on the default settings, you should see a terminal screen with a black or white background.

image.png

2. Install Homebrew

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".

image.png

Then you will see the Homebrew site at the top. Let's click this.

image.png

Once you've viewed the site, copy the script that exists below the "Install" section in the center of the page.

image.png

Paste this into the terminal. Then press the "Enter key".

image.png

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.

image.png

The installation will take some time.

.. .. .. Well, is it over?

3. 3. Install Python 3 using Homebrew

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?

Make sure it is installed properly.

On the terminal screen

python

I'll just type. Then python is started, but you will notice something strange. Isn't this the screen?

image.png

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.

4. Build a Python 3 virtual environment

Press "exit ()" again on the previous screen to return to the original.

That's fine, but let's build a Python 3 virtual environment.

[Question] Does it make sense to bother to build a 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 (; ´∀ `)

Resuming virtual environment construction

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.

image.png

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

[Mac] Create a Python3 execution environment from the fully initialized state
Create a Python environment on Mac (2017/4)
Create a python environment on your Mac
Create a Python environment
Create a Python execution environment on IBM i
Create a Python (pyenv / virtualenv) development environment on Mac (Homebrew)
Create a virtual environment for python on mac [Very easy]
Building a Python environment on Mac
Create a virtual environment with Python!
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Let's create a virtual environment for Python
[Python] Create a virtual environment with Anaconda
[Python] Create a Batch environment using AWS-CDK
[Mac] Building a virtual environment for Python
Create a Python development environment locally at the fastest speed (for beginners)
[Mac] Build a Python 3.x environment at the fastest speed using Docker
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
Edit Excel from Python to create a PivotTable
Prepare the execution environment of Python3 with Docker
How to create a Python virtual environment (venv)
Create a C array from a Python> Excel sheet
Simply build a Python 3 execution environment on Windows
ffmpeg-Build a python environment and split the video
Build a Python environment on Mac (Mountain Lion)
Create a New Todoist Task from Python Script
Create a python3 build environment with Sublime Text3
Build a Python development environment on your Mac
Think about building a Python 3 environment in a Mac environment
[Python] Create an asynchronous task execution environment + monitoring environment
[Venv] Create a python virtual environment on Ubuntu
Create a decision tree from 0 with Python (1. Overview)
Create a datetime object from a string in Python (Python 3.3)
Create a Mac app using py2app and Python3! !!
[Docker] Create a jupyterLab (python) environment in 3 minutes!
Create a Python virtual development environment on Windows
[Note] How to create a Mac development environment
Build a python execution environment with VS Code
Create a Python development environment in 10 minutes (Mac OS X + Visual Studio Code)
I want to create a nice Python development environment for my new Mac
Until a Python inexperienced engineer builds a TensorFlow execution environment + comfortable coding environment on Mac
Create a Django project and application in a Python virtual environment and start the server
Create a Python module
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 1 ~
Build a machine learning Python environment on Mac OS
Create a comfortable Python 3 (Anaconda) development environment on windows
Create a python development environment with vagrant + ansible + fabric
Python points from the perspective of a C programmer
Build a Python development environment on Mac OS X
Build a Python environment on your Mac using pyenv
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 2 ~
Python script to create a JSON file from a CSV file
Building a Python environment for programming beginners (Mac OS)
Mac environment construction Python
[It's not too late to learn Python from 2020] Part 2 Let's create a Python development environment
Create a Python development environment on OS X Lion
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 3 ~
How to create a kubernetes pod from python code
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 4 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 5 ~
A little bit from Python using the Jenkins API
Create a machine learning environment from scratch with Winsows 10