Anaconda 简介

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Anaconda 提供一个管理工具 conda ,可以把 conda 看作是
pip + virtualenv +PVM (Python Version Manager) +
一些必要的底层库,也就是一个更完整也更大的集成管理工具。
一般的搞深度学习的都会用这个工具


Anaconda Navigtor :
用于管理工具包和环境的图形用户界面,后续涉及的众多管理命令也可以在 Navigator 中手工实现。

Jupyter notebook :
基于web的交互式计算环境,可以编辑易于人们阅读的文档,用于展示数据分析的过程。

qtconsole :
一个可执行 IPython 的仿终端图形界面程序,相比 Python Shell 界面,
qtconsole 可以直接显示代码生成的图形,实现多行代码输入执行,以及内置许多有用的功能和函数。

spyder :一个使用Python语言、跨平台的、科学运算集成开发环境。

安装Anaconda

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
从官网可以直接下载,有不同平台的安装文件的。这里马哥采用的是Linux平台,
https://www.anaconda.com/download/#linux

这个是linux平台的下载链接 https://repo.anaconda.com/archive/Anaconda3-5.2.0-Linux-x86_64.sh (2018年08月24日更新)

下载之后是一个shell文件,这个文件有几百MB大小,这是个安装文件,文件的前半部分是普通的shell代码,可以用vim打开看的,
文件的后部分都是二进制内容,看不到什么的,这些内容最后会解压缩到你的安装路径下的。

安装 直接执行 这个 Anaconda3-5.2.0-Linux-x86_64.sh 文件进行

如果没有执行权限可以使用chmo 755 Anaconda3-5.2.0-Linux-x86_64.sh 加上执行权限。

然后是 ./Anaconda3-5.2.0-Linux-x86_64.sh 执行

执行过程是交互式的,非常的简单。

最后是设置一下环境变量path,安装的最后会提示你的。


bash 把环境变量配置到 ~/.bashrc 文件中最后 export PATH="$HOME/anaconda3/bin:$PATH".
这里假设你的anaconda3安装到了家目录下面。

zsh 把环境变量配置到 ~/.zshrc 文件中,添加
path=(
$HOME/anaconda3/bin
~/bin
$path
)



安装完之后就可以试试命令是否可以正确执行了

常用命令

切换环境

1
2
3
4
source /home/conda/bin/activate aais-2.7   #切换环境,linux需要前面加上个source

source /home/conda/bin/deactivate aais-2.7 #取消切换的环境

列出所有的环境

1
2
3
4
5
6
7
8
9
$ /home/conda/bin/conda env list 
# conda environments:
#
base * /home/conda
aais-2.7 /home/conda/envs/aais-2.7
aais-3.8 /home/conda/envs/aais-3.8
ansible /home/conda/envs/ansible
ota /home/conda/envs/ota

创建新的环境,

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
$ /home/conda/bin/conda create -n py3.7 python=3.7    # 一种方式就是这样执行去创建一个环境。
Collecting package metadata (current_repodata.json): done
Solving environment: done


==> WARNING: A newer version of conda exists. <==
current version: 4.9.2
latest version: 4.11.0

Please update conda by running

$ conda update -n base -c defaults conda



## Package Plan ##

environment location: /home/conda/envs/py3.7

added / updated specs:
- python=3.7


The following packages will be downloaded:

package | build
---------------------------|-----------------
_libgcc_mutex-0.1 | main 3 KB
_openmp_mutex-4.5 | 1_gnu 22 KB
ca-certificates-2021.10.26 | h06a4308_2 115 KB
certifi-2021.10.8 | py37h06a4308_0 151 KB
ld_impl_linux-64-2.35.1 | h7274673_9 586 KB
libffi-3.3 | he6710b0_2 50 KB
libgcc-ng-9.3.0 | h5101ec6_17 4.8 MB
libgomp-9.3.0 | h5101ec6_17 311 KB
libstdcxx-ng-9.3.0 | hd4cf53a_17 3.1 MB
ncurses-6.3 | h7f8727e_2 782 KB
openssl-1.1.1l | h7f8727e_0 2.5 MB
pip-21.2.2 | py37h06a4308_0 1.8 MB
python-3.7.11 | h12debd9_0 45.3 MB
readline-8.1 | h27cfd23_0 362 KB
setuptools-58.0.4 | py37h06a4308_0 775 KB
sqlite-3.36.0 | hc218d9a_0 990 KB
tk-8.6.11 | h1ccaba5_0 3.0 MB
wheel-0.37.0 | pyhd3eb1b0_1 33 KB
xz-5.2.5 | h7b6447c_0 341 KB
zlib-1.2.11 | h7b6447c_3 103 KB
------------------------------------------------------------
Total: 65.1 MB

The following NEW packages will be INSTALLED:

_libgcc_mutex pkgs/main/linux-64::_libgcc_mutex-0.1-main
_openmp_mutex pkgs/main/linux-64::_openmp_mutex-4.5-1_gnu
ca-certificates pkgs/main/linux-64::ca-certificates-2021.10.26-h06a4308_2
certifi pkgs/main/linux-64::certifi-2021.10.8-py37h06a4308_0
ld_impl_linux-64 pkgs/main/linux-64::ld_impl_linux-64-2.35.1-h7274673_9
libffi pkgs/main/linux-64::libffi-3.3-he6710b0_2
libgcc-ng pkgs/main/linux-64::libgcc-ng-9.3.0-h5101ec6_17
libgomp pkgs/main/linux-64::libgomp-9.3.0-h5101ec6_17
libstdcxx-ng pkgs/main/linux-64::libstdcxx-ng-9.3.0-hd4cf53a_17
ncurses pkgs/main/linux-64::ncurses-6.3-h7f8727e_2
openssl pkgs/main/linux-64::openssl-1.1.1l-h7f8727e_0
pip pkgs/main/linux-64::pip-21.2.2-py37h06a4308_0
python pkgs/main/linux-64::python-3.7.11-h12debd9_0
readline pkgs/main/linux-64::readline-8.1-h27cfd23_0
setuptools pkgs/main/linux-64::setuptools-58.0.4-py37h06a4308_0
sqlite pkgs/main/linux-64::sqlite-3.36.0-hc218d9a_0
tk pkgs/main/linux-64::tk-8.6.11-h1ccaba5_0
wheel pkgs/main/noarch::wheel-0.37.0-pyhd3eb1b0_1
xz pkgs/main/linux-64::xz-5.2.5-h7b6447c_0
zlib pkgs/main/linux-64::zlib-1.2.11-h7b6447c_3


Proceed ([y]/n)? # 这里输入 y 就行,后续就会下载相关包了


详细的 create 命令 可以使用--help查看

$ /home/conda/bin/conda create -h
usage: conda create [-h] [--clone ENV] [-n ENVIRONMENT | -p PATH] [-c CHANNEL] [--use-local] [--override-channels] [--repodata-fn REPODATA_FNS] [--strict-channel-priority] [--no-channel-priority] [--no-deps | --only-deps] [--no-pin] [--copy] [-C] [-k] [--offline] [-d] [--json] [-q] [-v] [-y] [--download-only] [--show-channel-urls]
[--file FILE] [--no-default-packages] [--dev]
[package_spec [package_spec ...]]

Create a new conda environment from a list of specified packages. To use the created environment, use 'conda activate envname' look in that directory first. This command requires either the -n NAME or -p PREFIX option.

Options:

positional arguments:
package_spec Packages to install or update in the conda environment.

optional arguments:
-h, --help Show this help message and exit.
--clone ENV Path to (or name of) existing local environment.
--file FILE Read package versions from the given file. Repeated file specifications can be passed (e.g. --file=file1 --file=file2).
--dev Use `sys.executable -m conda` in wrapper scripts instead of CONDA_EXE. This is mainly for use during tests where we test new conda source against old Python versions.

Target Environment Specification:
-n ENVIRONMENT, --name ENVIRONMENT
Name of environment.
-p PATH, --prefix PATH
Full path to environment location (i.e. prefix).

Channel Customization:
-c CHANNEL, --channel CHANNEL
Additional channel to search for packages. These are URLs searched in the order they are given (including local directories using the 'file://' syntax or simply a path like '/home/conda/mychan' or '../mychan'). Then, the defaults or channels from .condarc are searched (unless --override-channels is given). You can
use 'defaults' to get the default packages for conda. You can also use any name and the .condarc channel_alias value will be prepended. The default channel_alias is http://conda.anaconda.org/.
--use-local Use locally built packages. Identical to '-c local'.
--override-channels Do not search default or .condarc channels. Requires --channel.
--repodata-fn REPODATA_FNS
Specify name of repodata on remote server. Conda will try whatever you specify, but will ultimately fall back to repodata.json if your specs are not satisfiable with what you specify here. This is used to employ repodata that is reduced in time scope. You may pass this flag more than once. Leftmost entries are
tried first, and the fallback to repodata.json is added for you automatically.

Solver Mode Modifiers:
--strict-channel-priority
Packages in lower priority channels are not considered if a package with the same name appears in a higher priority channel.
--no-channel-priority
Package version takes precedence over channel priority. Overrides the value given by `conda config --show channel_priority`.
--no-deps Do not install, update, remove, or change dependencies. This WILL lead to broken environments and inconsistent behavior. Use at your own risk.
--only-deps Only install dependencies.
--no-pin Ignore pinned file.
--no-default-packages
Ignore create_default_packages in the .condarc file.

Package Linking and Install-time Options:
--copy Install all packages using copies instead of hard- or soft-linking.

Networking Options:
-C, --use-index-cache
Use cache of channel index files, even if it has expired.
-k, --insecure Allow conda to perform "insecure" SSL connections and transfers. Equivalent to setting 'ssl_verify' to 'false'.
--offline Offline mode. Don't connect to the Internet.

Output, Prompt, and Flow Control Options:
-d, --dry-run Only display what would have been done.
--json Report all output as json. Suitable for using conda programmatically.
-q, --quiet Do not display progress bar.
-v, --verbose Can be used multiple times. Once for INFO, twice for DEBUG, three times for TRACE.
-y, --yes Do not ask for confirmation.
--download-only Solve an environment and ensure package caches are populated, but exit prior to unlinking and linking packages into the prefix.
--show-channel-urls Show channel urls. Overrides the value given by `conda config --show show_channel_urls`.

Examples:

conda create -n myenv sqlite
$

创建新的环境,

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
$ conda env create -f py35.yml  # 一般的这样使用 用个yaml文件

$ conda env create --help
usage: conda-env create [-h] [-f FILE] [-n ENVIRONMENT | -p PATH] [-q]
[--force] [--json] [--debug] [--verbose]
[remote_definition]

Create an environment based on an environment file

Options:

positional arguments:
remote_definition remote environment definition / IPython notebook

optional arguments:
-h, --help Show this help message and exit.
-f FILE, --file FILE environment definition file (default: environment.yml)
-n ENVIRONMENT, --name ENVIRONMENT
Name of environment.
-p PATH, --prefix PATH
Full path to environment prefix.
-q, --quiet
--force force creation of environment (removing a previously
existing environment of the same name).
--json Report all output as json. Suitable for using conda
programmatically.
--debug Show debug output.
--verbose, -v Use once for info, twice for debug, three times for
trace.

examples:
conda env create
conda env create -n name
conda env create vader/deathstar
conda env create -f=/path/to/environment.yml
conda env create -f=/path/to/requirements.txt -n deathstar
conda env create -f=/path/to/requirements.txt -p /home/user/software/deathstar

```


# 更新环境
```
$ conda env update -h
usage: conda-env update [-h] [-n ENVIRONMENT | -p PATH] [-f FILE] [--prune]
[-q] [--json] [--debug] [--verbose]
[remote_definition]

Update the current environment based on environment file

Options:

positional arguments:
remote_definition remote environment definition / IPython notebook

optional arguments:
-h, --help Show this help message and exit.
-n ENVIRONMENT, --name ENVIRONMENT
Name of environment.
-p PATH, --prefix PATH
Full path to environment prefix.
-f FILE, --file FILE environment definition (default: environment.yml)
--prune remove installed packages not defined in
environment.yml
-q, --quiet
--json Report all output as json. Suitable for using conda
programmatically.
--debug Show debug output.
--verbose, -v Use once for info, twice for debug, three times for
trace.

examples:
conda env update
conda env update -n=foo
conda env update -f=/path/to/environment.yml
conda env update --name=foo --file=environment.yml
conda env update vader/deathstar


执行命令

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
$ bin/conda run -h  # 这种就是临时的 指定某个python环境去执行某个命令,不像交互式的时候 source activate那样,这种的命令执行结束后 对当前的环境没有什么影响的。
[10:23:17]
usage: conda run [-h] [-n ENVIRONMENT | -p PATH] [-v] [--dev] [--debug-wrapper-scripts] [--cwd CWD] [--no-capture-output] [--live-stream] ...

Run an executable in a conda environment. [Experimental]

Use '--' (double dash) to separate CLI flags for 'conda run' from CLI flags sent to
the process being launched.

Example usage:

$ conda create -y -n my-python-2-env python=2
$ conda run -n my-python-2-env python --version

Options:

positional arguments:
executable_call Executable name, with additional arguments to be passed to the executable on invocation.

optional arguments:
-h, --help Show this help message and exit.
-v, --verbose Use once for info, twice for debug, three times for trace.
--dev Sets `CONDA_EXE` to `python -m conda`, assuming the CWD contains the root of conda development sources. This is mainly for use during tests where we test new conda source against old Python versions.
--debug-wrapper-scripts
When this is set, where implemented, the shell wrapper scriptswill echo to stderr a lot of debugging information.
--cwd CWD Current working directory for command to run in. Defaults to cwd
--no-capture-output Don't capture stdout/stderr
--live-stream Display the output for the subprocess stdout and stderr on real time.

Target Environment Specification:
-n ENVIRONMENT, --name ENVIRONMENT
Name of environment.
-p PATH, --prefix PATH
Full path to environment location (i.e. prefix).



$ bin/conda run -n aais-2.7 python --version
Python 2.7.16 :: Anaconda, Inc.

$ bin/conda run -n aais-2.7 pip list
Package Version
----------------- ---------
asn1crypto 0.24.0
bcrypt 3.1.6
blinker 1.4
certifi 2019.9.11
cffi 1.12.3
chardet 3.0.4
cryptography 2.7
enum34 1.1.6
funcsigs 1.0.2
idna 2.8
ipaddress 1.0.22
mock 3.0.5
oauthlib 3.0.2
paramiko 2.0.1
pip 19.2.2
psutil 5.6.3
psycopg2 2.8.3
pyasn1 0.4.6
pycparser 2.19
PyJWT 1.7.1
PyNaCl 1.3.0
pyOpenSSL 19.0.0
PySocks 1.7.0
pyte 0.4.10
pytz 2019.2
requests 2.22.0
requests-oauthlib 1.2.0
setuptools 41.0.1
simplejson 3.16.0
six 1.12.0
urllib3 1.24.2
wheel 0.33.4
xlrd 1.2.0
xlwt 1.3.0

DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support

$ bin/conda run -n aais-2.7 which python # 显示aais-2.7 环境下面的python路径
/home/conda/envs/aais-2.7/bin/python


$ bin/conda run -n ota python --version # 显示ota环境下面 python 版本号
Python 2.7.18 :: Anaconda, Inc.

$ bin/conda run -n ansible python --version # 显示ansible环境下面 python 版本号
Python 3.8.12

$ bin/conda run -n ansible pip list # 显示ansible环境下面安装的包
Package Version
------------------ ---------
ansible 5.0.1
ansible-core 2.12.1
appdirs 1.4.4
bcrypt 3.2.0
boto 2.49.0
brotlipy 0.7.0
certifi 2021.10.8
cffi 1.14.6
charset-normalizer 2.0.9
cryptography 3.4.8
decorator 5.1.0
dogpile.cache 1.1.4
httplib2 0.20.2
idna 3.1
iso8601 1.0.2
Jinja2 3.0.3
jmespath 0.10.0
jsonpatch 1.32
jsonpointer 2.0
keystoneauth1 4.4.0
MarkupSafe 2.0.1
munch 2.5.0
netifaces 0.10.9
openstacksdk 0.60.0
os-service-types 1.7.0
packaging 21.3
paramiko 2.8.1
pbr 5.8.0
pip 21.2.4
pycparser 2.21
PyNaCl 1.4.0
pyOpenSSL 21.0.0
pyparsing 3.0.6
PySocks 1.7.1
PyYAML 5.4.1
requests 2.26.0
requestsexceptions 1.4.0
resolvelib 0.5.4
setuptools 58.0.4
six 1.16.0
stevedore 3.5.0
urllib3 1.26.7
wheel 0.37.0


jupyter 环境的一些配置

配置默认显示行号

1
2
3
4
5
6
7
8
9
10

在自己的家目录下面的 .jupyter\nbconfig 目录下面 新建个 nbconfig 文件。添加如下内容
{
"Cell": {
"cm_config": {
"lineNumbers": true
}
}
}

配置默认宽度

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
默认的cell宽度比较窄看代码不爽,可以调整为100% 浏览器宽度。
在自己的家目录下面的 .jupyter\custom 目录下面新建 custom.css 文件

/*
* Placeholder for custom user CSS mainly to be overridden
* in profile/static/custom/custom.css
* This will always be an empty file in IPython
*/
.CodeMirror pre {
font-family: Consolas;
font-size: 10pt;
}

* {
font-family: Consolas;
}

div.output_area pre {
font-family: Consolas;
font-size: 10pt;
}

div.input_prompt {
font-family: Consolas;
font-size: 10pt;
}

div.out_prompt_overlay {
font-family: Consolas;
font-size: 10pt;
}

div.prompt {
font-family: Consolas;
font-size: 10pt;
}

span.cm-comment {
font-family: Consolas !important;
font-style: normal !important;
color: #FFAE3C !important;
}

.container {
width: 100% !important;
}


jupyter 配置 jupyter_notebook_config.py 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
生成密码
打开ipython,就是弹出来的网页里按右上角的 new,然后选python

from notebook.auth import passwd
passwd()

按回车,它会提示你输入密码,记住!!!这就是你要设置的密码!!输一遍验证一遍是国际惯例,然后你就把密码设好了。

据说下面跳出来的sha….要记着,不知道干嘛,反之我是记了。

然后吧这个字符串 配置到.jupyter下面的 jupyter_notebook_config.py 文件中c.NotebookApp.password后面。大概260行左右

# The string should be of the form type:salt:hashed-password.
c.NotebookApp.password = 'sha1:be508d594774:f1d0ff02b2e368b5b5e141110dc5f5b45b77fab9'


配置起始工作目录,
c.NotebookApp.notebook_dir = '/home/bright/work'

配置notebook的ip。默认是localhost,改过之后就可以任意ip电脑都可以访问了
c.NotebookApp.ip = '0.0.0.0'

jupyter 配置不同的python环境

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
$ jupyter-kernelspec list

Available kernels:
python2.7.6 /home/bright/.local/share/jupyter/kernels/python2.7.6
python3 /home/bright/.local/share/jupyter/kernels/python3
python3.7.0 /home/bright/.local/share/jupyter/kernels/python3.7.0


source activate learn-python2.7.6
python -m ipykernel install --user --name python2.7.6 --display-name "Python 2.7.6"



source activate learn-python3.7.0
python -m ipykernel install --user --name python3.7.0 --display-name "Python 3.7.0"


$ tree .local/share/jupyter

.local/share/jupyter
├── kernels
│   ├── python2.7.6
│   │   ├── kernel.json
│   │   ├── logo-32x32.png
│   │   └── logo-64x64.png
│   ├── python3
│   │   ├── kernel.json
│   │   ├── logo-32x32.png
│   │   └── logo-64x64.png
│   └── python3.7.0
│   ├── kernel.json
│   ├── logo-32x32.png
│   └── logo-64x64.png
├── nbsignatures.db
└── notebook_secret

4 directories, 11 files

$ cat .local/share/jupyter/kernels/python*/kernel.json

{
"display_name": "Python 2.7.6",
"language": "python",
"argv": [
"/home/bright/anaconda3/envs/learn-python2.7.6/bin/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
]
}

{
"argv": [
"/home/bright/anaconda3/envs/learn-python3.7.0/bin/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3.7.0",
"language": "python"
}

{
"argv": [
"python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3",
"language": "python"
}



Advanced Jupyter Notebook Tricks — Part I

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
Magics Jupyter Notebook中的魔术命令
You probably know that you can start notebooks with different kernels (e.g., R, Julia) — not just Python. What you might not know is that even within a notebook, you can run different types of code in different cells. With “magics”, it is possible to use different languages. The magics that are available vary per notebook kernel, however.

By running % lsmagic in a cell you get a list of all the available magics. You can use % to start a single-line expression to run with the magics command. Or you can use a double %% to run a multi-line expression.


Some of my favorites are:


% env to list your environment variables.

!: to run a shell command. E.g., ! pip freeze | grep pandas to see what version of pandas is installed.

% matplotlib inline to show matplotlib plots inline the notebook.

% pastebin 'file.py' to upload code to pastebin and get the url returned.

% bash to run cell with bash in a subprocess.

% time will time whatever you evaluate

% latex to render cell contents as LaTeX

%timeit will time whatever you evaluate multiple times and give you the best, and the average times

%prun, %lprun, %mprun can give you line-by-line breakdown of time and memory usage in a function or script. See a good tutorial here.

%% HTML: to render the cell as HTML. So you can even embed an image or other media in your notebook: