PowerShell 中的 viralenv?

在 PowerShell 中使用 虚拟世界时似乎有一个问题。

当我尝试在 PowerShell 中激活环境时,比如..。

env/scripts/activate

. . 什么都没有发生。(shell 提示符应该已经改变了,PATH env. 变量也应该改变了。)

我想问题在于 PowerShell 生成了一个新的 cmd。进程只是为了运行 activate.bat,因此在完成后将 activate.bat对 shell 的更改呈现为死的。

你对这个问题有什么解决办法吗? (目前我还是坚持使用 cmd.exe)

132420 次浏览

My original answer is now outdated. Now, just use activate.ps1 (rather than activate.bat) to activate from a Powershell environment.


Original answer:

Here's a post that contains a Powershell script that allows you to run batch files that persistently modify their environment variables. The script propagates any environment variable changes back to the calling PowerShell environment.

A quick work-around would be to invoke cmd and then run your activate.bat from within the cmd session. For example:

PS C:\my_cool_env\Scripts> cmd
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.


C:\my_cool_env\Scripts>activate.bat
(my_cool_env) C:\my_cool_env\Scripts>

The latest version of virtualenv supports PowerShell out-of-the-box.

Just make sure you run:

Scripts\activate.ps1

instead of

Scripts\activate

The latter will execute activate.bat, which doesn't work on PowerShell.

try this: . .\env\Scripts\activate.ps1 watch dots and spaces

This error happens due to a security measure which won't let scripts be executed on your system without you having approved of it. You can do so by opening up a powershell with administrative rights (search for powershell in the main menu and select Run as administrator from the context menu) and entering:

set-executionpolicy remotesigned

for more: http://www.faqforge.com/windows/windows-powershell-running-scripts-is-disabled-on-this-system/

I wrote this quick little script to handle my activation and startup of a dev server.

$ep = Get-ExecutionPolicy


if ($ep -eq 'RemoteSigned') {


$root = "C:\Users\ALeven\OneDrive\!code_projects\!django_projects\"


$test = Read-Host -Prompt 'Would you like to activate the python environment? y/n'
if ($test -eq 'y') {


$activatestr = ($root + "\work_venv\Scripts\Activate.ps1")
& $activatestr


}


$test = Read-Host -Prompt 'Would you like to run the python server? y/n'


if ($test -eq 'y') {


$whichserver = Read-Host -Prompt 'Enter the name of the project.'
$path = ($root + $whichserver)
$runserverstr = ($path + "\manage.py")
python.exe $runserverstr runserver


}


} else {


Write-host "Execution Policy does not allow this script to run properly"
Write-host "If you have the proper permissions,"
Write-Host "Please close powershell,"
Write-host "then right click the powershell icon and run as administrator"
Write-host "Once in the powershell environment, execute the following:"
Write-host "Set-ExecutionPolicy RemoteSigned -Force"


}

Enjoy.

Inside of the Scripts directory of your virtual environments folder there are several activation scripts that can be used depending on where you are executing the command. If you are trying to activate your virtual env from the Windows PowerShell, try using the following command:

. .\env\Scripts\activate.ps1

In the event you receive an error about the activation script being disabled on your system, you will first need to invoke an execution policy change on your system. This will need to be done as the administrator.

To do this:

1) Right click on the PowerShell application and select Run as Administrator

2) Run the following command: Set-ExecutionPolicy Unrestricted

3) Rerun the activation command: . .\env\Scripts\activate.ps1

Windows users

In Powershell:

  1. Run Powershell as an administrator
  2. copy and paste this command: set-executionpolicy remotesigned
  3. Agree to the message.

Finally, Run

your_virtualenv_name\Scripts\activate.ps1

Instead of

your_virtualenv_name\Scripts\activate.bat

In CMD Just run:

your_virtualenv_name\Scripts\activate.bat

On Windows PowerShell, I have to type from the venv/Scripts folder :

. ./activate

I had that problem too! and finally found out what should we do in windows...

ok, follow these steps:

1)Type powershell in search bar of windows then right click on it and select Run as Administrator

(if you have problem in that check this)

2) Run the following command in powershell: Set-ExecutionPolicy Unrestricted

3) Rerun the activation command:.\\env\Scripts\activate.ps1

(just run the exact command! be careful about name of your environment.)

and that's it!:)

Run the this command:

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force

followed by:

./env/Scripts/activate.ps1

That's all

I wrote a little script to activate it.

# Don't forget to change execution policies
# Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser
# More info https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7


if (Test-Path env:VIRTUAL_ENV) {
deactivate
}


$env = .\venv\Scripts\activate.ps1


# use $env to set variables, for instance $env:FLASK_APP = "main.py"

Remember to save file with PowerShell extension .ps1.

Just 2 more suggestions:

Because it is always problematic to weaken a security policy i would advice to do it the minimal way for Powershell: invoke Powershell not as administrator but as the user who wants to use the virtualenv functions. Type "set-executionpolicy -executionpolicy unrestricted -scope currentuser". This way the policy is changed only for one user and not for the whole machine.

Secondly I would advice to download from github the sources "regisf/virtualenvwrapper-powershell". After download unpack the zip-file to a local directory and run the file "Install.ps1" inside. This will expand the Powershell profile permanently on your machine and hence enable the use of all "virtalenvwrapper-win" commands including "workon". After that you will not notice any difference in the behaviour of Powershell and the Commandshell concerning virtualenv.

Windows Power shell is consider powerful and "turbo-charged" than the normal command prompt shell. In an instance you are using visual studio code (VScode), running power shell to launch Python Virtual Environment may throw error especially on windows 10, as shown below.

\Activate : File C:\users\titus\django1\Scripts\Activate.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:1

To solve this, you can do the following:

  1. Go to windows Setting
  2. Open update & Security
  3. Select "For Developers"
  4. Enable the developers mode
  5. accept all the terms

Thank you all the best

For anyone still struggling to get things going, Windows PowerShell does not load commands from the current location by default. If you cd into the location of activate.ps1, running activate.ps1 could return "The term 'activate' is not recognized as the name of a cmdlet".

In order to run activate.ps1 from the current location try:

.\activate.ps1

firstly, make sure you are allowed to run Scripts. If not activate it. Now open the powershell pip install virtualenv (if virtualenv is not installed in your system) python -m virtualenv myenv (here "myenv" is the name of your environment)

#activating the virtual environment you created: myenv/Scripts/Activate.ps1

#deactivating the virtual environment: deactivate

  1. Search Windows Powershell
  2. Right click, select Run as Administrator; cofirm security pop-up if needed
  3. Enter: Set-ExecutionPolicy Unrestricted Enter: A
  4. cd.\env\Scripts\activate

No windows 10. CMD virtualenv venv/Script/activate.bat