0%

pip

pip commands

Here are frequently used commands with pip(pip3 for python3)

search available pkg online goto: official repo pypi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
$ pip config debug
env_var:
env:
global:
/etc/xdg/pip/pip.conf, exists: False
/etc/pip.conf, exists: False
site:
/usr/pip.conf, exists: False
user:
/root/.pip/pip.conf, exists: False
/root/.config/pip/pip.conf, exists: False

# 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

# list installed python packages using pip
$ pip list -v
Package Version Location Installer
---------------------------------- ------------------- -------------------------------------------------- ---------
alabaster 0.7.12 /home/ubuntu/anaconda3/lib/python3.8/site-packages conda
anaconda-client 1.7.2 /home/ubuntu/anaconda3/lib/python3.8/site-packages conda
anaconda-navigator 1.10.0 /home/ubuntu/anaconda3/lib/python3.8/site-packages
anaconda-project 0.8.3 /home/ubuntu/anaconda3/lib/python3.8/site-packages conda
argh 0.26.2 /home/ubuntu/anaconda3/lib/python3.8/site-packages
argon2-cffi 20.1.0 /home/ubuntu/anaconda3/lib/python3.8/site-packages conda
asn1crypto 1.4.0 /home/ubuntu/anaconda3/lib/python3.8/site-packages conda
astroid 2.4.2 /home/ubuntu/anaconda3/lib/python3.8/site-packages conda
astropy 4.0.2 /home/ubuntu/anaconda3/lib/python3.8/site-packages
async-generator 1.10 /home/ubuntu/anaconda3/lib/python3.8/site-packages conda
atomicwrites 1.4.0 /home/ubuntu/anaconda3/lib/python3.8/site-packages conda
attrs 20.3.0 /home/ubuntu/anaconda3/lib/python3.8/site-packages conda
autopep8 1.5.4 /home/ubuntu/anaconda3/lib/python3.8/site-packages conda
Babel 2.8.1 /home/ubuntu/anaconda3/lib/python3.8/site-packages conda
backcall 0.2.0 /home/ubuntu/anaconda3/lib/python3.8/site-packages conda
backports.functools-lru-cache 1.6.1 /home/ubuntu/anaconda3/lib/python3.8/site-packages conda
backports.shutil-get-terminal-size 1.0.0 /home/ubuntu/anaconda3/lib/python3.8/site-packages conda
backports.tempfile 1.0 /home/ubuntu/anaconda3/lib/python3.8/site-packages pip

$ pip list
Package Version
---------------------------------- -------------------
alabaster 0.7.12
anaconda-client 1.7.2
anaconda-navigator 1.10.0
anaconda-project 0.8.3
argh 0.26.2
argon2-cffi 20.1.0
asn1crypto 1.4.0
astroid 2.4.2
astropy 4.0.2

$ pip list --format=freeze
alabaster==0.7.12
anaconda-client==1.7.2
anaconda-navigator==1.10.0
anaconda-project==0.8.3
argh==0.26.2
argon2-cffi==20.1.0
asn1crypto==1.4.0
astroid==2.4.2
astropy==4.0.2
async-generator==1.10
atomicwrites==1.4.0
attrs==20.3.0
autopep8==1.5.4
Babel==2.8.1
backcall==0.2.0

$ pip list --outdated

# !!!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

build pip package

apt-get vs pip

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.

name is different

1
2
3
4
$ apt-get install python3-jinja2
$ yum install -y python3-jinja2
# vs
$ pip install jinjia2

conda

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!!!.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
$ conda info
active environment : base
active env location : /home/ubuntu/anaconda3
shell level : 1
user config file : /home/ubuntu/.condarc
populated config files :
conda version : 4.9.2
conda-build version : 3.20.5
python version : 3.8.5.final.0
virtual packages : __glibc=2.27=0
__unix=0=0
__archspec=1=x86_64
base environment : /home/ubuntu/anaconda3 (read only)
channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
https://repo.anaconda.com/pkgs/main/noarch
https://repo.anaconda.com/pkgs/r/linux-64
https://repo.anaconda.com/pkgs/r/noarch
package cache : /home/ubuntu/anaconda3/pkgs
/home/ubuntu/.conda/pkgs
envs directories : /home/ubuntu/.conda/envs
/home/ubuntu/anaconda3/envs
platform : linux-64
user-agent : conda/4.9.2 requests/2.24.0 CPython/3.8.5 Linux/4.15.0-136-generic ubuntu/18.04.5 glibc/2.27
UID:GID : 1000:1000
netrc file : None
offline mode : False

# USE conda mirror
$ cat /root/.condarc
channels:
- defaults
show_channel_urls: true
default_channels:
- http://mirrors.aliyun.com/anaconda/pkgs/main
- http://mirrors.aliyun.com/anaconda/pkgs/r
- http://mirrors.aliyun.com/anaconda/pkgs/msys2
custom_channels:
conda-forge: http://mirrors.aliyun.com/anaconda/cloud
msys2: http://mirrors.aliyun.com/anaconda/cloud
bioconda: http://mirrors.aliyun.com/anaconda/cloud
menpo: http://mirrors.aliyun.com/anaconda/cloud
pytorch: http://mirrors.aliyun.com/anaconda/cloud
simpleitk: http://mirrors.aliyun.com/anaconda/cloud

# list installed pkgs
$ conda list
# packages in environment at /home/ubuntu/anaconda3:
#
# Name Version Build Channel
_ipyw_jlab_nb_ext_conf 0.1.0 py38_0
_libgcc_mutex 0.1 main
alabaster 0.7.12 py_0
anaconda 2020.11 py38_0
anaconda-client 1.7.2 py38_0
anaconda-navigator 1.10.0 py38_0
anaconda-project 0.8.4 py_0
argh 0.26.2 py38_0
argon2-cffi 20.1.0 py38h7b6447c_1
asn1crypto 1.4.0 py_0
astroid 2.4.2 py38_0
astropy 4.0.2 py38h7b6447c_0
async_generator 1.10 py_0
atomicwrites 1.4.0 py_0
attrs 20.3.0 pyhd3eb1b0_0
autopep8 1.5.4 py_0
babel 2.8.1 pyhd3eb1b0_0
backcall 0.2.0 py_0
backports 1.0 py_2

# search available pkgs from remote repo
$ conda search '*scikit'
$ conda search 'numpy>=1.12'

# always use pkg from channel conda-forge which has high priority
$ conda search pytest-cov
Loading channels: done
# Name Version Build Channel
pytest-cov 2.5.1 py27_0 pkgs/main
pytest-cov 2.5.1 py27hc4181bb_0 pkgs/main
pytest-cov 2.5.1 py35_0 pkgs/main
pytest-cov 2.5.1 py35h9d7ae03_0 pkgs/main
pytest-cov 2.5.1 py36_0 pkgs/main
pytest-cov 2.5.1 py36hd4733f3_0 pkgs/main
pytest-cov 2.5.1 py37_0 pkgs/main
pytest-cov 2.6.0 py27_0 pkgs/main
pytest-cov 2.6.0 py35_0 pkgs/main
pytest-cov 2.6.0 py36_0 pkgs/main
pytest-cov 2.6.0 py37_0 pkgs/main
pytest-cov 2.6.1 py27_0 pkgs/main
pytest-cov 2.6.1 py36_0 pkgs/main
pytest-cov 2.6.1 py37_0 pkgs/main
pytest-cov 2.7.1 py_0 pkgs/main
pytest-cov 2.8.1 py_0 pkgs/main
pytest-cov 2.9.0 py_0 pkgs/main
pytest-cov 2.10.0 py_0 pkgs/main
pytest-cov 2.10.1 py_0 pkgs/main
pytest-cov 2.10.1 pyhd3eb1b0_0 pkgs/main
pytest-cov 2.11.0 pyhd3eb1b0_0 pkgs/main
pytest-cov 2.11.1 pyhd3eb1b0_0 pkgs/main

$ conda install -c conda-forge pytest-cov
$ conda info pytest-cov
$ conda uninstall pytest-cov

Advanced feature

lambda

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.

Read more »

Advanced function feature

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
def greet(name):
return "hello " + name

greet_someone = greet
greet_someone("John")

Function can return other function

1
2
3
4
5
6
7
8
def compose_greet_func(name):
def get_message():
return "Hello " + name

return get_message

greet = compose_greet_func("jason")
greet()

if nothing returns in function, None returned by default

Read more »

Class

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

Read more »

Overview

Here are some basics about python3 that you should know. if your code is written with python2, convert it to python3 with 2to3 tool.

drawing
1
2
$ 2to3 python2_file
$ 2to3 python2_dir

You can also run your app with virtual env, so that different python applications can use different python(python2 or python3) libraries

Read more »

Docker network

Overview

Docker’s networking subsystem is pluggable using drivers. Several drivers exist by default, and provide core networking functionality

Read more »

Introduction

Namespaces provide processes with their own view of the system, limit what you can see(and therefore use)

Read more »

cgroups

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)
  • Control (i.e. suspending and resuming processes)
Read more »