sublime text 2中怎样使用virtualenv环境进行build?


目录结构是这样的

/venv  # 这是virtualenv创建的虚拟目录
|--/bin
|--/include
`--/lib

/myapp # 这是我的flask应用地址
|--wsgi.py

在bash下运行 venv/bin/activate 后切换到虚拟环境,然后运行 wsgi.py 没有问题。
但是,在sublime text下点击【Tools】->【Build】提示出错,说找不到flask模块,flask模块是我在virtualenv下用pip安装的:

Traceback (most recent call last):
  File "/Users/Toon/Code/myapp/wsgi.py", line 4, in <module>
    from flask import Flask, request, session, redirect, url_for, abort, \
ImportError: No module named flask
[Finished in 0.1s with exit code 1]

于是我想,这里Build应该没有导入virtualenv下的site-packages,在网上搜了到了这个答案
http://stackoverflow.com/questions/11...
按照他的方法,我对工程文件myapp.sublime-project进行了下列设置

{
    "folders":
    [
    {
        "path": "/Users/Toon/Code/myapp"
    }
    ],
    "build_systems":
    [
        {
        "name": "Run Tests",
        "cmd": ["source", "/Users/Toon/Code/venv/bin/activate"],
        "working_dir": "/Users/Toon/Code/myapp",
        "cmd": ["python", "wsgi.py"]
        }
    ]
}

保存后build,仍然出现上面的错误提示,是不是site-packages荏苒没有被导入呢,问题出在哪了呢?

python sublime-text virtualenv

楓丶暮秋灬 11 years ago

你忘了指定python环境了和selecter了

"build_systems":
[
    {
        "name": "Run Tests",
        "env": {
            "PYTHONPATH": "/Users/Toon/Code/venv/bin/python:/Users/Toon/Code/venv/lib/python2.7/site-packages"
            },
        "cmd": ["python", "$file"],
        "selector": "source.python"
    }
]

这样就可以了,运行应该没问题了

希兹克利夫 answered 11 years ago

Your Answer