Мне не удается заставить его вызвать сценарий активации. Обычно в терминале я создаю виртуальную среду, а затем вызываю сценарий активации, используя .\venv\Scripts\Activate. Однако, поскольку я хочу, чтобы сценарий был гибким, я разрешаю (заставляю) пользователей выбирать имя для своей виртуальной среды.
В сценарии:
Код: Выделить всё
function Make-Python-Environment {
[CmdletBinding()] #make an advance function
Param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String]$VirtualEnvName,
[Parameter(Mandatory)]
[TransformBooleanInputs()] # not relevant to this, just parses string to boolean t/f
[switch]$IncludeDotEnv = $False
)
begin {
Write-Host "This is only a test"
}
end {
$ThisDir = (Get-Item .).FullName
if($IncludeDotEnv){
New-Item ".env" -Force
Write-Host ".env file has been created in the current directory."
} else {
Write-Host "Skipping .env creation."
}
if($VirtualEnvName){
py -m venv $VirtualEnvName
Write-Host "Created virtual environment at" (Join-Path -Path $ThisDir -ChildPath $VirtualEnvName)
.\$VirtualEnvName\Scripts\Activate # Make-Python-Environment
cmdlet Make-Python-Environment at command pipeline position 1
Supply values for the following parameters:
VirtualEnvName: fa
IncludeDotEnv: 1
This is only a test
Directory: C:\Users\Alan
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 09/01/2025 13:41 0 .env
.env file has been created in the current directory.
Created virtual environment at C:\Users\Alan\fa
.\$VirtualEnvName\Scripts\Activate : The term '.\$VirtualEnvName\Scripts\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 C:\Users\Alan\Documents\PythonSetup.ps1:54 char:9
+ .\$VirtualEnvName\Scripts\Activate
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (.\$VirtualEnvName\Scripts\Activate:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Подробнее здесь: https://stackoverflow.com/questions/793 ... ell-script
Мобильная версия