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 --version
conda 4.11.0
PS 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 djangorestframework
Collecting package metadata (current_repodata.json): done
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: done
## Package Plan ##
environment location: D:\devTools\Python\Miniconda3\envs\python27-32
added / updated specs:
- django
- djangorestframework
- python=2.7
The following packages will be downloaded:
package | build
---------------------------|-----------------
ca-certificates-2021.10.26 | h9f7ea03_4 118 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
certifi-2020.6.20 | pyhd3eb1b0_3 155 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
django-1.11.10 | py_0 3.4 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
djangorestframework-3.9.4 | py_0 756 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
pip-20.1.1 | pyh9f0ad1d_0 1.1 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
python-2.7.18 | hfb89ab9_0 14.9 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
pytz-2020.1 | pyh9f0ad1d_0 227 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
setuptools-44.0.0 | py27_0 535 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
sqlite-3.30.1 | h0c8e037_0 561 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
vc-9 | h7299396_1 3 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
vs2008_runtime-9.00.30729.1| hfaea7d5_1 464 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
wheel-0.37.1 | pyhd8ed1ab_0 31 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
wincertstore-0.2 | py27h87a2792_0 14 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
------------------------------------------------------------
Total: 22.2 MB
The following NEW packages will be INSTALLED:
ca-certificates anaconda/pkgs/main/win-32::ca-certificates-2021.10.26-h9f7ea03_4
certifi anaconda/pkgs/main/noarch::certifi-2020.6.20-pyhd3eb1b0_3
django anaconda/cloud/conda-forge/noarch::django-1.11.10-py_0
djangorestframewo~ anaconda/cloud/conda-forge/noarch::djangorestframework-3.9.4-py_0
pip anaconda/cloud/conda-forge/noarch::pip-20.1.1-pyh9f0ad1d_0
done
#
# To activate this environment, use
#
# $ conda activate python27-32
#
# To deactivate an active environment, use
#
# $ conda deactivate
C:\Users\DELL>conda env list
# conda environments:
#
base * D:\devTools\Python\Miniconda3
python27 D:\devTools\Python\Miniconda3\envs\python27
python27-32 D:\devTools\Python\Miniconda3\envs\python27-32
python35 D:\devTools\Python\Miniconda3\envs\python35
python36 D:\devTools\Python\Miniconda3\envs\python36
python38 D:\devTools\Python\Miniconda3\envs\python38
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
(python27-32) C:\Users\DELL>conda install -c pythonnet pythonnet
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
environment location: D:\devTools\Python\Miniconda3\envs\python27-32
added / updated specs:
- pythonnet
The following packages will be downloaded:
package | build
---------------------------|-----------------
pythonnet-2.3.0 | py27_0 63 KB pythonnet
------------------------------------------------------------
Total: 63 KB
The following NEW packages will be INSTALLED:
pythonnet pythonnet/win-32::pythonnet-2.3.0-py27_0
Proceed ([y]/n)? y
Downloading and Extracting Packages
pythonnet-2.3.0 | 63 KB | ############################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing 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-yasg
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
environment location: D:\devTools\Python\Miniconda3\envs\python27-32
added / updated specs:
- drf-yasg
The following packages will be downloaded:
package | build
---------------------------|-----------------
certifi-2018.4.16 | py27_0 143 KB conda-forge
coreapi-2.3.3 | pyh9f0ad1d_0 23 KB conda-forge
coreschema-0.0.4 | pyh9f0ad1d_0 15 KB conda-forge
drf-yasg-1.20.0 | py_0 1.3 MB conda-forge
inflection-0.5.1 | pyh9f0ad1d_0 9 KB conda-forge
itypes-1.2.0 | pyh9f0ad1d_0 7 KB conda-forge
jinja2-2.11.3 | pyh44b312d_0 93 KB conda-forge
markupsafe-1.0 | py27_0 25 KB conda-forge
packaging-20.9 | pyh44b312d_0 35 KB conda-forge
pyparsing-2.4.7 | pyh9f0ad1d_0 60 KB conda-forge
requests-2.12.5 | py27_0 763 KB conda-forge
ruamel.ordereddict-0.4.13 | py27_0 30 KB conda-forge
ruamel.yaml-0.15.37 | py27_0 235 KB conda-forge
simplejson-3.13.2 | py27_0 97 KB conda-forge
six-1.16.0 | pyh6c4a22f_0 14 KB conda-forge
typing-3.6.4 | py27_0 42 KB conda-forge
uritemplate-3.0.0 | py27_0 20 KB conda-forge
------------------------------------------------------------
Total: 2.8 MB
The following NEW packages will be INSTALLED:
coreapi conda-forge/noarch::coreapi-2.3.3-pyh9f0ad1d_0
coreschema conda-forge/noarch::coreschema-0.0.4-pyh9f0ad1d_0
drf-yasg conda-forge/noarch::drf-yasg-1.20.0-py_0
inflection conda-forge/noarch::inflection-0.5.1-pyh9f0ad1d_0
itypes conda-forge/noarch::itypes-1.2.0-pyh9f0ad1d_0
jinja2 conda-forge/noarch::jinja2-2.11.3-pyh44b312d_0
markupsafe conda-forge/win-32::markupsafe-1.0-py27_0
packaging conda-forge/noarch::packaging-20.9-pyh44b312d_0
pyparsing conda-forge/noarch::pyparsing-2.4.7-pyh9f0ad1d_0
requests conda-forge/win-32::requests-2.12.5-py27_0
ruamel.ordereddict conda-forge/win-32::ruamel.ordereddict-0.4.13-py27_0
ruamel.yaml conda-forge/win-32::ruamel.yaml-0.15.37-py27_0
simplejson conda-forge/win-32::simplejson-3.13.2-py27_0
six conda-forge/noarch::six-1.16.0-pyh6c4a22f_0
typing conda-forge/win-32::typing-3.6.4-py27_0
uritemplate conda-forge/win-32::uritemplate-3.0.0-py27_0
done
(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 Channel
certifi 2018.4.16 py27_0 conda-forge
coreapi 2.3.3 pyh9f0ad1d_0 conda-forge
coreschema 0.0.4 pyh9f0ad1d_0 conda-forge
django 1.11.10 py_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
djangorestframework 3.9.4 py_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
drf-yasg 1.20.0 py_0 conda-forge
inflection 0.5.1 pyh9f0ad1d_0 conda-forge
itypes 1.2.0 pyh9f0ad1d_0 conda-forge
jinja2 2.11.3 pyh44b312d_0 conda-forge
markupsafe 1.0 py27_0 conda-forge
packaging 20.9 pyh44b312d_0 conda-forge
pip 9.0.1 py27_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
pyparsing 2.4.7 pyh9f0ad1d_0 conda-forge
python 2.7.13 1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
pythonnet 2.3.0 py27_0 pythonnet
pytz 2020.1 pyh9f0ad1d_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
requests 2.12.5 py27_0 conda-forge
ruamel.ordereddict 0.4.13 py27_0 conda-forge
ruamel.yaml 0.15.37 py27_0 conda-forge
setuptools 36.4.0 py27_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
simplejson 3.13.2 py27_0 conda-forge
six 1.16.0 pyh6c4a22f_0 conda-forge
typing 3.6.4 py27_0 conda-forge
uritemplate 3.0.0 py27_0 conda-forge
vc 9 h7299396_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
vs2008_runtime 9.00.30729.1 hfaea7d5_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
wheel 0.37.1 pyhd8ed1ab_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
wincertstore 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-test
CommandError: '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 /F
Folder PATH listing
Volume serial number is 34D6-1340
D:.
│ main.py
│
├───.idea
│ │ .gitignore
│ │ misc.xml
│ │ modules.xml
│ │ python27-32.iml
│ │ vcs.xml
│ │ workspace.xml
│ │
│ └───inspectionProfiles
│ profiles_settings.xml
│
└───u8_test
│ manage.py
│
└───u8_test
settings.py
urls.py
wsgi.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-v1
CommandError: '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 /F
Folder PATH listing
Volume serial number is 34D6-1340
D:.
│ manage.py
│
├───u8V1
│ │ admin.py
│ │ apps.py
│ │ models.py
│ │ tests.py
│ │ views.py
│ │ __init__.py
│ │
│ └───migrations
│ __init__.py
│
└───u8_test
settings.py
settings.pyc
urls.py
wsgi.py
__init__.py
__init__.pyc
(python27-32) D:\OtherProjects\action\practice-demo-python\python27-32\u8_test>
4.4、编写自己的业务代码
步骤:
1. 编写 view
2. 创建路由
3. 加入模块路由到总路由
4. 注册自己的模块
4.4.1、编写 view
打开 views.py ,位置:u8_test\u8V1\views.py