我创建了一个virtualenv围绕我的项目,但当我尝试激活它时,我不能。 这可能只是语法或文件夹位置的问题,但我现在被难住了。

您可以在下面看到,我创建了virtualenv并将其命名为venv。一切看起来都很好,然后我尝试通过运行source venv/bin/activate来激活它

我想这可能只是与我的系统路径有关,但不确定指向什么(我确实知道如何编辑路径)。我使用的是python 7 / windows操作系统,虚拟环境2.2.x

Processing dependencies for virtualenv
Finished processing dependencies for virtualenv

c:\testdjangoproj\mysite>virtualenv --no-site-packages venv
The --no-site-packages flag is deprecated; it is now the default behavior.
Using real prefix 'C:\\Program Files (x86)\\Python'
New python executable in venv\Scripts\python.exe
File venv\Lib\distutils\distutils.cfg exists with different content; not overwri
ting
Installing setuptools.................done.
Installing pip...................done.

c:\testdjangoproj\mysite>source venv/bin/activate
'source' is not recognized as an internal or external command,
operable program or batch file.

c:\testdjangoproj\mysite>source venv/bin/activate
'source' is not recognized as an internal or external command,
operable program or batch file.

c:\testdjangoproj\mysite>source mysite/bin/activate
'source' is not recognized as an internal or external command,
operable program or batch file.

c:\testdjangoproj\mysite>

当前回答

有些人有问题与vscode我假设所有上述方法不工作。

这只是因为默认情况下vscode使用powershell而不是cmd… 单击它旁边的小箭头,选择CMD并运行命令。

其他回答

确保有venv,并遵循下面的命令。它适用于Windows 10。

转到您希望虚拟环境驻留的路径:

> cd <my_venv_path>

创建虚拟环境“env”:

> python -m venv env 

添加路径到git忽略文件(可选):

> echo env/ >> .gitignore

激活虚拟环境:

> .\env\Scripts\activate

source是一个shell命令,专为运行在Linux(或任何Posix,但不管怎样,不是Windows)上的用户而设计。

在Windows上,virtualenv会创建一个.bat/。ps1文件,所以你应该运行venv\Scripts\activate代替(根据virtualenv文档的激活脚本)。

只需运行activate,不需要扩展名,这样无论您使用cmd.exe还是PowerShell,都将使用正确的文件。

if .\venv\Scripts\activate也不工作,你会发现这个错误

\激活。Ps1无法加载,因为在此系统上禁用了运行脚本

你可以简单地在powershell中输入set-executionpolicy remotessigned,错误一定会消失。

Powershell应该以管理员身份运行

以管理员身份打开powershell 输入“Set-ExecutionPolicy remotessigned -Force” “gpedit运行。>管理模板>Windows组件>Windows Powershell 寻找“激活脚本执行”并将其设置为“激活” 设置执行指令为“允许全部” 应用 刷新你的环境

有些人有问题与vscode我假设所有上述方法不工作。

这只是因为默认情况下vscode使用powershell而不是cmd… 单击它旁边的小箭头,选择CMD并运行命令。