Pip dependency failure

Sometimes when install pip package it may result in dependency failure:

Downloading/unpacking cffi>=1.4.1 (from cryptography>=1.7->pyOpenSSL>=0.14->pyvmomi)
  Running setup.py (path:/tmp/pip_build_root/cffi/setup.py) egg_info for package cffi
    unable to execute 'x86_64-linux-gnu-gcc': No such file or directory
    unable to execute 'x86_64-linux-gnu-gcc': No such file or directory

        No working compiler found, or bogus compiler options
        passed to the compiler from Python's distutils module.
        See the error messages above.
        (If they are about -mno-fused-madd and you are on OS/X 10.8,
        see http://stackoverflow.com/questions/22313407/ .)
    Complete output from command python setup.py egg_info:
    unable to execute 'x86_64-linux-gnu-gcc': No such file or directory

unable to execute 'x86_64-linux-gnu-gcc': No such file or directory

To fix, try install deps first: Python3

sudo apt-get install python3 python-dev python3-dev \
     build-essential libssl-dev libffi-dev \
     libxml2-dev libxslt1-dev zlib1g-dev \
     python-pip

Python2

sudo apt-get install python-dev  \
     build-essential libssl-dev libffi-dev \
     libxml2-dev libxslt1-dev zlib1g-dev \
     python-pip

Virtualenv

Install virtualenv via pip:

$ pip install virtualenv

Create a virtual environment for a project:

$ virtualenv my_project

virtualenv my_project will create a folder in the current directory which will contain the Python executable files, and a copy of the pip library which you can use to install other packages. The name of the virtual environment (in this case, it was my_project) can be anything; omitting the name will place the files in the current directory instead. This creates a copy of Python in whichever directory you ran the command in, placing it in a folder named my_project.

You can also use the Python interpreter of your choice (like python2.7).

$ virtualenv -p /usr/bin/python2.7 my_project

or change the interpreter globally with an env variable in ~/.bashrc:

$ export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7

To begin using the virtual environment, it needs to be activated:

$ source my_project/bin/activate

The name of the current virtual environment will now appear on the left of the prompt (e.g. (my_project)Your-Computer:your_project UserName$) to let you know that it’s active. From now on, any package that you install using pip will be placed in the my_project folder, isolated from the global Python installation.

Install packages as usual, for example:

$ pip install requests

If you are done working in the virtual environment for the moment, you can deactivate it:

$ deactivate

This puts you back to the system’s default Python interpreter with all its installed libraries.

To delete a virtual environment, just delete its folder. (In this case, it would be rm -rf my_project.)

Class

The init() Function

All classes have a function called __init__(), which is always executed when the class is being initiated.

Use the __init__() function to assign values to object properties, or other operations that are necessary to do when the object is being created:

class Person:
  def __init__(self, name, age):
    self.name = name     #make external value 'name' become class's internal value which can be used in other function inside this class
    self.age = age

  def myfunc(self):   #self would now become [name age].
    print("Hello my name is " + self.name)

p1 = Person("John", 36)
p1.myfunc()

So the values given in __init__() actually represent the Class default value, and can further be used as self.* in other function inside that class. To better understand how this works, take a look at the code below:

class test:
    x = 5
    def __init__(self, template_file):
        self.template = template_file+"test"
        y = 6
        self.z = 7
print test.x
print test.y
print test.z
print test("lulu").x
print test("lulu").y
print test("lulu").z
print test("lulu").template

will output:

>>> print test.x
5

x won’t be visible in sub function, it’s usually used for class main function. Because we didn’t give class test what its __init__ function requires:

>>> print test.y
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: class test has no attribute 'y'

same reason as above, it fails for test.z

>>> print test.z
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: class test has no attribute 'z'

When we give class test what it asks in its __init__ func

>>> print test("lulu").x
5

y is not assigned to self property, which makes it only visible inside the class, so we can’t call it

>>> print test("lulu").y
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: test instance has no attribute 'y'

z and template are assigned to self and become visible in the entire class by calling self.*

>>> print test("lulu").z
7
>>> print test("lulu").template
lulutest

Take another look at the following code:

class test:
    x = 5
    def __init__(self, template_file):
        self.template = template_file+"test"
        y = 6
        self.z = 7
    def myfunc(self):
        a = self.z + 1
        self.b = self.z + 2
        print a 
        print a + x   #this causes break `global name 'x' is not defined`, because x is not self.property and not visible to the entire class.
        return self.b #self.b can be used in other func
print test("lulu").myfunc().a
print test("lulu").myfunc().b

output: __init__ property and func can be called by using class.*, but object property inside a function can’t be called this way

>>> print test("lulu").myfunc().a
8
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'a'

Can’t call it even if it’s a self.* defined class wise

>>> print test("lulu").myfunc().b
8
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'b'