VSCode:运行代码不考虑virtualenv

时间:2019-02-24 05:59:56

标签: python visual-studio-code virtualenv

vscode :1.31.1 python扩展:2019.1.0

选定的Python解释器:(适当的pyenv环境),它显示在页脚处。

但是,当我打开一个简单的测试脚本时

from apps.business.periods import PeriodFactory
import sys


def init(config='config.settings.local'):
    import os
    import django

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", config)
    django.setup()


init()

if __name__ == '__main__':

    from apps.business.metrics.data import get_metric
    from apps.business.metrics.widgets import get_widget

    period = PeriodFactory.current_month()
    print(period)
    print(sys.prefix)
    print(sys.executable)

并运行它(右键单击-运行代码),出现错误:

[Running] python -u "/home/dmitry/Projects/analytics/backend/test.py"
Traceback (most recent call last):
  File "/home/dmitry/Projects/analytics/backend/test.py", line 1, in <module>
    from apps.business.periods import PeriodFactory
  File "/home/dmitry/Projects/analytics/backend/apps/business/periods.py", line 43
    range_entries: PeriodOrPeriodTupleList
                 ^
SyntaxError: invalid syntax

如果我删除了导入内容

import sys

if __name__ == '__main__':

    print(sys.prefix)
    print(sys.executable)

我得到以下输出

[Running] python -u "/home/dmitry/Projects/analytics/backend/test.py"
/usr
/usr/bin/python

如果我从外壳运行相同的脚本,一切正常

(cam) 
➜ BACKEND_ENVIRONMENT=local python test.py
2019-02 (Feb)
/home/dmitry/.pyenv/versions/cam
/home/dmitry/.pyenv/versions/cam/bin/python
(cam) 

0 个答案:

没有答案