VSCode:运行代码不考虑virtualenv

2024-04-23 17:50:28 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

Tags: appsfrompytestimportbackendpyenvhome