Since this is a course about Python scripting, we will be writing the majority of our code in scripts instead of using the REPL. To create a Python script, we can create a file ending with the file extension of
Let's create our first script to write our obligatory "Hello, World!" program:
$ vim hello.py
From inside this file we can enter the lines of Python that we need. For the "Hello, World!" example we only need
There are a few different pays that we can run this file. The first is by passing it to the
$ python3.7 hello.py Hello, World!
You'll most likely want your scripts to be:
Thankfully, we can set the process to interpret our scripts by setting a shebang at the top of the file:
#!/usr/bin/env python3.7 print("Hello, World")
We're not quite done, because now we need to make the file executable using
$ chmod u+x hello.py
Run the script now by using
./hello.py and we'll see the same result. If we'd rather not have a file extension on our script we can now remove that since we've put a shebang in the file. Renaming it to get rid of the extension (
mv hello.py hello) and running
./hello will still result in Python 3.7 executing the script.
Now we need to make sure that we can put this in our
$PATH. For this course, we'll be using a
bin directory in our
$HOME folder to store our custom scripts, but scripts can go into any directory that is in your $PATH.
Let's create a
bin directory and move our script:
$ mkdir ~/bin $ mv hello ~/bin/
Here's how we add this directory to the $PATH in our
.bashrc for this course already contains this):
$ export PATH=$HOME/bin:$PATH
Finally, let's run the hello script from our
$ hello Hello, World!