1、背景
需要同步数据到 u8v13.0,调用 u8 api 的 csharp dll 接口。
由于u8版本较老,要使用 visual studio 2008 编写 X86 的 dll,python 也需要是 32 为的。
为了较为方便的部署到 Windows server 2008,python 版本选择为 python27-32位的。
2、环境准备
2.1、安装 mini-conda
下载地址: https://docs.conda.io/en/latest/miniconda.html
2.2、 配置 conda 镜像
conda config —add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config —add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config —add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config —add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
# 查看版本PS D:\devTools\Python\virtual_env> conda --versionconda 4.11.0PS D:\devTools\Python\virtual_env>PS D:\devTools\Python\virtual_env># 添加清华镜像PS D:\devTools\Python\virtual_env> conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/PS D:\devTools\Python\virtual_env> conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/PS D:\devTools\Python\virtual_env> conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/PS D:\devTools\Python\virtual_env> conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/PS D:\devTools\Python\virtual_env>
2.3、创建 32 位的 python27
2.3.1、 先切换环境到 32 位
2.3.2、创建 32 位的 python27 env
conda create —name python27-32
C:\Users\DELL>conda create --name python27-32 python=2.7 django djangorestframeworkCollecting package metadata (current_repodata.json): doneSolving environment: failed with repodata from current_repodata.json, will retry with next repodata source.Collecting package metadata (repodata.json): doneSolving environment: done## Package Plan ##environment location: D:\devTools\Python\Miniconda3\envs\python27-32added / updated specs:- django- djangorestframework- python=2.7The following packages will be downloaded:package | build---------------------------|-----------------ca-certificates-2021.10.26 | h9f7ea03_4 118 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/maincertifi-2020.6.20 | pyhd3eb1b0_3 155 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/maindjango-1.11.10 | py_0 3.4 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forgedjangorestframework-3.9.4 | py_0 756 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forgepip-20.1.1 | pyh9f0ad1d_0 1.1 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forgepython-2.7.18 | hfb89ab9_0 14.9 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/mainpytz-2020.1 | pyh9f0ad1d_0 227 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forgesetuptools-44.0.0 | py27_0 535 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/mainsqlite-3.30.1 | h0c8e037_0 561 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/mainvc-9 | h7299396_1 3 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/mainvs2008_runtime-9.00.30729.1| hfaea7d5_1 464 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/mainwheel-0.37.1 | pyhd8ed1ab_0 31 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forgewincertstore-0.2 | py27h87a2792_0 14 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main------------------------------------------------------------Total: 22.2 MBThe following NEW packages will be INSTALLED:ca-certificates anaconda/pkgs/main/win-32::ca-certificates-2021.10.26-h9f7ea03_4certifi anaconda/pkgs/main/noarch::certifi-2020.6.20-pyhd3eb1b0_3django anaconda/cloud/conda-forge/noarch::django-1.11.10-py_0djangorestframewo~ anaconda/cloud/conda-forge/noarch::djangorestframework-3.9.4-py_0pip anaconda/cloud/conda-forge/noarch::pip-20.1.1-pyh9f0ad1d_0done## To activate this environment, use## $ conda activate python27-32## To deactivate an active environment, use## $ conda deactivateC:\Users\DELL>conda env list# conda environments:#base * D:\devTools\Python\Miniconda3python27 D:\devTools\Python\Miniconda3\envs\python27python27-32 D:\devTools\Python\Miniconda3\envs\python27-32python35 D:\devTools\Python\Miniconda3\envs\python35python36 D:\devTools\Python\Miniconda3\envs\python36python38 D:\devTools\Python\Miniconda3\envs\python38C:\Users\DELL>
3、 安装 IDE 开发工具
pycharm 社区版: https://www.jetbrains.com/pycharm/download/#section=windows
4、u8项目
4.1、 创建项目(根据需要,是否创建)
pycharm 上的创建,file->new project 选择存放的位置、python env 等 ->create
选择 env ,上面创建的 python27-32
需要安装的包:django、djangorestframework、drf-yasg、pythonnet 等
安装 django djangorestframework
conda install django djangorestframework
安装 pythonnet:conda install -c pythonnet pythonnet
(python27-32) C:\Users\DELL>conda install -c pythonnet pythonnetCollecting package metadata (current_repodata.json): doneSolving environment: done## Package Plan ##environment location: D:\devTools\Python\Miniconda3\envs\python27-32added / updated specs:- pythonnetThe following packages will be downloaded:package | build---------------------------|-----------------pythonnet-2.3.0 | py27_0 63 KB pythonnet------------------------------------------------------------Total: 63 KBThe following NEW packages will be INSTALLED:pythonnet pythonnet/win-32::pythonnet-2.3.0-py27_0Proceed ([y]/n)? yDownloading and Extracting Packagespythonnet-2.3.0 | 63 KB | ############################################################################ | 100%Preparing transaction: doneVerifying transaction: doneExecuting transaction: done(python27-32) C:\Users\DELL>
安装 drf-yasg:conda install -c conda-forge drf-yasg
(python27-32) C:\Users\DELL>conda install -c conda-forge drf-yasgCollecting package metadata (current_repodata.json): doneSolving environment: done## Package Plan ##environment location: D:\devTools\Python\Miniconda3\envs\python27-32added / updated specs:- drf-yasgThe following packages will be downloaded:package | build---------------------------|-----------------certifi-2018.4.16 | py27_0 143 KB conda-forgecoreapi-2.3.3 | pyh9f0ad1d_0 23 KB conda-forgecoreschema-0.0.4 | pyh9f0ad1d_0 15 KB conda-forgedrf-yasg-1.20.0 | py_0 1.3 MB conda-forgeinflection-0.5.1 | pyh9f0ad1d_0 9 KB conda-forgeitypes-1.2.0 | pyh9f0ad1d_0 7 KB conda-forgejinja2-2.11.3 | pyh44b312d_0 93 KB conda-forgemarkupsafe-1.0 | py27_0 25 KB conda-forgepackaging-20.9 | pyh44b312d_0 35 KB conda-forgepyparsing-2.4.7 | pyh9f0ad1d_0 60 KB conda-forgerequests-2.12.5 | py27_0 763 KB conda-forgeruamel.ordereddict-0.4.13 | py27_0 30 KB conda-forgeruamel.yaml-0.15.37 | py27_0 235 KB conda-forgesimplejson-3.13.2 | py27_0 97 KB conda-forgesix-1.16.0 | pyh6c4a22f_0 14 KB conda-forgetyping-3.6.4 | py27_0 42 KB conda-forgeuritemplate-3.0.0 | py27_0 20 KB conda-forge------------------------------------------------------------Total: 2.8 MBThe following NEW packages will be INSTALLED:coreapi conda-forge/noarch::coreapi-2.3.3-pyh9f0ad1d_0coreschema conda-forge/noarch::coreschema-0.0.4-pyh9f0ad1d_0drf-yasg conda-forge/noarch::drf-yasg-1.20.0-py_0inflection conda-forge/noarch::inflection-0.5.1-pyh9f0ad1d_0itypes conda-forge/noarch::itypes-1.2.0-pyh9f0ad1d_0jinja2 conda-forge/noarch::jinja2-2.11.3-pyh44b312d_0markupsafe conda-forge/win-32::markupsafe-1.0-py27_0packaging conda-forge/noarch::packaging-20.9-pyh44b312d_0pyparsing conda-forge/noarch::pyparsing-2.4.7-pyh9f0ad1d_0requests conda-forge/win-32::requests-2.12.5-py27_0ruamel.ordereddict conda-forge/win-32::ruamel.ordereddict-0.4.13-py27_0ruamel.yaml conda-forge/win-32::ruamel.yaml-0.15.37-py27_0simplejson conda-forge/win-32::simplejson-3.13.2-py27_0six conda-forge/noarch::six-1.16.0-pyh6c4a22f_0typing conda-forge/win-32::typing-3.6.4-py27_0uritemplate conda-forge/win-32::uritemplate-3.0.0-py27_0done(python27-32) C:\Users\DELL>
查看已安装的包:
(python27-32) C:\Users\DELL>conda list# packages in environment at D:\devTools\Python\Miniconda3\envs\python27-32:## Name Version Build Channelcertifi 2018.4.16 py27_0 conda-forgecoreapi 2.3.3 pyh9f0ad1d_0 conda-forgecoreschema 0.0.4 pyh9f0ad1d_0 conda-forgedjango 1.11.10 py_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forgedjangorestframework 3.9.4 py_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forgedrf-yasg 1.20.0 py_0 conda-forgeinflection 0.5.1 pyh9f0ad1d_0 conda-forgeitypes 1.2.0 pyh9f0ad1d_0 conda-forgejinja2 2.11.3 pyh44b312d_0 conda-forgemarkupsafe 1.0 py27_0 conda-forgepackaging 20.9 pyh44b312d_0 conda-forgepip 9.0.1 py27_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/freepyparsing 2.4.7 pyh9f0ad1d_0 conda-forgepython 2.7.13 1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/freepythonnet 2.3.0 py27_0 pythonnetpytz 2020.1 pyh9f0ad1d_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forgerequests 2.12.5 py27_0 conda-forgeruamel.ordereddict 0.4.13 py27_0 conda-forgeruamel.yaml 0.15.37 py27_0 conda-forgesetuptools 36.4.0 py27_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/freesimplejson 3.13.2 py27_0 conda-forgesix 1.16.0 pyh6c4a22f_0 conda-forgetyping 3.6.4 py27_0 conda-forgeuritemplate 3.0.0 py27_0 conda-forgevc 9 h7299396_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/mainvs2008_runtime 9.00.30729.1 hfaea7d5_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/mainwheel 0.37.1 pyhd8ed1ab_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forgewincertstore 0.2 py27h87a2792_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main(python27-32) C:\Users\DELL>
安装不到时,上官网搜一下 https://anaconda.org/
4.2、使用 django 命令建立自己的 project
命令: django-admin startproject projectName
(python27-32) D:\OtherProjects\action\practice-demo-python\python27-32> django-admin startproject u8-testCommandError: 'u8-test' is not a valid project name. Please use only numbers, letters and underscores.(python27-32) D:\OtherProjects\action\practice-demo-python\python27-32>
报错:名称不能有中划线。
修改命令:django-admin startproject u8_test
(python27-32) D:\OtherProjects\action\practice-demo-python\python27-32> django-admin startproject u8_test(python27-32) D:\OtherProjects\action\practice-demo-python\python27-32>
查看目录:
(python27-32) D:\OtherProjects\action\practice-demo-python\python27-32>tree /FFolder PATH listingVolume serial number is 34D6-1340D:.│ main.py│├───.idea│ │ .gitignore│ │ misc.xml│ │ modules.xml│ │ python27-32.iml│ │ vcs.xml│ │ workspace.xml│ ││ └───inspectionProfiles│ profiles_settings.xml│└───u8_test│ manage.py│└───u8_testsettings.pyurls.pywsgi.py__init__.py(python27-32) D:\OtherProjects\action\practice-demo-python\python27-32>
4.3、添加自己的应用模块
命令:python project下的manage.py startapp appName
## 进入项目(python27-32) D:\OtherProjects\action\practice-demo-python\python27-32>cd u8_test(python27-32) D:\OtherProjects\action\practice-demo-python\python27-32\u8_test>python ./manage.py startapp u8-v1CommandError: 'u8-v1' is not a valid app name. Please use only numbers, letters and underscores.## 报错,一样的应用名称也不能有中划线(python27-32) D:\OtherProjects\action\practice-demo-python\python27-32\u8_test>python ./manage.py startapp u8V1## 查看目录层级结构(python27-32) D:\OtherProjects\action\practice-demo-python\python27-32\u8_test>tree /FFolder PATH listingVolume serial number is 34D6-1340D:.│ manage.py│├───u8V1│ │ admin.py│ │ apps.py│ │ models.py│ │ tests.py│ │ views.py│ │ __init__.py│ ││ └───migrations│ __init__.py│└───u8_testsettings.pysettings.pycurls.pywsgi.py__init__.py__init__.pyc(python27-32) D:\OtherProjects\action\practice-demo-python\python27-32\u8_test>
4.4、编写自己的业务代码
步骤:
1. 编写 view2. 创建路由3. 加入模块路由到总路由4. 注册自己的模块
4.4.1、编写 view
打开 views.py ,位置:u8_test\u8V1\views.py
