简体   繁体   English

使用相当于“source”的命令在 Windows Powershell 中激活虚拟环境

[英]Activate a virtual environment in Windows Powershell with a command that is equivalent to "source"

I need to activate a virtual environment in Windows Powershell.我需要在 Windows Powershell 中激活虚拟环境。 But, the guide i am using that to proceed my project tells me to use a command like this:但是,引导我使用的是继续我的项目告诉我,使用这样的命令:

source venv-slither/bin/activate

In my project directory, in first, i installed virtualenv with:在我的项目目录中,首先,我安装了virtualenv

pip3 install virtualenv

After that, I setup a virtual environment in folder ./venv-slither with:之后,我在文件夹./venv-slither设置了一个虚拟环境:

source venv-slither/bin/activate

Problem emerges here.问题就出现在这里。 Whenever i run the last command i approaches to this message in Powershell:每当我运行最后一个命令时,我都会在 Powershell 中处理此消息:

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:2
+  source venv-slither/bin/activate
+  ~~~~~~
    + CategoryInfo          : ObjectNotFound: (source:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

I read some guide to eliminate this.我阅读了一些指南来消除这种情况。 They suggest using .他们建议使用. instead source , but by this way the same error occurs.而是source ,但通过这种方式会发生相同的错误。 When, my Command is:什么时候,我的命令是:

. venv-slither/bin/activate

The message is:消息是:

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

More over, when i change slashes direct:更重要的是,当我直接更改斜杠时:

. venv-slither\\bin\\activate

This message comes up:出现此消息:

. : The module 'venv-slither' could not be loaded. For more information, run 'Import-Module venv-slither'.
At line:1 char:3
+ . venv-slither\bin\activate
+   ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (venv-slither\bin\activate:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CouldNotAutoLoadModule

I have these modules in my project directory: .bin, dot, findit, source .我的项目目录中有这些模块: .bin, dot, findit, source Please help a beginner man in working with Windows Powershell.请帮助初学者使用 Windows Powershell。

I think you should use like this, within Windows PS:我认为你应该像这样在 Windows PS 中使用:

venv-slither/Scripts/activate.ps1

After enabling the necessary execution policy for the current user with this command:使用此命令为当前用户启用必要的执行策略后:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser

I would return to previous policy with this command, after you finish your work, for safety:在您完成工作后,为了安全起见,我将使用此命令返回到以前的策略:

Set-ExecutionPolicy -ExecutionPolicy Undefined -Scope CurrentUser

There are other ways to set the execution temporarily.还有其他方法可以临时设置执行。 For example, I can set for the current PS's session by issuing this command:例如,我可以通过发出以下命令来设置当前 PS 的会话:

powershell.exe -ExecutionPolicy RemoteSigned

* RemoteSigned or Unrestricted, you can refer to this link to view the details which scripts are or not allowed for each type of policy: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.1 * RemoteSigned 或 Unrestricted,您可以参考此链接查看每种策略允许或不允许哪些脚本的详细信息: https : //docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core /about/about_execution_policies?view=powershell-7.1

Sorry for posting too late, I've just run into your post when found the same issue, tested myself and it worked.抱歉发帖太晚了,我刚遇到你的帖子,发现同样的问题,自己测试了一下,效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM