安装Windows Django开发环境(Windows & Python & pip & Django)

因为开发工程的缘故,这里安装了特定版本的开发环境。
1. 操作系统:Windows 10 x64
2. 编译工具链:Visual Studio 2019(x64 Native Tools Command Prompt for VS 2019)
3. Python版本: 3.4.2
4. Django版本: 1.9.5
这里的Python和Django并不是最成熟的版本。如果想学习和测试,直接照着流程快速进入;如果是为了生产使用,请谨慎选择版本。
对于使用了不同的python和django版本的同学,所遇到的问题可能会略有不同, 如果发现了与本文相同的问题,可以借鉴解决。

1.安装python

1.1 使用安装包

安装包直接下载最新版

1.2 使用choco

使用管理员权限运行powershell,执行

choco install python3 -y

1.3 使用pyenv

1.3.1 安装pyenv

在linux和macOS下都是使用pyenv,windows下需要特殊处理,对应的版本是pyenv-win。
使用管理员权限运行powershell,执行

choco install pyenv-win -y

其他安装方法参照git介绍
在这里插入图片描述
在这里插入图片描述

1.3.2 pyenv 安装特定python

打开cmd, 执行

pyenv install 3.4.2 # 安装3.4.2版本
pyenv global 3.4.2 # 设置全局为3.4.2

更多pyenv的详细使用方法查看git仓库的说明文件

1.3.3 设置全局的pyenv

因为后面vscode的调试要使用python,所以务必务必让pyenv的路径成为最优先
打开cmd, 执行

where python
where pip

检查python和pip的第一行是否是pyenv里你选择的路径,如果不是就调整环境变量PATH里的顺序。

在这里插入图片描述

1.4 源码编译

Windows源码编译最好使用msvc,注意编译后的python必须和pip安装的库使用相同的crt运行时库。

2 pip镜像源

2.1 国内镜像

https://pypi.tuna.tsinghua.edu.cn/simple/	# 清华大学
https://mirrors.aliyun.com/pypi/simple/		# 阿里云
https://pypi.douban.com/simple/				# 豆瓣
https://pypi.mirrors.ustc.edu.cn/simple/	# 中国科学技术大学
https://pypi.hustunique.com/				# 华中科技大学

2.1 命令使用

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ pip

2.2 配置

在用户目录下新建一个pip文件夹
在pip文件夹下新建pip.ini文件
比如 C:\Users\xxxx\pip\pip.ini

[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = https://mirrors.aliyun.com

3. pip安装包

管理员权限打开cmd执行,-r requirements.txt 是包的依赖文件,-i 是使用阿里云的镜像加速
依赖文件格式样例如下

boto3==1.4.6
botocore==1.6.2
certifi==2017.4.17
chardet==3.0.4
configparser==3.5.0
crcmod==1.7
decorator==4.0.11
Django==1.9.5
django-crontab==0.7.1
docutils==0.14
gevent==1.2.2
greenlet==0.4.12
gunicorn==19.7.1
idna==2.5
ipython==6.1.0
ipython-genutils==0.2.0
jedi==0.10.2
Jinja2==2.9.6
jmespath==0.9.3
MarkupSafe==1.0
mysqlclient==1.3.10
oss2==2.3.3
pexpect==4.2.1
pickleshare==0.7.4
pika==0.10.0
prompt-toolkit==1.0.14
protobuf==3.3.0
psutil==5.6.3
ptyprocess==0.5.2
pyasn1==0.2.3
pycrypto==2.6.1
Pygments==2.2.0
PyJWT==1.7.1
python-dateutil==2.6.1
redis==2.10.5
requests==2.18.1
rsa==3.4.2
s3transfer==0.1.10
simplegeneric==0.8.1
six==1.10.0
traitlets==4.3.2
urllib3==1.21.1
wcwidth==0.1.7
xlrd==1.0.0

在已经安装环境的机器上可以通过

pip list

查看,通过

pip freeze > requirements.txt

来导出

这里通过install 重新安装

pip install -r D:\workspace\requirements.txt -i https://mirrors.aliyun.com/pypi/simple/

3.1 缺少vcvarsall.bat

error:Unable to find vcvasall.bat 

报错的意思是找不到vcvarsall.bat的文件,这个是Visual Studio里面的一个批处理文件,初始化编译环境用的。
按照报错内容查看文件

C:\Users\lzong\.pyenv\pyenv-win\versions\3.5.2\Lib\distutils\msvc9compiler.py

找到两个方法,第二个


def find_vcvarsall(version):
    """Find the vcvarsall.bat file

    At first it tries to find the productdir of VS 2008 in the registry. If
    that fails it falls back to the VS90COMNTOOLS env var.
    """
    vsbase = VS_BASE % version
    try:
        productdir = Reg.get_value(r"%s\Setup\VC" % vsbase,
                                   "productdir")
    except KeyError:
        log.debug("Unable to find productdir in registry")
        productdir = None

    if not productdir or not os.path.isdir(productdir):
        toolskey = "VS%0.f0COMNTOOLS" % version
        toolsdir = os.environ.get(toolskey, None)

        if toolsdir and os.path.isdir(toolsdir):
            productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC")
            productdir = os.path.abspath(productdir)
            if not os.path.isdir(productdir):
                log.debug("%s is not a valid directory" % productdir)
                return None
        else:
            log.debug("Env var %s is not set or invalid" % toolskey)
    if not productdir:
        log.debug("No productdir found")
        return None
    vcvarsall = os.path.join(productdir, "vcvarsall.bat")
    if os.path.isfile(vcvarsall):
        return vcvarsall
    log.debug("Unable to find vcvarsall.bat")
    return None

find_vcvarsall 说明了寻找vcvarall.bat的规则,先找注册表,再找环境变量
再看第二个方法:

def get_build_version():
    """Return the version of MSVC that was used to build Python.

    For Python 2.3 and up, the version number is included in
    sys.version.  For earlier versions, assume the compiler is MSVC 6.
    """
    prefix = "MSC v."
    i = sys.version.find(prefix)
    if i == -1:
        return 6
    i = i + len(prefix)
    s, rest = sys.version[i:].split(" ", 1)
    majorVersion = int(s[:-2]) - 6
    if majorVersion >= 13:
        # v13 was skipped and should be v14
        majorVersion += 1
    minorVersion = int(s[2:3]) / 10.0
    # I don't think paths are affected by minor version in version 6
    if majorVersion == 6:
        minorVersion = 0
    if majorVersion >= 6:
        return majorVersion + minorVersion
    # else we don't know what version of the compiler this is
    return None

get_build_version定义了Python版本对应Visual studio的版本的规则,解释出来如下
MSC v.1000 -> Visual C++ 4.x
MSC v.1100 -> Visual C++ 5
MSC v.1200 -> Visual C++ 6
MSC v.1300 -> Visual C++ .NET
MSC v.1310 -> Visual C++ .NET 2003
MSC v.1400 -> Visual C++ 2005 (8.0)
MSC v.1500 -> Visual C++ 2008 (9.0)
MSC v.1600 -> Visual C++ 2010 (10.0)
MSC v.1700 -> Visual C++ 2012 (11.0)
MSC v.1800 -> Visual C++ 2013 (12.0)
MSC v.1900 -> Visual C++ 2015 (14.0)
MSC v.1910 -> Visual C++ 2017 (15.0)
MSC v.1911 -> Visual C++ 2017 (15.3)
MSC v.1912 -> Visual C++ 2017 (15.5)
MSC v.1913 -> Visual C++ 2017 (15.6)
MSC v.1914 -> Visual C++ 2017 (15.7)
MSC v.1915 -> Visual C++ 2017 (15.8)
MSC v.1916 -> Visual C++ 2017 (15.9)

注册表的地址是

“Software\Wow6432Node\Microsoft\VisualStudio%0.1f”

检查注册表里面没有配置
再检查环境变量 VS140COMNTOOLS
环境变量里面没有则说明VS没有安装
在这里插入图片描述
根据版本计算规则,应该安装x64的visual studio 2015 版本,
网上有的“仅仅需要安装工具包”的说法并不可信,本人亲验失败,一定要安装完整的VC++安装包才行。

3.2 缺少cl.exe

Visual studio 安装不完整,卸载重新安装即可。

3.3 缺少rc.exe

在这里插入图片描述

找到自己操作系统版本所对应的rc.exe和rc.dll,拷贝到

C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin

目录下
python的安装程序太旧,导致无法找到操作系统自带的rc.exe和rc.dll,如果放在环境变量下可能会污染其他的工作环境,可以拷贝过去。

4 VSCode 开发环境

4.1 安装VSCode

官网(点击)自行下载安装

4.2 VSCode插件

安装 ms-python.python

call code --install-extension ms-python.python

安装 django插件

call code --install-extension batisteo.vscode-django

4.3 pylint

pylint可以帮助进行语法检查

pip install pylint

4.4 设置启动python

点击左下角的Python版本
在这里插入图片描述

选择你的python 二进制执行文件地址
在这里插入图片描述

系统默认没有找到pyenv的路径,路径手动设置
C:\Users\lzong.pyenv\pyenv-win\versions\3.5.2\

4.5 Debug启动

在debug界面点击配置按钮,选择django调试器。

在这里插入图片描述
F5启动Django项目

4.6 启动Django

进入工程目录,在manage.py 根目录执行

python manage.py runserver

会默认绑定8000端口启动

3.4 启动Django Crypto 报错

ImportError: No module named 'winrandom'

如果启动遇到以上错误,使用如下办法:

找到python3安装目录下的 lib/Crypto/Random/OSRNG/nt.py
找到如下代码段

	import winrandom

改成

	from Crypto.Random.OSRNG import winrandom
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值