Python

From XennisWiki
(Redirected from Pip)
Jump to: navigation, search

Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C. (Wikipedia)

Setup

Installation

Ubuntu/Debian

Install Python (and for developing may also python-dev)

sudo apt-get install python
Install old version 2.6
sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get update
sudo apt-get install python2.6

Windows

Download the Python Windows installer and follow the introductions to install it

Execute Pythin in command line

The installer asks you to add path to your system path. Anyway, if you want to do it manually, expand the system variable PATH by adding (the second path it not necessary need): C:\Python27, C:\Python27\Scripts

pip

pip is a tool for installing and managing Python packages.

Installation

  • Ubuntu: sudo apt-get install python-pip (for Python 3 use python3-pip instead)
  • Windows: Download the get-pip.py file and execute it in the command line: python get-pip.py

Basic usage

Install a package or a specific version of a package

pip install <package-name>
pip install <package-name>==1.8.5
pip install <package-name>==1.8.*

Create a requirements file and install the requirements

pip freeze [--local] > requirements.txt
pip install -r requirements.txt

List packages

pip list

Advanced usage

Upgrade pip itself

pip install -U pip

Remove all git packages

pip freeze | xargs pip uninstall -y

virtualenv

Install virtualenv

virtualenv creates isolated Python environments.

pip install virtualenv

Usage

(1) Create the environment example_env, (2) activate, (3) install a Python package with pip (and (4) deactivate it)

virtualenv example_env
source example_env/bin/activate
pip install <package-name>
deactivate

In this environment you can normally run pip install <package>.

Specify Python version

Create an environment with a specific Python version

virtualenv --python python2.7 example_env
virtualenv --python /usr/bin/python2.7 example_env

Basics

Variables

example = 6
example = 'Hello'
example = float(6.0)
example = float('inf')  # infinity
example = int(6)

Condition

if example > 6:
    print 'if'
elif example is '6':
    print 'elif'
else:
    print 'else'

Loops

For loop

for i in range(0, 3):
    print 'index %d' % (x)

While loop

start = 0
while start <= 5:
    print start
    start += 1

Function

def myFunction(param1, param2):
    print param1
    return param2

Set

# unordered set
example = {20, 5, 31}
# ordered set
example = [20, 5, 31]
example = ['hello', 'world']

Advanced

Run a script

Create a file named myScript.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

print 'Hello world'

Run the file

python myScript.py

Main method and arguments

if __name__ == '__main__':
    argv = sys.argv[1:]
    if len(argv) == 1:
        print('filename: {}'.format(argv[0]))
    else:
        print('Usage: example.py <FILENAME>')

Class

class MyClass:

    TYPE = 'Hello'

    def __init__(self, type):
        if type != self.TYPE:
            raise Exception('Unkown type!')
        self.type = type

    def myFunction(self, param):
        print(self.type)
        print(param)
        return
MyClass('Hello').myFunction('World')
Inherit class
class Car:
    def myFunction(self, type):
        print("Hi")

class Audi(Car):
    def myFunction(self, type):
        print("World")

More

Code style

PEP 8 is a style guide for Python. You can check your code against this style convention by using the tool pep8 (documentation). In the Eclipse plugin PyDev is pep8 already include:

  • Window > Preferences
  • PyDev > Editor > Code Analysis
  • pep8.py

Problems and errors

IOError: Could not build the egg

Error message
IOError: Could not build the egg.
Solution[1]
pip install mysql-python

References

See also

External links