admin管理员组文章数量:1123425
I'm trying to write some powershell scripts to "one-click" some setup for project initialisation.
I am unable to get it to call the activate script. Typically, in a terminal I would create the virtual environment, then call the activate script using .\venv\Scripts\Activate
. However, because I want to have the script be flexible, I am letting (forcing) users to pick a name for their virtual environment.
Onto the script:
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 #<-- point of failure
}
}
}
This is the output when I run the script:
PS C:\Users\Alan> 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
I have also tried wrapping the the VirtualEnvName in parenthesis .\($virtualEnvName)\Scripts\Activate
but that also fails.
I'm trying to write some powershell scripts to "one-click" some setup for project initialisation.
I am unable to get it to call the activate script. Typically, in a terminal I would create the virtual environment, then call the activate script using .\venv\Scripts\Activate
. However, because I want to have the script be flexible, I am letting (forcing) users to pick a name for their virtual environment.
Onto the script:
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 #<-- point of failure
}
}
}
This is the output when I run the script:
PS C:\Users\Alan> 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
I have also tried wrapping the the VirtualEnvName in parenthesis .\($virtualEnvName)\Scripts\Activate
but that also fails.
1 Answer
Reset to default 1What exactly is .\$VirtualEnvName\Scripts\Activate
and what are you expecting it to do?
From the context it sounds like it's a Python script perhaps, without a file extension? If so then you surely need to call Python itself and pass it that information, eg :
python .\$VirtualEnvName\Scripts\Activate
especially since without a file extension there's no way for the system to know what that file is.
Alternatively, while I've not tried calling Python scripts from PowerShell, when calling external PowerShell scripts you'd need to do something like :
& C:\path\myscript.ps1
or
& .\myscript.ps1
not just give it the file path and expect PowerShell to work out what to do with it.
本文标签: pythonTrouble calling an activate script from within a powershell scriptStack Overflow
版权声明:本文标题:python - Trouble calling an activate script from within a powershell script - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736569716a1944755.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论