Join Bill Weinman for an in-depth discussion in this video Getting started with "Hello World", part of Python 3 Essential Training.
As we start the Python Quick Start, we're going to use the traditional "Hello, World!" program. And purpose of "Hello, World!" is not an exercise to find the simplest form of code in a particular language, or anything like that. It's actually to learn about the development environment, to learn about what we call the development cycle: what it takes to edit, and to test, and to change, and to test and to run code in a particular environment. And so for this purpose we use the simplest form of a program itself, so that that can get out of the way and then we can focus on the environment.
In this case, I've this up here on the screen in Eclipse. And this is Eclipse using Pydev, which is a Python development environment that runs inside of Eclipse. Eclipse is an integrated development environment originally written for developing in Java, but it has plug- ins for a lot of different languages. And Pydev is actually a pretty good plug-in for the Python language. And so, we'll be using this throughout this course. You do not have to use Eclipse, and if you have some other development environment that you prefer, or you just want to use an editor and a command line, that's perfectly fine.
You can do all of the exercises; you do not need to use Eclipse. The reason that I'm using Eclipse is because it's convenient for the purpose of teaching. I've got everything that I need right up here on the screen. I can run code, I get the results of code, and it works well for the purpose of teaching, but you don't need to use it for your development work. And you do not need to use it to follow along in the exercises. In Eclipse, if I want to run this Hello, World!, I simply click on this Run button up here, and it'll ask me, How I want to run it? and I'm going to select Python Run.
I only need to do this the first time I run a script. So I'm going to say OK. And here we have the result down here: "Hello, World!" If I were running this in another environment and I actually have this script loaded up here on a Unix Server - this is my Unix Server - there is the script, you see it's exactly the same. And if run it I say ./hello.py, and it runs it. An important part of this is this shebang line.
If we look here in Eclipse, we see this shebang line here. In many environments, mostly Unix- based environments, but this includes Macintosh and it includes many, many web servers that are running Unix-based operating systems, like Linux or PSD, this shebang line is actually very important. It's called a shebang line because it's starts with a hash and then has an exclamation mark, which is sometimes called a bang. And then after that it has the path to the interpreter that will be used to run the script.
When the script is executed and the shell sees it, it will look here for the interpreter and then use that to run the script. So sometimes you'll see a bash up here, or Perl, or something like that. And all that's doing is it's telling the shell to use that interpreter to run whatever script is there. For Python, it's going to be something like this. It might be #!/usr/bin/python3. It might be #!/usr/local/bin/python3. It might be #!/usr/bin/env python3, something like that.
In this case, I'm using #!/usr/bin/python3 because I know that, that works in the environments that I'm using. And if it doesn't, then I'll make a symbolic link or something and make it work. So if you're running this on a Unix-based operating system, you'll want to do ls -l and look at the file permissions, and make sure that it has executable permissions. If it doesn't, you can make it executable by typing chmod, or as we pronounce it chmod 755, which makes it editable and executable for your user ID, and then the name of the file.
And that will make it executable. And then you can execute it with ./ or however it is that you do that on your system. So that is the development environment that we will be using. For the most part, we're going to using Eclipse. And so we'll be running scripts like this. Save it and Run it. It'll run here in the Console. So you're going to want to take the opportunity, using this "Hello, World!" script, to learn about the development cycle and whatever environment you're using, especially if it's not Eclipse, to follow along with the exercises in this course.
- A Python 3 quick start for experienced developers
- Creating functions and objects
- Using Python's built-in objects and classes
- Repeating code with loops and iterators
- Understanding and using conditional expressions
- Creating sequences with generators
- Reusing code with objects and libraries
- Handling errors with exceptions
Skill Level Beginner
Q: The installation process for the PyDev Eclipse plug-in doesn’t work as described in the videos. What should I do?
A: The version of Eclipse used in the recording of Python 3 Essential Training was Eclipse 3.5.2 (Galileo SR2) and the version of PyDev was 1.5.7. Soon after recording, the Eclipse project was updated to version 3.6, called Helios. As of this writing, the current version of Eclipse is 3.6.1 (Helios SR1). <br /> Around the same time as these updates were being released, the PyDev project was updated as well. As of this writing, the current version of PyDev is 1.6.2. If you are using these most recent versions, the procedure for installing Eclipse itself has not changed, but the process documented in the videos for installing PyDev may not work. <br /> <br /> We will be releasing new versions of the videos soon, but the author has posted a document describing the new installation procedure at:<br /> <a target="_blank" href="http://python.bw.org/pydev-installation/">http://python.bw.org/pydev-installation/</a>
Q: How do you install the pydev-interpreter in Eclipse Indigo on Mac OS X Lion? It's significantly different than what's shown in the video "Installing Python 3 and Eclipse for Mac."
A: Since this course was publishing, there have been upgrades to Mac OS X and Eclipse. In this scenario, after installing Python and Eclipse and the Pydev interpreter, there is a different directory to go to when modifying the preferences. As instructed in the movie (at around 6:40) restart Eclipse and then go to Eclipse > Preferences and drill down to Pydev > Interpreter - Python in the sidebar. Click New and in the Select interpreter dialog that opens, click Browse.<br /><br />The Open dialog box will open, but does not appear to display your hard drive. You must press and hold the Command+Shift+Period keys. This will display all hidden files on your system. Navigate to the new path ~/Library/Frameworks/Python.framework/versions/3.2/bin/python 3.2 and click Open.<br /><br />You should be able to proceed normally from there.
Q: When I try to install PyDev, it's not showing up in the Available Software window.
<div id="pastingspan1">A: This can happen if the pydev.org/updates site is down.</div><div> </div> <div id="pastingspan1"></div> <div id="pastingspan1"> You can manually download PyDev from the pydev.org web site, or from my site here:</div><div> </div> <div id="pastingspan1"></div> <div id="pastingspan1"> http://python.bw.org/</div><div> </div> <div id="pastingspan1"></div> <div id="pastingspan1"> Installation is simple. I've included instructions on my web site above.</div>