首页 > 解决方案 > 如何使用命令行在 Visual Studio Code 中为 python 设置虚拟环境

问题描述

我是一个非常新的程序员,现在我已经学习了 Python 的基础知识,正在尝试学习 Flask。我正在关注本教程https://www.youtube.com/watch?v=Z1RJmh_OqeA&t=355s ...我被困在 5:46。

本教程以及其他几个网站上的文档解释了键入以下命令:\env\Scripts\activate.batfor Windows,他们建议使用:source env/bin/activatefor Mac。

这是我得到的回报:

\env\Scripts\activate.bat : The term '\env\Scripts\activate.bat' is not recognized as the name of a 
cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was 
included, verify that the path is correct and try again.
At line:1 char:1
+ \env\Scripts\activate.bat
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (\env\Scripts\activate.bat:String) [], CommandNotFoundEx  
   ception
    + FullyQualifiedErrorId : CommandNotFoundException


Suggestion [3,General]: The command \env\Scripts\activate.bat was not found, but does exist in the current location. Windows PowerShell does not load commands from the current location by default. If you trust this command, instead type: ".\\env\Scripts\activate.bat". See "get-help about_Command_Precedence" for more details.

在此之前,其他一切都进行得很顺利。这就是我在此之前所做的:

pip3 install virtualenv
virtualenv env

运行这些命令后,本教程要求我运行我遇到问题的命令。

我有什么选择来解决这个问题?

标签: pythonpowershellvirtualenv

解决方案


您可能没有设置路径变量,因此系统找不到 Scripts 文件夹。在 Windows 上的典型 Python 安装中(您似乎正在使用它),Python 安装在 Program Files 的 C 驱动器下,并且该目录内是 Scripts 目录,它就是所在的位置virtualenv.exe

所以你需要打开控制面板,进入系统,点击高级系统设置,点击环境变量,然后在用户变量下突出显示路径,点击编辑,看看脚本的路径是否存在;它会是这样 C:\Program Files\Python37\Scripts 的:如果它不存在,您需要单击“新建”并输入它。


推荐阅读