一、环境配置
1.0 安装
从
1、官网:https://www.anaconda.com/products/individual#linux
2、国内镜像:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
下载,https://repo.anaconda.com/archive/Anaconda3-2020.02-Linux-x86_64.sh
如果为debain或Ubuntu,则apt-get install libgl1-mesa-glx libegl1-mesa libxrandr2 libxrandr2 libxss1 libxcursor1 libxcomposite1 libasound2 libxi6 libxtst6
,安装依赖包。
如果为redhat或centos,则执行yum install libXcomposite libXcursor libXi libXtst libXrandr alsa-lib mesa-libEGL libXdamage mesa-libGL libXScrnSaver
,安装依赖包。
安装:
bash Anaconda3-2020.02-Linux-x86_64.sh
由于使用非root用户,则安装在家目录下PREFIX=/home/thirsd/anaconda3
1.1 设置环境变量
假设anaconda部署在D:\Programs\Anaconda3
,在环境变量中配置:
D:\Programs\Anaconda3;D:\Programs\Anaconda3\Scripts;D:\Programs\Anaconda3\Library\bin
输入 conda -V
检验是否安装以及当前conda的版本
查看anaconda的配置信息
conda config --show-sources
可以查看到.condarc的配置信息内容
1.2 验证环境变量
1)conda list 查看安装了哪些包
2)conda env list 或 conda info -e 查看当前存在哪些虚拟环境
3)conda update conda 检查更新当前conda
1.3 安装第三方库
1.3.1 直接安装
1、查询是否存在第三方包
anaconda search -t conda jieba`
d:\Programs\Anaconda5.3.1\Scripts>anaconda search -t conda jieba
Using Anaconda API: https://api.anaconda.org
Packages:
Name | Version | Package Types | Platforms | Builds
------------------------- | ------ | --------------- | --------------- | ----------
auto/jieba | 0.32 | conda | linux-64, linux-32 | py27_0
: http://github.com/fxsjy
conda-forge/jieba | 0.39 | conda | linux-64, win-32, osx-64, noarch, win-64 | py_1, py36_0, p
yh9f0ad1d_1, py35_0, py27_0
: Chinese Words Segementation Utilities
conda-forge/jieba3k | 0.35.1 | conda | linux-64, win-32, osx-64, win-64, noarch | py36_1001, py36
h9f0ad1d_1002, py37_1001, py38h32f6830_1002, py36_1, py37hc8dfbb8_1002, py36hc560c46_1002, py_0, py35_1001, py38_1001, p
y35_1
: Chinese Words Segementation Utilities
creditx/jieba | 0.38 | conda | linux-64 | py35_0, py27_0
hargup/jieba | | conda | linux-64 | py27_0
: Chinese Words Segementation Utilities
iilab/jieba | 0.36.2 | conda | linux-64, osx-64 | py34_0
: Chinese Words Segementation Utilities
2、获取安装命令
anaconda show conda-forge/jieba
d:\Programs\Anaconda5.3.1\Scripts>anaconda show conda-forge/jieba
Using Anaconda API: https://api.anaconda.org
Name: jieba
Summary: Chinese Words Segementation Utilities
Access: public
Package Types: conda
Versions:
+ 0.38
+ 0.39
To install this package with conda run:
conda install --channel https://conda.anaconda.org/conda-forge jieba
根据安装jieba,执行最后一行的信息即可安装输入
3、执行安装
d:\Programs\Anaconda5.3.1\Scripts>conda install --channel https://conda.anaconda.org/conda-forge jieba
Solving environment: done
1.3.2 使用源码安装
1、打开anaconda promote的窗口
2、切换至源码目录,并执行python setup.py install
二、虚拟环境创建
2.1 查看当前的虚拟环境
2.1.1 查看虚拟环境列表
conda env list
,输出:
(django_restful) C:\Users\thirsd>conda env list
conda environments:
base D:\Programs\Anaconda3
django_restful * D:\Programs\Anaconda3\envs\django_restful
2.1.2 设置国内源
命令:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
信息配置在~/.condarc中,如:
thirsd@thirsd-16:~$ cat .condarc
channels:
- defaults
show_channel_urls: true
channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
运行 conda clean -i
清除索引缓存,保证用的是镜像站提供的索引
2.2 创建虚拟环境
创建虚拟环境:
conda create -n django_restful python=3.7
创建名字为django_restful的虚拟环境,指定的python版本为3.7
此时虚拟环境安装在:
anaconda的部署目录下的envs目录下,例如, /home/thirsd/anaconda3/envs/django_restful/
2.3 配置虚拟环境
2.3.1切换为虚拟环境
切换虚拟环境为django_restful
conda activate django_restful
此时命令提示符包含虚拟环境,如下所示:
(django_restful) thirsd@thirsd-16:~$
2.3.2 搜索需要安装的包
conda search django*
例如:
(django_restful) thirsd@thirsd-16:~$ conda search django*
Loading channels: done
# Name Version Build Channel
django 1.6.5 py26_0 anaconda/pkgs/free
django 1.6.5 py27_0 anaconda/pkgs/free
django 1.6.5 py33_0 anaconda/pkgs/free
django 1.6.5 py34_0 anaconda/pkgs/free
django 1.6.6 py26_0 anaconda/pkgs/free
2.3.4 在虚拟环境中,安装指定包
在django_restful的虚拟环境中,安装djangorestframework
conda install djangorestframework -n django_restful
conda install markdown -n django_restful # Markdown support for the browsable API.
conda install django-filter -n django_restful # Filtering support
conda install pygments -n django_restful # We'll be using this for the code highlighting
2.3.5 查看虚拟环境中的安装包情况
查看虚拟环境django_restful中的安装包情况
(django_restful) thirsd@thirsd-16:~$ conda list -n django_restful
2.3.6 更新虚拟环境中的安装包
更新命令为:`conda update $package_name -n $ $your_env_name`
conda update markdown -n django_restful
2.3.6 删除虚拟机环境中的安装包
删除命令格式为:`conda remove --name $your_env_name $package_name`
将markdown的包,从虚拟环境django_restful中删除
conda remove markdown -n django_restful
2.4 退出虚拟环境
从虚拟环境django_restful中,退出,执行`conda deactivate`
(django_restful) thirsd@thirsd-16:~$ conda deactivate
(base) thirsd@thirsd-16:~$
2.5 虚拟环境迁移
2.5.1导出虚拟环境
分享代码的时候,同时也需要将运行环境分享给大家
conda django_restful export > env.yaml
2.5.2 恢复虚拟环境
用对方分享的YAML文件来创建一模一样的运行环境
conda django_restful create -f env.yaml
2.6 删除虚拟环境
conda remove -n your_env_name(虚拟环境名称) --all
三、Djangorestfulframwork项目配置
3.1 手工配置
cd projects/ #进入项目保存的位置
django-admin startproject restful_demo #创建项目
python manage.py startapp app #创建web服务
目录结果如下:
(django_restful) thirsd@thirsd-16:~/projects/restful_demo ll
total 20
drwxrwxr-x 3 thirsd thirsd 4096 May 9 02:03 app/
-rwxrwxr-x 1 thirsd thirsd 632 May 9 02:02 manage.py*
drwxrwxr-x 3 thirsd thirsd 4096 May 9 02:02 restful_demo/(django_restful) thirsd@thirsd-16:~/projects/restful_demo$ ll app/
total 32
-rw-rw-r-- 1 thirsd thirsd 63 May 9 02:03 admin.py
-rw-rw-r-- 1 thirsd thirsd 81 May 9 02:03 apps.py
-rw-rw-r-- 1 thirsd thirsd 0 May 9 02:03 init.py
drwxrwxr-x 2 thirsd thirsd 4096 May 9 02:03 migrations/
-rw-rw-r-- 1 thirsd thirsd 57 May 9 02:03 models.py
-rw-rw-r-- 1 thirsd thirsd 60 May 9 02:03 tests.py
-rw-rw-r-- 1 thirsd thirsd 63 May 9 02:03 views.py
将新建的app的web应用和rest_framework
,加入到配置~/projects/restful_demo/restful_demo/settings.py中:
INSTALLED_APPS = [
...
'rest_framework',
'app.apps.appConfig',
]
app.apps.appConfig,实际为`${webname}.apps.${webname}Config`
实际存储情况为:
(django_restful) thirsd@thirsd-16:~/projects/restful_demo$ cat app/apps.py
from django.apps import AppConfig
class AppConfig(AppConfig):
name = 'app'
3.2 通过pycharm来配置
为支持远程访问,在我们创建的项目里**修改setting.py文件**
ALLOWED_HOSTS = ['*'] #在这里请求的host添加了*