【问题标题】:How to activate a virtualenv? [duplicate]如何激活虚拟环境? [复制]
【发布时间】:2021-08-01 13:51:27
【问题描述】:

我想安装 virtualenv。我输入

pip install virtualenv

反应是

Requirement already satisfied: virtualenv in c:\users\novin\appdata\roaming\python\python38\site-packages (20.6.0)
Requirement already satisfied: platformdirs<3,>=2 in c:\users\novin\appdata\roaming\python\python38\site-packages (from virtualenv) (2.2.0)
Requirement already satisfied: distlib<1,>=0.3.1 in c:\users\novin\appdata\roaming\python\python38\site-packages (from virtualenv) (0.3.2)
Requirement already satisfied: filelock<4,>=3.0.0 in c:\programdata\anaconda3\lib\site-packages (from virtualenv) (3.0.12)
Requirement already satisfied: backports.entry-points-selectable>=1.0.4 in c:\users\novin\appdata\roaming\python\python38\site-packages (from virtualenv) (1.1.0)
Requirement already satisfied: six<2,>=1.9.0 in c:\programdata\anaconda3\lib\site-packages (from virtualenv) (1.15.0)

当我想激活它时,输入“source bin/activate”,响应是“source:

The term 'source' 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
+ source bin/activate
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (source:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException"

【问题讨论】:

  • 是不是需要先使用命令virtualenv &lt;env directory name&gt;创建一个虚拟环境目录,然后使用source &lt;env directory name&gt;/bin/activate激活它?
  • 您使用的是什么操作系统?
  • source 在 PowerShell 中不起作用;那是针对 linux/unix/macos 等的。

标签: python django virtualenv


【解决方案1】:

首先,制作一个venv:

C:\Users\username> mkvirtualenv web

一个快速的解决方法是像这样激活 cmd:

PS C:\Users\username> cmd
Microsoft Windows [Version 10.0.19043.1151]
(c) Microsoft Corporation. All rights reserved.

C:\Users\username>workon web
(web) C:\Users\username>

如果您坚持使用 PowerShell,请运行:

PS C:\Users\username> .\\Envs\web\Scripts\activate.ps1

这里,Envs 是存放你的 venvs 的目录,对我来说这个目录是:C:\Users\username\Envs

但是,要使用它,您必须以管理员身份运行 PowerShell,然后运行:

PS C:\Users\username> Set-ExecutionPolicy Unrestricted

在此之后,您将能够运行您的虚拟环境。

【讨论】:

  • 谢谢。但它不起作用!
猜你喜欢
  • 2021-06-15
  • 1970-01-01
  • 2022-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 2021-09-14
  • 1970-01-01
相关资源
最近更新 更多