iTranslated by AI
[Beginners] Set Up a Python Environment in 5 Minutes
Create a Python environment in 5 minutes.
We will build an environment similar to what professional data scientists use, starting from scratch.
[Prerequisite Knowledge] What is Homebrew?
It is a system that allows you to easily install various software on macOS.
[Prerequisite Knowledge] What is Docker?
It is software that allows you to easily create environments necessary for programming.
[Prerequisite Knowledge] What is Anaconda?
It is a collection of many libraries used for data science.
[Prerequisite Knowledge] What is Jupyter Lab?
It is an environment where you can write and execute Python code in your browser. Data scientists and AI engineers use it when writing Python code. Using the same environment as the pros makes you feel like a pro yourself.
*This article is intended for Mac users.
Overall Flow

Step 1: Install Homebrew
Step 2: Install Docker
Step 3: Create files and folders
Step 4: Create a Dockerfile
Step 5: Run Docker
Step 6: Play with Python using Jupyter Lab
Step 1: Installing Homebrew

First, install Homebrew.
We are installing Homebrew in order to install Docker later. If it's already installed, you can skip this.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
By running the above command in your terminal, Homebrew will be installed on your Mac. This allows you to use commands like brew ... in your terminal. Since Homebrew is used quite frequently, it's a good idea to have it installed.
[Supplementary Info] How to check if Homebrew is already installed
brew -v
Run the above command; if the version is displayed, it is already installed. Let's move on.
Step 2: Installing Docker

You can install Docker using the Homebrew you just installed.
brew install --cask docker
By running the above command, Docker will be installed on your Mac. This allows you to use commands like docker ... in your terminal. This process takes a significant amount of time.
Step 3: Create Files and Folders

Run the commands below. This will create a working folder, and inside it, one file and one folder. You can copy and paste the commands.
mkdir my-folder
cd my-folder
mkdir my-py-folder
touch Dockerfile

Commands and the resulting directory structure
Ultimately, the structure will look like the one shown above.
Step 4: Create a Dockerfile
Open the Dockerfile you just created and write the contents as follows. Imagine that you are creating a blueprint for your Python environment in the Dockerfile.
FROM ubuntu:latest
RUN apt-get update && apt-get install -y \
sudo \
wget \
WORKDIR /opt
RUN wget https://repo.continuum.io/archive/Anaconda3-2019.10-Linux-x86_64.sh && \
sh /opt/Anaconda3-2019.10-Linux-x86_64.sh -b -p /opt/anaconda3 && \
rm -f Anaconda3-2019.10-Linux-x86_64.sh
ENV PATH /opt/anaconda3/bin:$PATH
RUN pip install --upgrade pip
WORKDIR /
CMD ["jupyter", "lab", "--ip=0.0.0.0", "--allow-root", "--LabApp.token=''"]
Step 5: Run Docker

Creating a Docker image from a Dockerfile and then creating a Docker container
Create a Docker image based on the Dockerfile you created.
Run the following command in your terminal. This process takes a significant amount of time.
docker build .
A screen that looks like it's building various things continues for a while.
Finally, if "Successfully built (IMAGE ID)" is displayed, the build was successful.
Create a container from the image you created.
By running the following command, a container will be created, and the Jupyter Lab environment (Python environment) will be ready.
docker run -p 8888:8888 -v ~/Desktop/my-folder/my-py-folder:/work (Write IMAGE ID here)
Text indicating the startup is displayed on the screen.
The Jupyter Lab environment is now ready for use.
If you don't know the IMAGE ID
Run the following command to display a list of created images.
Please copy the IMAGE ID you just created.
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> faf24e1523f3 4 weeks ago 3.41GB
Step 6: Play with Python using Jupyter Lab

Overview
Access http://localhost:8888 in your browser.
You should see a screen like the one shown below.

Jupyter Lab home screen
The directory structure is displayed on the left side of the screen (the orange area). This reflects the directory structure of the Ubuntu OS inside Docker. It is not linked to your Mac's directory structure, so you won't see your desktop or other local folders.
At the bottom of the directory structure, there is a folder named "work" (the blue area). This is the folder you created.
This "work" folder is the only part connected to your Mac. It looks something like the image below.

Image of directory structure and mounting
The "work" folder is linked to the "my-py-folder" you created earlier. Linking folders like this is called "mounting". Think of "my-py-folder" as being identical to "work". Therefore, if you create a new file in the "work" folder within Jupyter Lab, it will also appear in "my-py-folder".

The work directory is linked to my-py-folder
Check it in Finder. You should see that the file has been created.
You can execute Python code in Jupyter Lab. Feel free to write code as shown below.

You can write Python code
That concludes the method for creating an environment like those used by professional data scientists, starting from scratch.
Since the installation takes some time, "creating it in 5 minutes" might have been an exaggeration. My apologies. Let's call it 5 minutes of actual hands-on work time.
I've summarized the basics of Python in a book. I'd be very happy if you'd check it out as well ↓ I've summarized the basics of Deep Learning in a book. I'd be very pleased if you'd take a look ↓
Learn more about Machine Learning
NekoAllergy AI
I share content about machine learning on YouTube. I'd be glad if you could take a look if you have the time.
Created by NekoAllergy

Discussion