【发布时间】:2014-03-20 18:46:18
【问题描述】:
我经常发现自己为 Flask 应用程序重新创建文件结构,因此我决定编写一个脚本来为我完成所有这些工作。我希望脚本创建我需要的所有文件夹以及带有一些基本样板的文件,它确实如此,那部分工作正常。但是,我还想创建一个虚拟环境并将 Flask 安装到该环境。那就是我遇到问题的地方。该脚本会运行,但会将 Flask 安装到我的 Python 系统安装中。
我遵循了this question here 中的建议,但它不起作用。我正在 Chromebook 上通过 crouton 运行 Ubuntu 12.04.4 LTS。
#!/usr/bin/python
from os import mkdir, chdir, getcwd, system
import sys
APP_NAME = sys.argv[1]
ROOT = getcwd()
PROJECT_ROOT = ROOT + '/' + APP_NAME
# dictionary represents folder structure. Key is the folder name and the value is it's contents
folders = {APP_NAME : {'app' : {'static': {'css' : '', 'img' : '', 'js' : ''}, 'templates' : ''} } }
def create_folders(dic):
for key in dic:
if isinstance(dic[key], dict):
mkdir(key)
prev = getcwd() + '/' + key
chdir(prev)
create_folders(dic[key])
else:
mkdir(key)
create_folders(folders)
chdir(PROJECT_ROOT)
open('config.py', 'a').close()
with open('run.py', 'a') as run:
run.write("""stuff""")
with open('app/__init__.py', 'a') as init:
init.write("""stuff""")
with open('app/views.py', 'a') as views:
views.write("""stuff""")
open('app/models.py', 'a').close()
open('app/forms.py', 'a').close()
with open('app/templates/layout.html', 'a') as layout:
layout.write("""stuff""")
system('chmod a+x run.py')
system('virtualenv venv')
system('. venv/bin/activate;sudo pip install flask') # this does not seem to be working the way I am expecting it to
【问题讨论】:
标签: python python-2.7