【问题标题】:How to run python script with python from virtual environment without cmd如何在没有 cmd 的虚拟环境中使用 python 运行 python 脚本
【发布时间】:2018-04-10 09:51:09
【问题描述】:

有谁知道如何在没有 cmd 的虚拟环境中使用 python 运行 python 脚本。在 Linux 中,使用 shebang 很容易。

问题是我已经安装了 Python2,并且在 MyProject 上我在 Python3 中创建了虚拟环境。 现在,当我在 MyProject 中单击 main.py 时出现语法错误,这是正常的,因为它调用 Python2,而在 main.py 中,我使用了 Python2 中不存在的 async def。

我必须在 main.py 中添加什么,以便它自动识别它应该由位于虚拟环境中的 python3 执行,而不是由分配默认 python2 的 windows 执行。

我不想使用 cmd,我希望所有在 main.py 中都需要在点击时运行该 py。

有什么想法吗?

【问题讨论】:

    标签: python virtualenv


    【解决方案1】:

    查看链接:Detect Python version at runtime

    您可以在代码中添加以下行。

    import sys
    if sys.version_info[0] < 3:
        raise "Must be using Python 3"
    else:
        from your_script import your_method
        your_method()
    

    【讨论】:

    • 好的,但这并不能解决任何问题,而且这个答案对我的问题没有任何解决方案......这只是检查当前python的版本。并且 SyntaxError 仍然会被抛出,因为在你甚至可以检查版本之前它必须通过解释器的语法检查以便你知道。
    • 在 else 块中调用你的脚本方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2016-07-05
    • 2020-02-10
    • 1970-01-01
    • 2020-01-15
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多