0% found this document useful (0 votes)
28 views

Run Python On Ubuntu

Uploaded by

janardandev
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Run Python On Ubuntu

Uploaded by

janardandev
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Install Python 3 Using apt (Easier)

Most factory versions of Ubuntu 18.04 or Ubuntu 20.04 come with Python pre-
installed. Check your version of Python by entering the following:

python ��version

If the revision level is lower than 3.7.x, or if Python is not installed, continue
to the next step.
Step 1: Update and Refresh Repository Lists
Open the terminal by searching for it in the dashboard or pressing Ctrl + Alt + T
and enter the following:

sudo apt update

Step 2: Install Supporting Software


The software-properties-common package gives you better control over your package
manager by letting you add PPA (Personal Package Archive) repositories. Install the
supporting software with the command:

sudo apt install software-properties-common


install additional software for python
Step 3: Add Deadsnakes PPA
Deadsnakes is a PPA with newer releases than the default Ubuntu repositories. Add
the PPA by entering the following:

sudo add-apt-repository ppa:deadsnakes/ppa


The system will prompt you to press enter to continue. Do so, and allow it to
finish. Refresh the package lists again:

sudo apt update


Step 4: Install Python 3
Now you can start the installation of Python 3.8 with the command:

sudo apt install python3.8


Allow the process to complete and verify the Python version was installed
sucessfully::

python ��version
Check Python version to confirm installation.

-----------------------------------------------------------------------------------
--------------------------------------------------------------------------------
To use the WebDriver API in Python, you must first install the Selenium Python
bindings. This will give you access to your browser from Python code. The easiest
way to install the bindings is via pip.
On Ubuntu/Debian systems, this will install pip (and dependencies) and then install
the Selenium Python bindings from PyPI:

$ sudo apt-get install python-pip


$ sudo pip install selenium
After the installation, the following code should work:

#!/usr/bin/env python

from selenium import webdriver


browser = webdriver.Firefox()
browser.get('http://www.ubuntu.com/')
This should open a Firefox browser sessions and navigate to http://www.ubuntu.com/

Here is a simple functional test in Python, using Selenium WebDriver and the
unittest framework:

#!/usr/bin/env python

import unittest
from selenium import webdriver

class TestUbuntuHomepage(unittest.TestCase):

def setUp(self):
self.browser = webdriver.Firefox()

def testTitle(self):
self.browser.get('http://www.ubuntu.com/')
self.assertIn('Ubuntu', self.browser.title)

def tearDown(self):
self.browser.quit()

if __name__ == '__main__':
unittest.main(verbosity=2)
Output:
testTitle (__main__.TestUbuntuHomepage) ... ok

----------------------------------------------------------------------
Ran 1 test in 5.931s

OK

-----
Install latest google-chrome webdriver for Python-selenium binding:

$ LATEST=$(wget -q -O - http://chromedriver.storage.googleapis.com/LATEST_RELEASE)
$ wget http://chromedriver.storage.googleapis.com/$LATEST/chromedriver_linux64.zip
$ unzip chromedriver_linux64.zip && sudo ln -s $PWD/chromedriver
/usr/local/bin/chromedriver
Try below Example to open 'http://www.ubuntu.com/' in google-chrome browser:

#!/usr/bin/env python
from selenium import webdriver

browser = webdriver.Chrome()
browser.get('http://www.ubuntu.com/')
You may also need to update the path, as explained here

On Unix systems you can do the following to append it to your system�s search path,
if you�re using a bash-compatible shell:

export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step
On Windows you will need to update the Path system variable to add the full
directory path to the executable geckodriver manually or command line(don't forget
to restart your system after adding executable geckodriver into system PATH to take
effect). The principle is the same as on Unix.

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
Running a Script :

Once the script has been written, save it to a specific location in your system and
then follow the steps below to run it:

Open the terminal by searching for it in the dashboard or pressing Ctrl + Alt + T.

Navigate the terminal to the directory where the script is located using the cd
command.

Type python SCRIPTNAME.py in the terminal to execute the script.

If the script is python3, use python3 in the terminal command:python3 SCRIPTNAME.py

You might also like