• Nenhum resultado encontrado

INSTALL PYTHON AND SPYDER

A P P E N D I X A

Installing Python

This appendix explains how to install the Python environment described in the main text. The Anaconda distribution is provided and maintained by Anaconda, Inc. It includes the Python language, many libraries, the Spyder integrated development environment (IDE), and the Jupyter Notebooks system in a single package. (A softwarepackageis a collection of programs and data files related to a particular application or library. The data files include information about any other packages that may be required for the new software to function properly.) Anaconda also provides a simple protocol for updating packages and installing additional packages. There are many alternatives available. You may find one better suited to your own needs or preferences. One place to look is www.python.org.

Our intention is not to promote a particular distribution of Python, but to promote Python itself as a tool for scientific computing.

114 Appendix A Installing Python

The Anaconda website offers extensive documentation, so even if the instructions given here are insufficient, you should still be able to get Anaconda up and running on your computer. For the most detailed and up to date installation instructions, go todocs.anaconda.com/anaconda/install.

A.1.1 Graphical installation macOS

To get the latest stable version of Python, onanaconda.com/download/find the “Download” button directly beneath “Python 3.7 version.” Click on the link to download the package. This is a large download that will install many packages not used in this tutorial; however, the installation is very easy. Unless you are comfortable working from the command line, this is the installation we recommend.

Once the download finishes, find the package and open it. It is probably in your Downloads folder. Double-click on the file to initiate an installation dialog. Unless you want to customize the installation, just click “Continue” until you reach “Installation type.” Click “Change Install Location,”

then click “Install for Me Only.” This will create a folder called anaconda3orAnaconda3in your home directory and store all the associated files there. Click “Continue.” Finally, click “Install” to initiate a standard installation. You may be prompted for a password.

Once the installation is complete, you may find a new icon on your Desktop calledAnaconda Navigator.app. Open this application. Click on the

Launchbutton below “spyder” to start Spyder.

You can also start Spyder withoutAnaconda Navigator.appby double-clicking onspyderin the folder where Anaconda installed it. By default, this isanaconda3/binor Anaconda3/bin. A third option is to start Spyder from your operating system’s command line by typing

$ spyder

and hitting <Return/Enter>. (Don’t type the$.)

Once you have successfully installed Anaconda and launched Spyder, proceed to Section A.2.

Windows

To get the latest stable version of Python, onanaconda.com/download/find the “Download” button directly beneath “Python 3.7 version.” Click on the link to download the package. This is a large download that will install many packages not used in this tutorial; however, the installation is very easy. Unless you are comfortable working from the command line, this is the installation we recommend.

Once the download finishes, find the package and open it. It is probably in your Downloads folder. Double-click on the file to initiate an installation dialog. Unless you want to customize the installation, just click “Continue” until you reach “Destination Select.” Select “Install for Me Only”

unless you have a good reason for doing otherwise. This will create a folder calledAnaconda3in your home directory and store all the associated files there. Click “Continue” to bring up the “Installation Type” dialog. Click “Install” to initiate a standard installation. You may be prompted for a password.

Once the installation is complete, you may find a new icon on your Desktop calledAnaconda Navigator.app. Open this application. Click on the

Launchbutton below “spyder” to start Spyder.

You can also start Spyder without the Desktop application. After the install, you will find a new folder calledAnacondaorAnaconda (64-bit)underStart>All Programs. This folder contains a shortcut to launch Spyder. The script that launches Spyder may also be found in the User directory under Anaconda3\Scripts\spyder-script.py; alternatively, Anaconda3\Scripts\spyder.exe

A.1 Install Python and Spyder 115 will also work.

Once you have successfully installed Anaconda and launched Spyder, proceed to Section A.2.

A.1.2 Command line installation

To install Python on a Linux machine, or to install a leaner environment that includes only the packages you want under macOS or Windows, you can use theconda package manager. You can directly access this tool from the command line.

In this appendix, “command line” refers to a command line interface for your operating system—

not the IPython command prompt. For macOS users, this is Terminal.app. For Windows users, it iscmd.exe. For Linux users, it is a bash shell. We have indicated the command prompt as$, but it may look different on your system. Do not type the command prompt symbol when entering the following commands.

To install Anaconda from the command line, first, download Miniconda. Go to

docs.conda.io/en/latest/miniconda.htmland select the current version of Python 3 for your operating system.

Once the download is complete, macOS and Linux users will need to run a script. At the command line, type

$ bash ˜/Downloads/Miniconda3-latest-MacOSX-x86_64.sh

for macOS, or the equivalent command for your operating system, file name, and file location. This will run the script. Windows users should double click the.exefile and follow the instructions on the screen. After you agree to the license terms and select a directory for the installation (or agree to the default location), the script will install Python 3 and theconda package manager. (Note that you need to specify the complete path: /Users/username/anaconda3or /anaconda3for macOS or Linux, orC:Anaconda3for Windows. If you only provide the name “anaconda,” then files will be installed in a new directory named anacondain your current directory, wherever that may be.)

If this was successful, you can now run Python, but you will not yet be able to use modules like NumPy, PyPlot, or SciPy, and you will not have access to the Spyder IDE. You can now install them, and the entire Anaconda Python distribution, by typing

$ conda update conda

$ conda install anaconda

If you prefer to install individual packages to minimize disk space, this is simple usingconda: Instead of the second line above, use commands like

$ conda install numpy matplotlib scipy ipython

If you watch the screen after you execute these commands, you will see whatcondadoes. It determines which packages are necessary to install the specific ones you asked for, downloads all of the required packages, and then installs and links everything.

Python comes with the IDLE integrated development environment. However, if you want to use the Spyder IDE described in this tutorial, you will need to install it.

$ conda install spyder

In principle, this should install all the packages you need to run Spyder. Launch Spyder by typing

$ spyder

Jump to Contents Jump to Index

116 Appendix A Installing Python

In practice, you may find that some modules are missing and Spyder will not run. Spyder is actually a colossal Python program. When something is amiss, it displays Python error messages. Perhaps Spyder crashes when you try to run it. At the end of the terminal output you see

ImportError: No module named 'docutils' Just issue the command

$ conda install docutils

and this problem is fixed. To get Spyder up and running, install any other missing modules using the conda installcommand.

Even after Spyder is running, you may not have access to all of the debugging tools described in this tutorial. If you are writing your own code later and find another missing module, you may be able to install it withconda.

To run all of the code samples in this tutorial, you will need to usecondato install the following packages:

conda install ipython IPython interpreter conda install numpy NumPy

conda install matplotlib Matplotlib and PyPlot conda install scipy SciPy

conda install pillow Image processing library conda install sympy Symbolic computation library

To use the Spyder IDE or Jupyter notebooks, you will need to install them separately:

conda install spyder Spyder IDE (discussed above) conda install jupyter Jupyter Notebooks

After installing Anaconda, you can runanaconda-navigatorandspyderfrom the command line. If you use Miniconda instead, you can installanaconda-navigatorandspyderseparately, and then run these programs from the command line.

This set of packages should suffice to complete the exercises in this tutorial. However, there is a lot more you can do withconda. For example, you can set up environments that allow you to switch between Python 2 and Python 3, or use different versions of NumPy and SciPy. You can learn more aboutcondaat docs.conda.io/en/latest/.