如何在macOS上安装Python 3和设置本地编程环境

本教程详细介绍了如何在macOS上安装Python 3及设置编程环境。首先,你需要一台连接到互联网的macOS计算机。通过终端安装Xcode和Homebrew,接着使用Homebrew安装Python 3。安装完成后,利用Python 3创建虚拟环境,以确保项目之间的依赖隔离。最后,创建一个简单的示例程序验证环境是否正常工作。这样,你便具备了一个完整的Python 3编程环境。
摘要由CSDN通过智能技术生成

介绍 (Introduction)

Python is a versatile programming language that can be used for many different programming projects. First published in 1991 with a name inspired by the British comedy group Monty Python, the development team wanted to make Python a language that was fun to use. Easy to set up, and written in a relatively straightforward style with immediate feedback on errors, Python is a great choice for beginners and experienced developers alike. Python 3 is the most current version of the language and is considered to be the future of Python.

Python是一种通用的编程语言,可用于许多不同的编程项目。 开发团队于1991年首次发布,其名称受到英国喜剧团体Monty Python的启发,开发团队希望使Python成为一种有趣的语言。 Python易于设置,并且以相对简单的风格编写,并且可以立即对错误进行反馈,对于初学者和经验丰富的开发人员而言,Python是一个不错的选择。 Python 3是该语言的最新版本,被认为是Python的未来。

This tutorial will guide you through installing Python 3 on your local macOS machine and setting up a programming environment via the command line.

本教程将指导您在本地macOS机器上安装Python 3,并通过命令行设置编程环境。

先决条件 (Prerequisites)

You will need a macOS computer with administrative access that is connected to the internet.

您将需要一台具有管理访问权限的macOS计算机,该计算机已连接到Internet。

第1步-打开终端 (Step 1 — Opening Terminal)

We’ll be completing most of our installation and set up on the command line, which is a non-graphical way to interact with your computer. That is, instead of clicking on buttons, you’ll be typing in text and receiving feedback from your computer through text as well. The command line, also known as a shell, can help you modify and automate many of the tasks you do on a computer every day, and is an essential tool for software developers.

我们将完成大部分安装并在命令行上进行设置,这是与计算机交互的非图形方式。 也就是说,您将不用输入按钮,而是输入文本,并通过文本接收来自计算机的反馈。 命令行(也称为外壳程序)可以帮助您修改和自动化每天在计算机上执行的许多任务,并且是软件开发人员的必备工具。

The macOS Terminal is an application you can use to access the command line interface. Like any other application, you can find it by going into Finder, navigating to the Applications folder, and then into the Utilities folder. From here, double-click the Terminal like any other application to open it up. Alternatively, you can use Spotlight by holding down the command and spacebar keys to find Terminal by typing it out in the box that appears.

macOS终端是可用于访问命令行界面的应用程序。 与任何其他应用程序一样,您可以通过以下方法找到它:进入Finder,导航到Applications文件夹,然后进入Utilities文件夹。 从此处,像其他任何应用程序一样,双击终端以将其打开。 或者,您可以通过按住commandspacebar键来使用Spotlight,方法是在出现的框中将其键入,以找到Terminal。

There are many more Terminal commands to learn that can enable you to do more powerful things. The article “An Introduction to the Linux Terminal” can get you better oriented with the Linux Terminal, which is similar to the macOS Terminal.

还有更多的终端命令需要学习,可以使您执行更强大的功能。 文章“ Linux终端简介 ”可以使您更好地了解与MacOS终端类似的Linux终端。

第2步-安装Xcode (Step 2 — Installing Xcode)

Xcode is an integrated development environment (IDE) that is comprised of software development tools for macOS. You may have Xcode installed already. To check, in your Terminal window, type:

Xcode是一个集成开发环境(IDE),由用于macOS的软件开发工具组成。 您可能已经安装了Xcode。 要检查,请在“终端”窗口中键入:

  • xcode-select -p

    xcode选择-p

If you receive the following output, then Xcode is installed:

如果收到以下输出,则说明已安装Xcode:


   
   
Output
/Library/Developer/CommandLineTools

If you received an error, then in your web browser install Xcode from the App Store and accept the default options.

如果收到错误,请在Web浏览器中从App Store安装Xcode并接受默认选项。

Once Xcode is installed, return to your Terminal window. Next, you’ll need to install Xcode’s separate Command Line Tools app, which you can do by typing:

Xcode安装完成后,返回到“终端”窗口。 接下来,您需要安装Xcode的单独的命令行工具应用程序,您可以通过键入以下命令进行安装:

  • xcode-select --install

    xcode-select-安装

At this point, Xcode and its Command Line Tools app are fully installed, and we are ready to install the package manager Homebrew.

至此,Xcode及其命令行工具应用程序已完全安装,我们准备安装软件包管理器Homebrew。

步骤3 —安装和设置Homebrew (Step 3 — Installing and Setting Up Homebrew)

While the OS X Terminal has a lot of the functionality of Linux Terminals and other Unix systems, it does not ship with a good package manager. A package manager is a collection of software tools that work to automate installation processes that include initial software installation, upgrading and configuring of software, and removing software as needed. They keep installations in a central location and can maintain all software packages on the system in formats that are commonly used. Homebrew provides OS X with a free and open source software package managing system that simplifies the installation of software on OS X.

尽管OS X Terminal具有Linux Terminals和其他Unix系统的许多功能,但它并没有附带好的软件包管理器。 软件包管理器是软件工具的集合,这些工具可用于自动化安装过程,包括初始软件安装,软件的升级和配置以及根据需要删除软件。 它们将安装保持在中央位置,并可以以常用格式维护系统上的所有软件包。 Homebrew为OS X提供了免费的开源软件包管理系统,从而简化了OS X上软件的安装。

To install Homebrew, type this into your Terminal window:

要安装Homebrew,请在“终端”窗口中输入以下内容:

  • /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

    / usr / bin / ruby​​ -e“ $(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”

Homebrew is made with Ruby, so it will be modifying your computer’s Ruby path. The curl command pulls a script from the specified URL. This script will explain what it will do and then pauses the process to prompt you to confirm. This provides you with a lot of feedback on what the script is going to be doing to your system and gives you the opportunity to verify the process.

Homebrew是用Ruby制作的,因此它将修改您计算机的Ruby路径。 curl命令从指定的URL中提取脚本。 该脚本将说明它将执行的操作,然后暂停该过程以提示您进行确认。 这为您提供了有关脚本将对系统执行的操作的大量反馈,并为您提供了验证过程的机会。

If you need to enter your password note that your keystrokes will not display in the Terminal window but they will be recorded, simply press the return key once you’ve entered your password. Otherwise press the letter y for “yes” whenever you are prompted to confirm the installation.

如果您需要输入密码,请注意,您的击键不会显示在“终端”窗口中,但会被记录下来,只需在输入密码后按return键即可。 否则,每当系统提示您确认安装时,将字母y表示“是”。

Let’s walk through the flags that are associated with the curl command:

让我们看一下与curl命令关联的标志:

  • The -f or --fail flag tells the Terminal window to give no HTML document output on server errors.

    f--fail标志告诉终端窗口在服务器错误时不输出HTML文档。

  • The -s or --silent flag mutes curl so that it does not show the progress meter, and combined with the -S or --show-error flag it will ensure that curl shows an error message if it fails.

    -s--silent标志使curl静音,以使其不显示进度表,并与-S--show-error标志结合使用,将确保curl在失败时显示错误消息。

  • The -L or --location flag will tell curl to redo the request to a new place if the server reports that the requested page has moved to a different location.

    如果服务器报告所请求的页面已移至其他位置,则-L--location标志将告知curl将请求重做至新位置。

Once the installation process is complete, we’ll put the Homebrew directory at the top of the PATH environment variable. This will ensure that Homebrew installations will be called over the tools that Mac OS X may select automatically that could run counter to the development environment we’re creating.

安装过程完成后,我们将Homebrew目录放在PATH环境变量的顶部。 这样可以确保通过Mac OS X可能会自动选择的工具调用Homebrew安装,这些工具可能会与我们正在创建的开发环境背道而驰。

You should create or open the ~/.bash_profile file with the command-line text editor nano using the nano command:

您应该使用nano命令,使用命令行文本编辑器nano创建或打开~/.bash_profile文件:

  • nano ~/.bash_profile

    纳米〜/ .bash_profile

Once the file opens up in the Terminal window, write the following:

在“终端”窗口中打开文件后,编写以下内容:

export PATH=/usr/local/bin:$PATH

To save your changes, hold down the control key and the letter o, and when prompted press the return key. Now you can exit nano by holding the control key and the letter x.

要保存更改,请按住control键和字母o ,然后在出现提示时按return键。 现在,您可以按住control键和字母x退出nano。

For these changes to activate, in the Terminal window, type:

要激活这些更改,请在“终端”窗口中键入:

  • source ~/.bash_profile

    来源〜/ .bash_profile

Once you have done this, the changes you have made to the PATH environment variable will be effective.

完成此操作后,对PATH环境变量所做的更改将生效。

We can make sure that Homebrew was successfully installed by typing:

我们可以通过键入以下命令来确保成功安装了Homebrew:

  • brew doctor

    酿造医生

If no updates are required at this time, the Terminal output will read:

如果此时不需要更新,则终端输出将显示:


   
   
Output
Your system is ready to brew.

Otherwise, you may get a warning to run another command such as brew update to ensure that your installation of Homebrew is up to date.

否则,您可能会收到警告,请运行其他命令(例如brew update以确保您安装的Homebrew是最新的。

Once Homebrew is ready, you can install Python 3.

准备好Homebrew之后,即可安装Python 3。

第4步-安装Python 3 (Step 4 — Installing Python 3)

You can use Homebrew to search for everything you can install with the brew search command, but to provide us with a shorter list, let’s instead search for just the available Python-related packages or modules:

您可以使用Homebrew来搜索可以通过brew search命令安装的所有内容,但是为了向我们提供更短的列表,我们只搜索可用的与Python相关的软件包或模块:

  • brew search python

    酿造搜索python

The Terminal will output a list of what you can install, like this:

终端将输出您可以安装的内容的列表,如下所示:


   
   
Output
app-engine-python micropython python3 boost-python python wxpython gst-python python-markdown zpython homebrew/apache/mod_python homebrew/versions/gst-python010 homebrew/python/python-dbus Caskroom/cask/kk7ds-python-runtime homebrew/python/vpython Caskroom/cask/mysql-connector-python

Python 3 will be among the items on the list. Let’s go ahead and install it:

Python 3将在列表中。 让我们继续安装它:

  • brew install python3

    brew安装python3

The Terminal window will give you feedback regarding the installation process of Python 3, it may take a few minutes before installation is complete.

“终端”窗口将为您提供有关Python 3安装过程的反馈,安装完成可能需要几分钟。

Along with Python 3, Homebrew will install pip, setuptools and wheel.

与Python 3一起,Homebrew将安装pipsetuptoolswheel

A tool for use with Python, we will use pip to install and manage programming packages we may want to use in our development projects. You can install Python packages by typing:

这是与Python一起使用的工具,我们将使用pip来安装和管理我们可能要在开发项目中使用的编程包。 您可以通过输入以下命令安装Python软件包:

  • pip3 install package_name

    pip3安装package_name

Here, package_name can refer to any Python package or library, such as Django for web development or NumPy for scientific computing. So if you would like to install NumPy, you can do so with the command pip3 install numpy.

在这里, package_name可以引用任何Python包或库,例如用于Web开发的Django或用于科学计算的NumPy。 因此,如果您想安装NumPy,则可以使用命令pip3 install numpy

setuptools facilitates packaging Python projects, and wheel is a built-package format for Python that can speed up your software production by reducing the number of times you need to compile.

setuptools有助于打包Python项目,而wheel是Python的内置打包格式,可通过减少编译次数来加快软件生产。

To check the version of Python 3 that you installed, you can type:

要检查已安装的Python 3的版本,可以输入:

  • python3 --version

    python3-版本

This will output the specific version of Python that is currently installed, which will by default be the most up-to-date stable version of Python 3 that is available.

这将输出当前安装的特定版本的Python,默认情况下它将是可用的Python 3的最新稳定版本。

To update your version of Python 3, you can first update Homebrew and then update Python:

要更新您的Python 3版本,您可以先更新Homebrew,然后再更新Python:

  • brew update

    酿造更新
  • brew upgrade python3

    酿造升级python3

It is good practice to ensure that your version of Python is up-to-date.

优良作法是确保您的Python版本是最新的。

第5步—创建虚拟环境 (Step 5 — Creating a Virtual Environment)

Now that we have Xcode, Homebrew, and Python installed, we can go on to create our programming environment.

现在我们已经安装了Xcode,Homebrew和Python,我们可以继续创建我们的编程环境。

Virtual environments enable you to have an isolated space on your computer for Python projects, ensuring that each of your projects can have its own set of dependencies that won’t disrupt any of your other projects.

虚拟环境使您可以在计算机上拥有一个用于Python项目的隔离空间,从而确保每个项目都可以拥有自己的一组依赖关系,而这些依赖关系不会破坏任何其他项目。

Setting up a programming environment provides us with greater control over our Python projects and over how different versions of packages are handled. This is especially important when working with third-party packages.

设置编程环境使我们可以更好地控制Python项目以及如何处理不同版本的软件包。 在使用第三方软件包时,这一点尤其重要。

You can set up as many Python programming environments as you would like. Each environment is basically a directory or folder in your computer that has a few scripts in it to make it act as an environment.

您可以根据需要设置任意数量的Python编程环境。 每种环境基本上都是计算机中的目录或文件夹,其中包含一些脚本以使其充当环境。

Choose which directory you would like to put your Python programming environments in, or create a new directory with mkdir, as in:

选择您要放置Python编程环境的目录,或使用mkdir创建一个新目录,如下所示:

  • mkdir Environments

    mkdir 环境

  • cd Environments

    cd 环境

Once you are in the directory where you would like the environments to live, you can create an environment by running the following command:

一旦进入您希望环境存在的目录,就可以通过运行以下命令来创建环境:

  • python3.7 -m venv my_env

    python3.7 -m venv my_env

Essentially, this command creates a new directory (in this case called my_env) that contains a few items:

本质上,此命令创建一个包含以下项目的新目录(在本例中为my_env ):

  • The pyvenv.cfg file points to the Python installation that you used to run the command.

    pyvenv.cfg文件指向您用于运行命令的Python安装。

  • The lib subdirectory contains a copy of the Python version and has a site-packages subdirectory inside it that starts out empty but will eventually hold the relevant third-party modules that you install.

    lib子目录包含Python版本的副本,并且其中包含一个site-packages子目录,该目录开始为空,但最终将保留您安装的相关第三方模块。

  • The include subdirectory compiles packages.

    include子目录可编译软件包。

  • The bin subdirectory has a copy of the Python binary along with the activate shell script that is used to set up the environment.

    bin子目录包含Python二进制文件的副本以及用于设置环境的Activate Shell脚本。

Together, these files work to make sure that your projects are isolated from the broader context of your local machine, so that system files and project files don’t mix. This is good practice for version control and to ensure that each of your projects has access to the particular packages that it needs.

这些文件一起工作,以确保您的项目与本地计算机的更广泛的上下文隔离开来,从而避免系统文件和项目文件混在一起。 这是进行版本控制并确保您的每个项目都可以访问所需的特定程序包的良好做法。

To use this environment, you need to activate it, which you can do by typing the following command that calls the activate script:

要使用此环境,您需要激活它,您可以通过键入以下调用激活脚本的命令来激活它:

  • source my_env/bin/activate

    源my_env / bin / activate

Your prompt will now be prefixed with the name of your environment, in this case it is called my_env:

现在,您的提示将以您的环境名称为前缀,在这种情况下,它称为my_env :

This prefix lets us know that the environment my_env is currently active, meaning that when we create programs here they will use only this particular environment’s settings and packages.

该前缀使我们知道环境my_env当前处于活动状态,这意味着在此处创建程序时,它们将仅使用此特定环境的设置和程序包。

Note: Within the virtual environment, you can use the command python instead of python3, and pip instead of pip3 if you would prefer. If you use Python 3 on your machine outside of an environment, you’ll need to use the python3 and pip3 commands exclusively, as python and pip will call an earlier version of Python.

注意:在虚拟环境中,可以根据需要使用命令python代替python3 ,并使用pip代替pip3 。 如果您在环境之外的计算机上使用Python 3,则需要专门使用python3pip3命令,因为pythonpip将调用Python的早期版本。

After following these steps, your virtual environment is ready to use.

完成这些步骤后,即可使用虚拟环境。

第6步-创建示例程序 (Step 6 — Creating a Sample Program)

Now that we have our virtual environment set up, let’s create a traditional “Hello, World!” program. This will make sure that our environment is working and gives us the opportunity to become more familiar with Python if we aren’t already.

现在我们已经建立了虚拟环境,让我们创建一个传统的“ Hello,World!”。 程序。 这将确保我们的环境能够正常工作,并且使我们有机会熟悉Python(如果尚未熟悉)。

To do this, we’ll open up a command-line text editor such as nano and create a new file:

为此,我们将打开一个命令行文本编辑器(例如nano)并创建一个新文件:

  • nano hello.py

    纳米hello.py

Once the text file opens up in Terminal we’ll type out our program:

在终端中打开文本文件后,我们将输入程序:

print("Hello, World!")

Exit nano by typing the control and x keys, and when prompted to save the file press y.

通过输入controlx键退出nano,然后在提示您保存文件时,按y

Once you exit out of nano and return to your shell, let’s run the program:

一旦退出nano并返回外壳,让我们运行该程序:

  • python hello.py

    python hello.py

The hello.py program that you just created should cause Terminal to produce the following output:

您刚创建的hello.py程序应使Terminal产生以下输出:


   
   
Output
Hello, World!

To leave the environment, simply type the command deactivate and you’ll return to your original directory.

要离开环境,只需键入命令deactivate ,您将返回到原始目录。

结论 (Conclusion)

Congratulations! At this point you have a Python 3 programming environment set up on your local Mac OS X machine and can begin a coding project!

恭喜你! 至此,您已经在本地Mac OS X机器上设置了Python 3编程环境,并且可以开始编码项目!

To set up Python 3 on another computer, follow the local programming environment guides for Ubuntu 16.04, Debian 8, CentOS 7, or Windows 10. You can also read about installing Python and setting up a programming environment on an Ubuntu 16.04 server, which is especially useful when working on development teams.

要在另一台计算机上设置Python 3,请遵循适用于Ubuntu 16.04Debian 8CentOS 7Windows 10本地编程环境指南 。 您还可以阅读有关在Ubuntu 16.04服务器上安装Python和设置编程环境的信息 ,这在与开发团队合作时特别有用。

With your local machine ready for software development, you can continue to learn more about coding in Python by following “Understanding Data Types in Python 3” and “How To Use Variables in Python 3”.

在准备好进行软件开发的本地计算机之后,您可以按照“ 了解Python 3中的数据类型 ”和“ 如何在Python 3中使用变量 ”继续学习有关Python编码的更多信息。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-python-3-and-set-up-a-local-programming-environment-on-macos

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值