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/

  1. # 查看版本
  2. PS D:\devTools\Python\virtual_env> conda --version
  3. conda 4.11.0
  4. PS D:\devTools\Python\virtual_env>
  5. PS D:\devTools\Python\virtual_env>
  6. # 添加清华镜像
  7. PS D:\devTools\Python\virtual_env> conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  8. PS D:\devTools\Python\virtual_env> conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  9. PS D:\devTools\Python\virtual_env> conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
  10. PS D:\devTools\Python\virtual_env> conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
  11. PS D:\devTools\Python\virtual_env>

2.3、创建 32 位的 python27

2.3.1、 先切换环境到 32 位

命令:set CONDA_FORCE_32BIT=1

2.3.2、创建 32 位的 python27 env

conda create —name python27-32

  1. C:\Users\DELL>conda create --name python27-32 python=2.7 django djangorestframework
  2. Collecting package metadata (current_repodata.json): done
  3. Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
  4. Collecting package metadata (repodata.json): done
  5. Solving environment: done
  6. ## Package Plan ##
  7. environment location: D:\devTools\Python\Miniconda3\envs\python27-32
  8. added / updated specs:
  9. - django
  10. - djangorestframework
  11. - python=2.7
  12. The following packages will be downloaded:
  13. package | build
  14. ---------------------------|-----------------
  15. ca-certificates-2021.10.26 | h9f7ea03_4 118 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  16. certifi-2020.6.20 | pyhd3eb1b0_3 155 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  17. django-1.11.10 | py_0 3.4 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
  18. djangorestframework-3.9.4 | py_0 756 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
  19. pip-20.1.1 | pyh9f0ad1d_0 1.1 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
  20. python-2.7.18 | hfb89ab9_0 14.9 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  21. pytz-2020.1 | pyh9f0ad1d_0 227 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
  22. setuptools-44.0.0 | py27_0 535 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  23. sqlite-3.30.1 | h0c8e037_0 561 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  24. vc-9 | h7299396_1 3 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  25. vs2008_runtime-9.00.30729.1| hfaea7d5_1 464 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  26. wheel-0.37.1 | pyhd8ed1ab_0 31 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
  27. wincertstore-0.2 | py27h87a2792_0 14 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  28. ------------------------------------------------------------
  29. Total: 22.2 MB
  30. The following NEW packages will be INSTALLED:
  31. ca-certificates anaconda/pkgs/main/win-32::ca-certificates-2021.10.26-h9f7ea03_4
  32. certifi anaconda/pkgs/main/noarch::certifi-2020.6.20-pyhd3eb1b0_3
  33. django anaconda/cloud/conda-forge/noarch::django-1.11.10-py_0
  34. djangorestframewo~ anaconda/cloud/conda-forge/noarch::djangorestframework-3.9.4-py_0
  35. pip anaconda/cloud/conda-forge/noarch::pip-20.1.1-pyh9f0ad1d_0
  36. done
  37. #
  38. # To activate this environment, use
  39. #
  40. # $ conda activate python27-32
  41. #
  42. # To deactivate an active environment, use
  43. #
  44. # $ conda deactivate
  45. C:\Users\DELL>conda env list
  46. # conda environments:
  47. #
  48. base * D:\devTools\Python\Miniconda3
  49. python27 D:\devTools\Python\Miniconda3\envs\python27
  50. python27-32 D:\devTools\Python\Miniconda3\envs\python27-32
  51. python35 D:\devTools\Python\Miniconda3\envs\python35
  52. python36 D:\devTools\Python\Miniconda3\envs\python36
  53. python38 D:\devTools\Python\Miniconda3\envs\python38
  54. C:\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

  1. (python27-32) C:\Users\DELL>conda install -c pythonnet pythonnet
  2. Collecting package metadata (current_repodata.json): done
  3. Solving environment: done
  4. ## Package Plan ##
  5. environment location: D:\devTools\Python\Miniconda3\envs\python27-32
  6. added / updated specs:
  7. - pythonnet
  8. The following packages will be downloaded:
  9. package | build
  10. ---------------------------|-----------------
  11. pythonnet-2.3.0 | py27_0 63 KB pythonnet
  12. ------------------------------------------------------------
  13. Total: 63 KB
  14. The following NEW packages will be INSTALLED:
  15. pythonnet pythonnet/win-32::pythonnet-2.3.0-py27_0
  16. Proceed ([y]/n)? y
  17. Downloading and Extracting Packages
  18. pythonnet-2.3.0 | 63 KB | ############################################################################ | 100%
  19. Preparing transaction: done
  20. Verifying transaction: done
  21. Executing transaction: done
  22. (python27-32) C:\Users\DELL>

安装 drf-yasg:conda install -c conda-forge drf-yasg

  1. (python27-32) C:\Users\DELL>conda install -c conda-forge drf-yasg
  2. Collecting package metadata (current_repodata.json): done
  3. Solving environment: done
  4. ## Package Plan ##
  5. environment location: D:\devTools\Python\Miniconda3\envs\python27-32
  6. added / updated specs:
  7. - drf-yasg
  8. The following packages will be downloaded:
  9. package | build
  10. ---------------------------|-----------------
  11. certifi-2018.4.16 | py27_0 143 KB conda-forge
  12. coreapi-2.3.3 | pyh9f0ad1d_0 23 KB conda-forge
  13. coreschema-0.0.4 | pyh9f0ad1d_0 15 KB conda-forge
  14. drf-yasg-1.20.0 | py_0 1.3 MB conda-forge
  15. inflection-0.5.1 | pyh9f0ad1d_0 9 KB conda-forge
  16. itypes-1.2.0 | pyh9f0ad1d_0 7 KB conda-forge
  17. jinja2-2.11.3 | pyh44b312d_0 93 KB conda-forge
  18. markupsafe-1.0 | py27_0 25 KB conda-forge
  19. packaging-20.9 | pyh44b312d_0 35 KB conda-forge
  20. pyparsing-2.4.7 | pyh9f0ad1d_0 60 KB conda-forge
  21. requests-2.12.5 | py27_0 763 KB conda-forge
  22. ruamel.ordereddict-0.4.13 | py27_0 30 KB conda-forge
  23. ruamel.yaml-0.15.37 | py27_0 235 KB conda-forge
  24. simplejson-3.13.2 | py27_0 97 KB conda-forge
  25. six-1.16.0 | pyh6c4a22f_0 14 KB conda-forge
  26. typing-3.6.4 | py27_0 42 KB conda-forge
  27. uritemplate-3.0.0 | py27_0 20 KB conda-forge
  28. ------------------------------------------------------------
  29. Total: 2.8 MB
  30. The following NEW packages will be INSTALLED:
  31. coreapi conda-forge/noarch::coreapi-2.3.3-pyh9f0ad1d_0
  32. coreschema conda-forge/noarch::coreschema-0.0.4-pyh9f0ad1d_0
  33. drf-yasg conda-forge/noarch::drf-yasg-1.20.0-py_0
  34. inflection conda-forge/noarch::inflection-0.5.1-pyh9f0ad1d_0
  35. itypes conda-forge/noarch::itypes-1.2.0-pyh9f0ad1d_0
  36. jinja2 conda-forge/noarch::jinja2-2.11.3-pyh44b312d_0
  37. markupsafe conda-forge/win-32::markupsafe-1.0-py27_0
  38. packaging conda-forge/noarch::packaging-20.9-pyh44b312d_0
  39. pyparsing conda-forge/noarch::pyparsing-2.4.7-pyh9f0ad1d_0
  40. requests conda-forge/win-32::requests-2.12.5-py27_0
  41. ruamel.ordereddict conda-forge/win-32::ruamel.ordereddict-0.4.13-py27_0
  42. ruamel.yaml conda-forge/win-32::ruamel.yaml-0.15.37-py27_0
  43. simplejson conda-forge/win-32::simplejson-3.13.2-py27_0
  44. six conda-forge/noarch::six-1.16.0-pyh6c4a22f_0
  45. typing conda-forge/win-32::typing-3.6.4-py27_0
  46. uritemplate conda-forge/win-32::uritemplate-3.0.0-py27_0
  47. done
  48. (python27-32) C:\Users\DELL>

查看已安装的包:

  1. (python27-32) C:\Users\DELL>conda list
  2. # packages in environment at D:\devTools\Python\Miniconda3\envs\python27-32:
  3. #
  4. # Name Version Build Channel
  5. certifi 2018.4.16 py27_0 conda-forge
  6. coreapi 2.3.3 pyh9f0ad1d_0 conda-forge
  7. coreschema 0.0.4 pyh9f0ad1d_0 conda-forge
  8. django 1.11.10 py_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
  9. djangorestframework 3.9.4 py_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
  10. drf-yasg 1.20.0 py_0 conda-forge
  11. inflection 0.5.1 pyh9f0ad1d_0 conda-forge
  12. itypes 1.2.0 pyh9f0ad1d_0 conda-forge
  13. jinja2 2.11.3 pyh44b312d_0 conda-forge
  14. markupsafe 1.0 py27_0 conda-forge
  15. packaging 20.9 pyh44b312d_0 conda-forge
  16. pip 9.0.1 py27_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  17. pyparsing 2.4.7 pyh9f0ad1d_0 conda-forge
  18. python 2.7.13 1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  19. pythonnet 2.3.0 py27_0 pythonnet
  20. pytz 2020.1 pyh9f0ad1d_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
  21. requests 2.12.5 py27_0 conda-forge
  22. ruamel.ordereddict 0.4.13 py27_0 conda-forge
  23. ruamel.yaml 0.15.37 py27_0 conda-forge
  24. setuptools 36.4.0 py27_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  25. simplejson 3.13.2 py27_0 conda-forge
  26. six 1.16.0 pyh6c4a22f_0 conda-forge
  27. typing 3.6.4 py27_0 conda-forge
  28. uritemplate 3.0.0 py27_0 conda-forge
  29. vc 9 h7299396_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  30. vs2008_runtime 9.00.30729.1 hfaea7d5_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  31. wheel 0.37.1 pyhd8ed1ab_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
  32. wincertstore 0.2 py27h87a2792_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  33. (python27-32) C:\Users\DELL>

安装不到时,上官网搜一下 https://anaconda.org/

4.2、使用 django 命令建立自己的 project

命令: django-admin startproject projectName

  1. (python27-32) D:\OtherProjects\action\practice-demo-python\python27-32> django-admin startproject u8-test
  2. CommandError: 'u8-test' is not a valid project name. Please use only numbers, letters and underscores.
  3. (python27-32) D:\OtherProjects\action\practice-demo-python\python27-32>

报错:名称不能有中划线。
修改命令:django-admin startproject u8_test

  1. (python27-32) D:\OtherProjects\action\practice-demo-python\python27-32> django-admin startproject u8_test
  2. (python27-32) D:\OtherProjects\action\practice-demo-python\python27-32>

查看目录:

  1. (python27-32) D:\OtherProjects\action\practice-demo-python\python27-32>tree /F
  2. Folder PATH listing
  3. Volume serial number is 34D6-1340
  4. D:.
  5. main.py
  6. ├───.idea
  7. .gitignore
  8. misc.xml
  9. modules.xml
  10. python27-32.iml
  11. vcs.xml
  12. workspace.xml
  13. └───inspectionProfiles
  14. profiles_settings.xml
  15. └───u8_test
  16. manage.py
  17. └───u8_test
  18. settings.py
  19. urls.py
  20. wsgi.py
  21. __init__.py
  22. (python27-32) D:\OtherProjects\action\practice-demo-python\python27-32>

4.3、添加自己的应用模块

命令:python project下的manage.py startapp appName

  1. ## 进入项目
  2. (python27-32) D:\OtherProjects\action\practice-demo-python\python27-32>cd u8_test
  3. (python27-32) D:\OtherProjects\action\practice-demo-python\python27-32\u8_test>python ./manage.py startapp u8-v1
  4. CommandError: 'u8-v1' is not a valid app name. Please use only numbers, letters and underscores.
  5. ## 报错,一样的应用名称也不能有中划线
  6. (python27-32) D:\OtherProjects\action\practice-demo-python\python27-32\u8_test>python ./manage.py startapp u8V1
  7. ## 查看目录层级结构
  8. (python27-32) D:\OtherProjects\action\practice-demo-python\python27-32\u8_test>tree /F
  9. Folder PATH listing
  10. Volume serial number is 34D6-1340
  11. D:.
  12. manage.py
  13. ├───u8V1
  14. admin.py
  15. apps.py
  16. models.py
  17. tests.py
  18. views.py
  19. __init__.py
  20. └───migrations
  21. __init__.py
  22. └───u8_test
  23. settings.py
  24. settings.pyc
  25. urls.py
  26. wsgi.py
  27. __init__.py
  28. __init__.pyc
  29. (python27-32) D:\OtherProjects\action\practice-demo-python\python27-32\u8_test>

4.4、编写自己的业务代码

步骤:

  1. 1. 编写 view
  2. 2. 创建路由
  3. 3. 加入模块路由到总路由
  4. 4. 注册自己的模块

4.4.1、编写 view

打开 views.py ,位置:u8_test\u8V1\views.py