Setting up Python 3 on your Raspberry Pi

Being a windows user for a long time and likewise writing all the python3 scripts in the windows environment was easy and cool.

But then there is Raspberry Pi as well. And that is on Raspbian. Well as far as the python is concerned there is no issue with the script that I wrote on window. It would run just as well and without any issues on any linux or raspbian operating. There is a fits line you will have to change offcourse. And we will address that malso when we come to that part and within this blog itself.

I am assuming here that you are able to login in to your raspberry pi and are ready for the show.

You are ready even if you have just booted your raspi and logged in. It is just fine if you are directly connected to your raspi. That is, you have hooked up your monitor directly to the raspi with a HDMI cable and are using a USB keyboard and mouse that is directly hooked up to raspi. This is the simplest and the barest minimum setup you nee to have to get started.

But first things first, let’s see what are the things we are going to discuss in this blog :

  1. Where is your python installed. Get to know your python directory location on your raspi.
  2. Where would you store your python scripts and where is your home directory ?
  3. What should be in your python script’s file for it to be run from a command line?
  4. What would you need to install before if you are running a script in this environment for the first time ?
    1. Installing pip for python2 (if for some reason you still are using it) and for python3
    2. Installing other python packages that you may use in yoru scripts
  5. Lastly, running the script from a command line.
  6. (In the next blog post) I will cover how to run your script as a cronjob on your raspi.

Install python 3 on RasPi : sudo apt-get install python3

Executing scripts in the python-shell using python3 : python3 name_of_your_script.py

Shabang line on your python scripts : #1 /usr/bin/ python3

Directory where python3 is installed is :

Directory where you should store your python scripts : /home/pi/your_python_scripts_dir/

Installing pip for python3 : sudo apt-get install python3-pip

 

sudo apt-get install python-pip

sudo apt-get install python3-pip

sudo apt-get install python2-pip

 

It is also good to install the python setup tools :

sudo apt-get install python3-setuptools

Then you modify your shell script to the new aliases if you are only going to use python 3 :

alias vi =’vim’

alias python = ‘python 3,2’

alias pip = ‘pip-3.2’

 

The best way to do this is with your favourite editor and then adding the lines you mention, followed by using source to make them available in your current session.

sudo nano ~/.bash_aliases
source ~/.bash_aliases

Once this is done… you can install the packages for python3 and using pip as :

sudo pip install besutifulsoup4

instead of having to type the long form each time.

sudo pip-3.2 install beautifulsoup4 for example.cd\