"Hello, world!" Python Script

Jimmy McBride - Apr 6 '20 - - Dev Community

Step 1: Set up bin folder.

Open up a bash terminal and in your home route (cd ~) run: echo $HOME

This returns your home path. For me, my home path is: /home/jimmy.

Then we can run: echo $PATH. You should get a long string that looks something like:

/home/jim/.yarn/bin:/home/jim/.config/yarn/global/node_modules/.bin:/home/jim/.yarn/bin:/home/jim/.config/yarn/global/node_modules/.bin:/home/jim/.yarn/bin:/home/jim/.config/yarn/global/node_modules/.bin:/home/jim/.local/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/var/lib/snapd/snap/bin:/home/jim/local/nodejs/bin:/home/jim/local/python/bin:/home/jim/bin:/home/jim/Android/Sdk/emulator:/home/jim/Android/Sdk/tools:/home/jim/Android/Sdk/tools/bin:/home/jim/Android/Sdk/platform-tools:/home/jim/local/nodejs/bin:/home/jim/local/python/bin:/home/jim/bin:/home/jim/Android/Sdk/emulator:/home/jim/Android/Sdk/tools:/home/jim/Android/Sdk/tools/bin:/home/jim/Android/Sdk/platform-tools:/home/jim/local/nodejs/bin:/home/jim/local/python/bin:/home/jim/bin:/home/jim/Android/Sdk/emulator:/home/jim/Android/Sdk/tools:/home/jim/Android/Sdk/tools/bin:/home/jim/Android/Sdk/platform-tools
Enter fullscreen mode Exit fullscreen mode

The : is what separates the paths.

Then lets: ls | grep bin. If it returns bin you're good to go. If it returns nothing, run: mkdir bin. Now that we have a bin folder, we need to know if $HOME/bin is in your path. If it was already, continue to next step. If it was not then run: ls -a | grep bash. It should return something like:

.bash_history
.bash_it
.bash_logout
.bash_profile
.bashrc
Enter fullscreen mode Exit fullscreen mode

If you have a .bashrc then open it in any text editor. If you have a .bash_profile (if it doesn't exist, then create it) open that in any text editor and make sure it contains the following code:

#
# ~/.bash_profile
#

[[ -f ~/.bashrc ]] && . ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Then create a .bashrc and make sure it includes the following contents:

#!/usr/bin/env bash

# Exports /home/jimmy/bin to PATH variable
export PATH=$PATH:$HOME/bin
Enter fullscreen mode Exit fullscreen mode

Perfect! Now that we have our bin folder set up, we can start to make some magic happen!

Step 2: Let's create a new file in our bin folder and write our script!

Now that everything is set up we can simply: cd bin && touch hello and open this hello file in our favorite text editor.

Once the file is open we can add the contents of our script:

#!/bin/python

print("Hello, world!")
Enter fullscreen mode Exit fullscreen mode

So you probably noticed that weird #!/bin/python thing at the top of the script. You probably might have noticed that we called the file hello and not hello.py. The .py extension on a file lets the computer know it's a python file, much the same the #!/bin/python also lets the computer know this is a python file. But what exactly is it?

Well, the #! (also called a shebang) lets the computer know that we're about to write a script. The /bin/python lets the computer know what language that script is going to be in. It's pointing to the actual location python is located on your computer. So you might need to do: /bin/python2 | /bin/python3. If you can't find it, try and run: cd /bin && ls -a and see if you have any files in there. If the location doesn't exist or python isn't there you might need to search your filesystem for where you have python installed.

Step 3: Making it executable!

Now we can try and run hello but we will get a permissions denied error. So if we want to run this we need to turn it into an executable file. We can do that by running: chmod 755 hello or chmod +x hello. I'm not going to get into the details in this lesson, but 755 and +x are the same thing. +x is just a shorthand for 755 which is the most common permissions setting when making a file executable. This makes sure you can read, write and execute and that everyone else can read and execute it.

Now what we've made it executable we can run our file again. We can type hello from any directory in our terminal now and it should return Hello, world!. πŸ”₯

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player