Set up Python Environment on Mac and Windows在Mac和Windows下设置Python环境

Set up Python on Mac and Windows

1. File and Author Information

/*

  • File: python setup.md
  • Project: setup environment
  • File Created: Thursday, 2nd March 2023 5:41:13 pm
  • Author: Hanlin Gu (hg_fine_codes@163.com)

  • Last Modified: Thursday, 2nd March 2023 10:19:19 pm
  • Modified By: Hanlin Gu (hg_fine_codes@163.com>)
    */

2. Python Versions

2.1. Mac

In Mac computers, by default there would be python 2.7 installed already with
the computer. To check if python is pre-installed, open terminal and key in

python --version

Output

python 2.7.10

First, one would like to download the latest version of python. Current, python 3.x is generally used.

Python Download Page

https://www.python.org/getit/

After the installation, in the Application folder, one is able to find the folder of the installed python, for example python 3.10.

python3 --version

Output

python 3.10.0

If one use python --version, it will still show python 2.7.10.

2.1.1. Let python linked to python3 in terminal using alias
nano ~/.bash_profile

There are a few lines added about python at the bottom of the file. And input
the following line

alias python=python3

Hit ctrl + x to exit, Y to confirm change and then hit enter to keep the
same file name.

If one check the python --version, they will get python 3.10.0 instead.

2.2. Windows

During the installation, check the box Add Python 3.10 to PATH to allow the python command to work with the command prompt. Similarly, one can check the version of python by python --version in cmd or Windows Powershell etc.

3. How to use Python

3.1. Interactive Prompt

In Mac one can use terminal or iTerm2 and Windows user can choose cmd or Windows Powershell or Anaconda Powershell Prompt.

python

Interactive prompt allows to write one line of code at a time. It’s suitable for testing python command rather than writing python scripts.

3.2. IDLE - Integrated Development Learning Environment

By default, the IDLE interface is also only allow for one line at a time as the three arrows >>> in the front indicts.

If one want to write a script, click File and New File to create a blank script window.

3.3. Common IDE

  • Sublime Text
  • Atom
  • PyCharm
  • VS Code

Either of the IDE works great. The author uses VS Code since it’s also easy to write markdown, LaTex file with it.

3.4. Run a Python script

In the prompt,

python file_address/file_name.py

Directly use python to open the file_name.py script file at the file_address folder.

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oNtNfRJI-1678462131463)(./python%20scripts.png)]

3.5. Add comments in Python

Add # sign at the front of the line of the comments, the program will not run the comments part.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值