Mac OSX can have multiple type of python; System python and the installed pythons. The system python should never be removed. To find the working version of python, open Terminal and type either of
python --version
python3 --version
depending on python 2.x or 3.x. To remove unwanted versions,
brew uninstall python
brew uninstall python3
This will remove the python installed via Homebrew. When you encouter an error, a quick fix is
brew doctor
brew upgrade
brew cleanup
Pip is a Package Installer for Python. More precisely pip is for python3.x, and pip2 is for python 2.x. If we want to install a package on Python2, use pip2. Some examples are
pip2 install cython
pip2 install PyOpenGL
pip install pygame
pip install dlib
If you want to check which (legacy) versions of the package (e.g. numpy) is available, use (see here)
pip install --use-deprecated=legacy-resolver numpy==
When using virtual environments (pyenv-virtualenv, pipenv, anaconda, etc), we should first activate an environment, then install the package. For example, installing via pip (without virtualization) is different from installing via pipenv. If we want to install a package inside pipenv, type
pipenv install cython
Pyenv manages different versions of python. Both pyenv and pyenv-virtualenv can be installed by brew in OSX,
brew install pyenv
brew install pyenv-virtualenv
See the official website for setting up PATH. To check which version of pythons are available,
pyenv install --list
To install (or uninstall) a python version in pyenv,
pyenv (un)install 3.x.y
To check all the installed versions of pythons inside pyenv,
pyenv versions
To create a new virtal environment using the current version of python,
pyenv virtualenv environment_name
Pyenv-virtualenv manages virtual environments. Suppose you installed pyenv, pyenv-virtualenv, the latest version of python, set PATH, and created a new virtal environment environment_name (see this page for how to install).
To activate the virtal environment environment_name, and launch a jupyter notebook,
pyenv activate environment_name
(environment_name)> jupyter notebook
If you install various packages in the jupyter notebooks, then the packages remain installed inside this environment. (In Google Colab, you need to install packages for each session.) To exit from the virtal environment, type Ctrl+C or
pyenv deactivate
Even if you launch a jupyter notebook from inside a virtual environment, jupyter uses its default kernel. In order to switch to the ipython kernel of your new environment, you should click "Kernel > Switch Kernel" in the menu. Alternatively, you can use the command (see here)
jupyter notebook --NotebookApp.kernel_manager_class=my_kernel
Pip exists for each virtual environment of Python. Sometimes, pyenv-virtualenv chooses the correct Python but an incorrect Pip, which causes a ModuleNotFoundError. To avoid this problem, one may try
!python3 -m pip install XXX
rather than
!pip install XXX
The former calls Pip associated to the Python in action (virtual Python in the virtual environment), whereas the latter calls Pip associated to the global Python.
Maths The math module can be imported in two ways,
List
Loop “for” loop should be written in the format,
for (var) in (iterable object):
Range range(start, stop, step) is an iterable object, which should be used with caution.
Dictionary data type
Decimal and binary
Conditional statements
Class A class can be defined as follows,
class ClassA(ClassB):
def __init__(self):
do something
super().__init__(arg):
The first line constructs Class A inherited from Class B, and there is no optional argument. In the second line, __init__(self) constructs an instance of Class A. In the last line, super().__init__(arg) construct an instance of Class B with the argument arg. One should not confuse a class with a function. For more information, read the official documents like inheritance and super.
Misc