macOS virtual machine builds Python and Go environments and writes HelloWorld programs


The virtual machine for this article is macOS Catalina 10.15.3.


foreword

This article will use Homebrew (package management tool) to install Python and Go. The installation of Homebrew must be accompanied by Command Line Tools (Xcode can install this tool). Since it is difficult for macOS virtual machines to directly obtain Command Line Tools and Xcode in the store, they need to be downloaded. Command Line Tools installation package to install. The applicable version for macOS Catalina 10.15.3 is Command Line Tools for Xcode 11.5.


Download Document

Command Line Tools for Xcode 11.5.dmg
Link: https://pan.baidu.com/s/1lBaYMD4Y2p11HpAWGS2O2w
Extraction code: vox0


1. Set up macOS virtual machine shared folder

You can download Command Line Tools directly in the virtual machine for installation, or you can download it on your computer and then upload it through the shared folder.


1. Shut down the virtual machine, go to Edit Virtual Machine Settings-Options, and open the shared folder settings.


2. Enter the wizard and set the shared folder path by yourself until it is completed.



3. Turn on the virtual machine, check the connected server in Finder-Preferences, and the shared folder will be displayed in VMware Shared Folders in the upper right corner of the screen.




2. Install Homebrew

1. Press the shortcut key Command+Space to open the spotlight search terminal and open it.
(Win keyboard and mac keyboard key correspondence: Win -> Command, Alt -> Option)


2. Enter the following command to install Homebrew using the domestic mirror source, during which a password is required.

zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

After the installation is successful, you can enter the following code to check the Homebrew version and confirm the successful installation.

brew -v


3. Build a Python environment and write a HelloWorld program

1. Enter the following command to view the version of python3 that can be installed.

brew search python3


2. Here, take the installation of python3.7 as an example, and directly enter the following command to install it.

brew install [email protected]

3. After the installation is successful, use the following command to view the installation directory of python3.7, and then configure environment variables and aliases.

brew list [email protected]

The output shows that python3.7 is installed in /usr/local/Cellar/[email protected]/3.7.13_1/bin.


To configure environment variables for the first time, use the following commands to create a configuration file and start editing the configuration file:

touch .bash_profile
vim .bash_profile 

Press i to enter the editing mode and enter the following three lines of instructions, which are environment variables, python3.7 aliases and pip3 aliases, and finally press ESC to exit editing and enter: wq to save and exit.

export PATH=${
    
    PATH}:/usr/local/Cellar/[email protected]/3.7.13_1/bin
alias python="/usr/local/Cellar/[email protected]/3.7.13_1/bin/python3.7"
alias pip="/usr/local/Cellar/[email protected]/3.7.13_1/bin/pip3"

Enter the following command to make the configuration take effect.

source ~/.bash_profile

The .bash_profile configuration file becomes invalid every time the system is restarted, and the above command needs to be used to make it take effect. In order to automatically enable .bash_profile to take effect after each system restart, it is also necessary to create a .zshrc configuration file in the user directory and add the above instructions in it. The method of adding is the same as above, and the system will automatically execute after restarting: source ~/.zshrc. (In the mac system, files starting with . are hidden files. Press Shift+Command+. in Finder to display/hide these files)


4. Write the HelloWorld program and output the result.


Use the following commands to create a directory for storing python files and enter the directory to create and edit py files.

mkdir -p ~/code/python/src
cd ~/code/python/src
touch HelloWorld.py
vim HelloWorld.py

Write a line of code in HelloWorld.py and save and exit.


Finally, enter the following command to output the result

python HelloWorld.py


3. Build the Go environment and write the HelloWorld program

1. Enter the following command to view the version of go that can be installed.

brew search go


2. Here choose go 1.17 to install, similar to installing python3.7, enter the following command.

brew install [email protected]

3. After successful installation, use the same command to view the installation directory of go.17, and then configure the environment variables.

brew list [email protected]

The output shows that go1.17 is installed in /usr/local/Cellar/[email protected]/1.17.11/libexec/bin.


4. First create the go project directory, which also contains three specified directories: src, pkg and bin, which store source files, package files and executable files respectively. (~ represents the user directory, which is equivalent to /Users/abc here, and abc is the current user name)

mkdir -p ~/code/go/src
mkdir ~/code/go/pkg
mkdir ~/code/go/bin

5. Edit .bash_profile and add the following three lines of instructions, which are the installation directory, project directory and environment variables. (Note to change abc to the username you set)

export GOROOT="/usr/local/Cellar/[email protected]/1.17.11/libexec"
export GOPATH="/Users/abc/code/go"
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

6. Write the HelloWorld program and output the result.


Enter the src directory to create and edit source files.

cd ~/code/go/src
touch HelloWorld.go
vim HelloWorld.go

Write the following code in HelloWorld.go. Since the tab key of the editor represents 8 spaces by default, it is not very beautiful to use the tab key to write code.


Then run the HelloWorld.go file directly to output the result.

go run HelloWorld.go


You can also build and then run, the executable file of HelloWorld will be generated in the bin directory.

go install HelloWorld.go


Double-click the executable file to output the result.


This concludes the tutorial

If there are mistakes or improvements, please actively point them out!

Guess you like

Origin blog.csdn.net/embracestar/article/details/125686228