# use pip mirror for speeding up $ pip3 install -U pip # OR $ pip install -U pip
$ pip config set global.index-url http://mirrors.aliyun.com/pypi/simple/ # OR manually edit /root/.config/pip/pip.conf $ cat /root/.config/pip/pip.conf [global] index-url = http://mirrors.aliyun.com/pypi/simple/ # or change index-url temporary for each install $ pip install --index-url http://mirrors.aliyun.com/pypi/simple/ ansible-core==2.11.12
$ pip cache dir /home/ubuntu/.cache/pip
$ pip cache list Nothing cached.
# global install, all users see it!!! $ sudo pip install package_name # install/uninstall for current user $ pip install package_name # ~/.local/lib/python
# list all version of given package $ pip install ansible==not_exist # install a package of given version $ pip install package_name==version $ pip uninstall package_name
# pip list --format=freeze>req.txt $ pip install -r req.txt
# !!!while dpkg -l only shows apt-get installed packages!!!
# show info of package $ pip show self-tool-lib Name: self-tool-lib Version: 0.1 Summary: self utilities for tool Home-page: Author: Jason Luo Author-email: jason_lkm@163.com License: UNKNOWN Location: /home/data/Anaconda3/envs/py3.9/lib/python3.9/site-packages Requires: Required-by:
# show files of installed package $ pip show -f self-tool-lib Name: self-tool-lib Version: 0.1 Summary: self utilities for tool Home-page: Author: Jason Luo Author-email: jason_lkm@163.com License: UNKNOWN Location: /home/data/Anaconda3/envs/py3.9/lib/python3.9/site-packages Requires: Required-by: Files: self_tool_lib-0.1.dist-info/INSTALLER self_tool_lib-0.1.dist-info/LICENSE self_tool_lib-0.1.dist-info/METADATA self_tool_lib-0.1.dist-info/RECORD self_tool_lib-0.1.dist-info/REQUESTED self_tool_lib-0.1.dist-info/WHEEL self_tool_lib-0.1.dist-info/direct_url.json self_tool_lib-0.1.dist-info/top_level.txt
# show files in wheel file(which can be installed by pip directly) # pip install dist/self_tool_lib-0.1-py3-none-any.whl $ python -m zipfile --list dist/self_tool_lib-0.1-py3-none-any.whl File Name Modified Size self_tool_lib-0.1.dist-info/LICENSE 2022-06-14 15:19:08 1074 self_tool_lib-0.1.dist-info/METADATA 2022-06-14 15:19:08 403 self_tool_lib-0.1.dist-info/WHEEL 2022-06-14 15:19:08 92 self_tool_lib-0.1.dist-info/top_level.txt 2022-06-14 15:19:08 1 self_tool_lib-0.1.dist-info/RECORD 2022-06-14 15:19:08 404
Note: some python pkg may be only available by apt source or pypi source, not both
pkg source is different
apt-get can install any package from remote repo, not only python related but pip/pip3 only for python package, for some python packages you can install it from apt-get or pip, the difference is apt-get use source.list library that's dedicated for particular ubuntu version, the package from there is always old, but pip uses its own library(PyPI https://pypi.org/pypi) which always have new version for the application, while for some python package, apt-get may NOT have. from apt-get you can’t specify the version to install while pip can have.
pypi usually has many versions of a single pkg while apt source only has one
install dest location is different
apt-get installs python modules in system-wide location. We cannot just install modules in our project virtualenv. pip solves this problem for us. If we are using pip after activating the virtualenv, it is intelligent enough to only install the modules in our project virtualenv. As mentioned in previous point, if there is a version of a particular python package already installed in system-wide location, and one of our project requires an older version of the same python package, in such situations we can use virtualenv and pip to install that older version of python package without any conflicts.
apt dest location
1 2 3 4
# show dest location $ dpkg -L python3-httplib2 /usr/bin /usr/lib/
pip dest location
1 2 3 4 5 6
# show dest location $ pip show httplib2 /user/local/bin /usr/local/lib ~/.local/lib ~/.local/bin
if you install one package by these two, there will be two copies(may be two version of this pkg), but apt-get always wins, as the sys.path always, /usr/bin/; /usr/local/bin, but you can change the PATH.
conda(which has its own repo: https://anaconda.org/) is another way to install python pkg, as conda has a python env, activated after you reboot system after installation, hence pip/pip3 comes from conda python env, all packages installed either by conda or pip, they see the others as well!!!.
lambda is like a function(with return implicitly) with one statement, but no name(anonymous function), if you have more logic, use function instead, create a lambda means create a function object, then call it later on.
lambda features
return implicitly
one statement
anonymous function
f = lambda arg1, arg2: expression the result of the expression is return value.
In python, function is an object that means you can assign it to a variable or pass it as a parameter! we can also return a function inside another function, cool!
Assign functions to a variable
1 2 3 4 5
defgreet(name): return"hello " + name
greet_someone = greet greet_someone("John")
Function can return other function
1 2 3 4 5 6 7 8
defcompose_greet_func(name): defget_message(): return"Hello " + name
return get_message
greet = compose_greet_func("jason") greet()
if nothing returns in function, None returned by default
Python class is more like C++ class, it supports inheritance, function overridden (not like C++, if you create an instance of child class, you never see parent's method if child defines method with same name even different signature(argument list)), operator overload, static method etc.
Overloading vs Overriding Overloading occurs when two or more methods in one class have the same method name but different parameters.
Overriding occurs when two methods have the same method name and parameters. One of the methods is in the parent class, and the other is in the child class. Overriding allows a child class to provide the specific implementation of a method that is already present in its parent class
provides a mechanism for aggregating/partitioning sets of tasks, and all their future children, into hierarchical groups with specialized behavior(under resource controller) like
Resource Limiting (i.e. not to exceed a memory limit) mostly used
Prioritization (i.e. groups may have larger share of CPU)
Isolation (i.e. isolate GPUs for particular processes)
Accounting (i.e. monitor resource usage for processes)