Python教程

windows + apache2.4 + flask+ python3.7 虚拟环境部署

本文主要是介绍windows + apache2.4 + flask+ python3.7 虚拟环境部署,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、使用虚拟环境关键点

1、在虚拟环境Scripts中增加 activate_this.py 文件,3.7版本没有,3.5版本有,拷贝过去即可。

# activate_this.py
# -*- coding: utf-8 -*-
"""Activate virtualenv for current interpreter:

Use exec(open(this_file).read(), {'__file__': this_file}).

This can be used when you must use an existing Python interpreter, not the virtualenv bin/python.
"""
import os
import site
import sys

try:
    abs_file = os.path.abspath(__file__)
except NameError:
    raise AssertionError("You must use exec(open(this_file).read(), {'__file__': this_file}))")

bin_dir = os.path.dirname(abs_file)
base = bin_dir[: -len("Scripts") - 1]  # strip away the bin part from the __file__, plus the path separator

# prepend bin to PATH (this file is inside the bin directory)
os.environ["PATH"] = os.pathsep.join([bin_dir] + os.environ.get("PATH", "").split(os.pathsep))
os.environ["VIRTUAL_ENV"] = base  # virtual env is right above bin directory

# add the virtual environments libraries to the host python import mechanism
prev_length = len(sys.path)
for lib in "..\Lib\site-packages".split(os.pathsep):
    path = os.path.realpath(os.path.join(bin_dir, lib))
    site.addsitedir(path.decode("utf-8") if "" else path)
sys.path[:] = sys.path[prev_length:] + sys.path[0:prev_length]

sys.real_prefix = sys.prefix
sys.prefix = base

2、添加一个.wsgi文件

import sys, os

activate_this = r'XXX\myvenv\Scripts\activate_this.py'
#exec(compile(open(activate_this, "rb").read(), activate_this, 'exec'), dict(__file__=activate_this))

with open(activate_this) as f:
    exec(f.read(),dict(__file__=activate_this))

#Expand Python classes path with your app's path
sys.path.insert(0, r'XXX\flask_blog')

from myblog import app as application

3、系统调试:调试.wsgi  文件 使用 python xxx.wsgi  不报错程序可正常运行

这篇关于windows + apache2.4 + flask+ python3.7 虚拟环境部署的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!