- Daniel Arbuckle's Mastering Python
- Daniel Arbuckle
- 406字
- 2021-07-02 21:09:34
Installing Python
If you're a Windows or Mac user, you can download an installer directly from the Python website (https://www.python.org/downloads/). Pick the installer appropriate for your computer, download it, and run it for any platform. We also have the option of downloading the source code, compiling it, and installing Python that way.
Unix and Linux users, as well as Mac users who prefer it, have the option to install Python through their package manager instead. For systems which integrate a package manager, this is probably the best and easiest choice. If we use the package manager, this next part is probably already done, otherwise we need to make sure that the Python programs are able to run from the command line.
On macOS and Unix-like operating systems, all we need is to add a line to the profile or the bashrc file in our home directory:
- macOS X (edit ~/.profile):
export PATH=<pydir>:$PATH
- Unix/Linux (edit ~/.bashrc):
export PATH=<pydir>:$PATH
- Windows:
- Open Advanced System Settings in Control Panel.
- Click on Environments Variables....
- Edit PATH.
- Add ;<pydir> at the end.
Windows is only slightly more involved, as you'll need to open up the Control Panel and locate the Environment Variables screen. In each of the preceding examples, pydir is the directory where you installed Python—C:\python36, for example.
Once we've got the path environment variable set, we should be good to go. To check that-open a Terminal window (Command Prompt on Windows) and type Python, then hit Enter. If you don't know how to open the Terminal, don't worry, we'll talk about that in more detail in the next chapter.
Also, if you are a Unix user and you do not receive the correct result, it is potentially because the bashrc file or profile has not been executed yet. You may need to log out and log back in again.
That's it for setting up.
If you're feeling adventurous, you can experiment with the interactive shell that we just started up. Try typing in mathematical expressions and see what happens. In the next section, we'll look more closely at running Python code using the command line and the interactive shell.