0% found this document useful (0 votes)
46 views4 pages

Py 6

Uploaded by

fortiratra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views4 pages

Py 6

Uploaded by

fortiratra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

sudo make install

Also add the path of new python in PATH environment variable. If new python is in /root/python-
2.7.X then run

export PATH = $PATH:/root/python-2.7.X

Now to check if Python installation is valid write in terminal:

python --version

Ubuntu (From Source)

If you need Python 3.6 you can install it from source as shown below (Ubuntu 16.10 and 17.04 have
3.6 version in

the universal repository). Below steps have to be followed for Ubuntu 16.04 and lower versions:

sudo apt install build-essential checkinstall

sudo apt install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbmdev

libc6-dev libbz2-dev

wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz

tar xvf Python-3.6.1.tar.xz

cd Python-3.6.1/

./configure --enable-optimizations

sudo make altinstall

macOS

As we speak, macOS comes installed with Python 2.7.10, but this version is outdated and slightly
modified from the

regular Python.

The version of Python that ships with OS X is great for learning but it’s not good for development.
The

version shipped with OS X may be out of date from the official current Python release, which is

considered the stable production version. (source)

Install Homebrew:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Install Python 2.7:

brew install python

For Python 3.x, use the command brew install python3 instead.

Section 1.11: String function - str() and repr()


There are two functions that can be used to obtain a readable representation of an object.

repr(x) calls x.__repr__(): a representation of x. eval will usually convert the result of this function
back to the

original object.

str(x) calls x.__str__(): a human-readable string that describes the object. This may elide some
technical detail.

repr()

GoalKicker.com – Python® Notes for Professionals 29

For many types, this function makes an attempt to return a string that would yield an object with the
same value

when passed to eval(). Otherwise, the representation is a string enclosed in angle brackets that
contains the name

of the type of the object along with additional information. This often includes the name and address
of the object.

str()

For strings, this returns the string itself. The difference between this and repr(object) is that
str(object) does

not always attempt to return a string that is acceptable to eval(). Rather, its goal is to return a
printable or 'human

readable' string. If no argument is given, this returns the empty string, ''.

Example 1:

s = """w'o"w"""

repr(s) # Output: '\'w\\\'o"w\''

str(s) # Output: 'w\'o"w'

eval(str(s)) == s # Gives a SyntaxError

eval(repr(s)) == s # Output: True

Example 2:

import datetime

today = datetime.datetime.now()

str(today) # Output: '2016-09-15 06:58:46.915000'

repr(today) # Output: 'datetime.datetime(2016, 9, 15, 6, 58, 46, 915000)'

When writing a class, you can override these methods to do whatever you want:

class Represent(object):
def __init__(self, x, y):

self.x, self.y = x, y

def __repr__(self):

return "Represent(x={},y=\"{}\")".format(self.x, self.y)

def __str__(self):

return "Representing x as {} and y as {}".format(self.x, self.y)

Using the above class we can see the results:

r = Represent(1, "Hopper")

print(r) # prints __str__

print(r.__repr__) # prints __repr__: '<bound method Represent.__repr__ of

Represent(x=1,y="Hopper")>'

rep = r.__repr__() # sets the execution of __repr__ to a new variable

print(rep) # prints 'Represent(x=1,y="Hopper")'

r2 = eval(rep) # evaluates rep

print(r2) # prints __str__ from new object

print(r2 == r) # prints 'False' because they are different objects

Section 1.12: Installing external modules using pip

pip is your friend when you need to install any package from the plethora of choices available at the
python

package index (PyPI). pip is already installed if you're using Python 2 >= 2.7.9 or Python 3 >= 3.4
downloaded from

python.org. For computers running Linux or another *nix with a native package manager, pip must
often be

manually installed.

GoalKicker.com – Python® Notes for Professionals 30

On instances with both Python 2 and Python 3 installed, pip often refers to Python 2 and pip3 to
Python 3. Using

pip will only install packages for Python 2 and pip3 will only install packages for Python 3.

Finding / installing a package

Searching for a package is as simple as typing

$ pip search <query>

# Searches for packages whose name or summary contains <query>


Installing a package is as simple as typing (in a terminal / command-prompt, not in the Python
interpreter)

$ pip install [package_name] # latest version of the package

$ pip install [package_name]==x.x.x # specific version of the package

$ pip install '[package_name]>=x.x.x' # minimum version of the package

where x.x.x is the version number of the package you want to install.

When your server is behind proxy, you can install package by using below command:

$ pip --proxy http://<server address>:<port> install

Upgrading installed packages

When new versions of installed packages appear they are not automatically installed to your system.
To get an

overview of which of your installed packages have become outdated, run:

$ pip list --outdated

To upgrade a specific package use

$ pip install [package_name] --upgrade

Updating all outdated packages is not a standard functionality of pip.

Upgrading pip

You can upgrade your existing pip installation by using the following commands

On Linux or macOS X:

$ pip install -U pip

You may need to use sudo with pip on some Linux Systems

On Windows:

py -m pip install -U pip

or

python -m pip install -U pip

You might also like