I'm trying to set variable before start the Integrated Terminal
User Setting:
{
"terminal.integrated.env.windows": {
"foo": "bar"
}
}
Then View > Integrated Terminal
Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.
PS C:\bar> echo $foo
PS C:\bar>
It's seem not work,
It's bug or I'm wrong?
VSCode Version: 1.16.1
PS: Already seem Set global $PATH environment variable in VS Code
Finally, yume-chan answer by question in
Ref: https://github.com/Microsoft/vscode/issues/34420#issuecomment-329705397
It's should be echo $Env:foo instead of echo $foo
Related
Version Anaconda 4.8.3
What I cannot do:
I can't activate any environment from powershell.
conda activate base
What I can do:
conda env list
conda create -n xxx conda remove -n xxx
I have tried many solutions, but they turn out to be useless:
conda init powershell
restart powershell
conda activate
conda update -n base conda and redo 1
conda install -n root -c pscondaenvs pscondaenvs
Set-ExecutionPolicy RemoteSigned
Run activate base
The result of running conda activate
You don't need Admin permission.
Once you install Anaconda or Miniconda on Windows, open a Anaconda Powershell Prompt from Start Menu.
Or, If you don't see it there, then assuming you have installed
miniconda3 at path C:\miniconda3\4.9.2, do:
powershell -ExecutionPolicy ByPass -NoExit -Command "& 'C:\miniconda3\4.9.2\shell\condabin\conda-hook.ps1' ; conda activate 'C:\miniconda3\4.9.2' "
Now try:
conda init powershell
and reopen powershell.
Additional note: By default conda will autoactivate itself, when we open terminal. If you prefer not, then disable auto-activation with:
conda config --set auto_activate_base false
Open a Anaconda Powershell Prompt from Start Menu.
Now Try:
conda init powershell
Now restart the powershell, if find some error like this in powershell:
\WindowsPowerShell\profile.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:3
Then change the execution policy. Type this code to powershell:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
To find more with execution policy you can visit this link.
Here is my easier solution which works with Anaconda, Miniconda, and even Miniforge:
Open Powershell and browse to condabin folder in your conda installation directory, for example: C:\Users\<username>\anaconda3\condabin
Run ./conda init powershell in that folder, and re-open the powershell.
Please note: If you encountered ps1 cannot be loaded because running scripts is disabled on this system, simply run the Powershell as Administrator and enter the following: Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
Restart the Powershell & Enjoy!
When you use Anaconda or Miniconda type in Anacondaprompt:
conda init powershell
In order to activate conda on Powershell, I just executed this command:
C:\Users\<username>\anaconda3\shell\condabin\conda-hook.ps1
Then I could see all environments without needing to restart Powershell.
conda env list
conda activate base
So you need to basically just run the conda-hook.ps1 script.
Encountered the same problem. The trick is to make sure that the environment is included in the PATH variable of Windows Powershell. To view the PATH variables enter the following in the powershell:
$env:Path.split(';')
If the environment you desire doesn't show up in the output list, as in
.....
.....
..../envs/yourEnv/...
.....
run the following command on cmd:
conda init powershell
then restart _Anaconda Powershell,
then:
conda activate yourEnv
Hopefully, now the conda prompt should now show your desired environment.
To solve the problem without changing te security policy on powershell, you need to be on powershell 7. Powershell 7 will let you use a \WindowsPowerShell\profile.ps1 file as startup script with default (restricted) security policy settings. Windows Powershell is by default (on windows 10 at 14-08-2022) version 5.x.
Check your powershell version with
$PSVersionTable
If you're still on powershell 5, install powershell from the windows store. This will install powershell 7 alongside powershell 5. It will probably be called Powershell instead of Windows Powershell. Or if you're not allowed to use the windows store, ask IT to install it for you. Check the version again in the new powershell.
After this, we can follow the answer of prashant:
Open Anaconda prompt and run
conda init powershell
To stop conda starting by default run
conda config --set auto_activate_base false
& 'C:\ProgramData\Miniconda3\shell\condabin\conda-hook.ps1'; conda activate 'C:\ProgramData\Miniconda3'
This is the command found in Anaconda Powershell Prompt (Miniconda3).lnk on my computer.
On my machine, I can enter the environment anywhere by executing these two commands.
I'm new to WSL as a development environment, and I'm trying to set some environment variable to begin working on an open source project. In this case, a domain and secret key. I've followed the steps mentioned here https://devblogs.microsoft.com/commandline/share-environment-vars-between-wsl-and-windows/ and here https://medium.com/#kevinv92/how-to-use-wslenv-to-share-environment-variables-between-windows-and-wsl-c06099d5b901 and this is output:
Microsoft Windows [Version 10.0.19041.388]
(c) 2020 Microsoft Corporation. All rights reserved.
C:\Users\kyles>wsl -l -v
NAME STATE VERSION
* Ubuntu-20.04 Running 2
C:\Users\kyles>echo %WSLENV%
MVC_FIREBASE_API_KEY/p:MVC_FIREBASE_DOMAIN/p
C:\Users\kyles>echo %MVC_FIREBASE_DOMAIN%
minimum-viable-ceremonies-dev
C:\Users\kyles>wsl
kylesnowschwartz#DESKTOP-VN55S9F:/mnt/c/Users/kyles$ echo $WSLENV
MVC_FIREBASE_API_KEY/p:MVC_FIREBASE_DOMAIN/p
kylesnowschwartz#DESKTOP-VN55S9F:/mnt/c/Users/kyles$ echo $MVC_FIREBASE_DOMAIN
Nothing is output when for the environment variable, as I am expecting. I would appreciate any help in understanding what I don't understand!
The Microsoft blog entry is a bit confusing. Fortunately your problem is quite simple: You are using path translation option /p but your environment variable is not a path!
Passing environment variable from Windows to WSL, use /u option:
Microsoft Windows [Version 10.0.19044.1706]
(c) Microsoft Corporation. All rights reserved.
C:\Users\arttu>set MVC_FIREBASE_DOMAIN=minimum-viable-ceremonies-dev
C:\Users\arttu>set WSLENV=%WSLENV%:MVC_FIREBASE_DOMAIN/u
C:\Users\arttu>wsl
$ echo $MVC_FIREBASE_DOMAIN
minimum-viable-ceremonies-dev
I'm trying to add anaconda prompt to start up instead of powershell to avoid having to add python to env variables.
"terminal.integrated.shellArgs.windows": [
<args>
]
I tried putting them into single line, splitting them "-Foo Goo" as well as "-Foo","Goo". Each version leads to either error or simply ignoring the "-Command" parameter (the lines simply get pasted, but not executed).
First of all, I I'd like to give a hint for everyone that uses PowerShell to use the new one.
So, with the Anaconda ready to go (and it been equal or greater than 4.6 - use conda --version) run in sequence (from base environment in cwd terminal):
conda update conda
conda init
This will update your conda root environment and the init will setup all you need to run it on both cwd and powershell.
After this, you can start any powershell (inside vscode or not) and it will be conda ready.
Look at this article for further information.
Hope it helps!
I ended up using this (although it has tendency to break).
"terminal.integrated.shellArgs.windows": [
"-ExecutionPolicy"
, "ByPass"
, "-NoExit"
, "-Command"
, "& 'C:\\ProgramData\\Anaconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate 'C:\\ProgramData\\Anaconda3'"
],
Thanks Zerg! Your answer worked for me but I also got a warning message to say that this approach has been depreciated. After some googling I got this working by adding a new terminal profile to settings.json.
"terminal.integrated.profiles.windows": {
"PowerShell (Anaconda)": {
"source": "PowerShell",
"args": [
"-ExecutionPolicy"
, "ByPass"
, "-NoExit"
, "-Command"
, "& 'C:\\Users\\<username>\\AppData\\Local\\Continuum\\anaconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate 'C:\\Users\\<username>\\AppData\\Local\\Continuum\\anaconda3'"
]
}
},
Then changing the default profile:
"terminal.integrated.defaultProfile.windows": "PowerShell (Anaconda)",
I just installed PowerShell 7, and since I had anaconda installed before, this seems to add the starting command to the profile.ps1 automatically.
The profile.ps1 in C:\Users\USER\Documents\PowerShell (this is version 7, directory WindowsPowerShell would be the old version 5) contains:
#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
(& "C:\Users\USER\anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression
#endregion
With these automatic settings at the start of PowerShell 7, adding PowerShell 7 as a new terminal type to vsccode solved it.
This is how to add PowerShell 7 to the dropdown menu:
Enter Ctrl+Shift+P, open settings.json for the User, and add
{
"terminal.integrated.profiles.windows": {
"PowerShell7": {
"path": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
"args": ["-NoProfile",
"-noexit",
"-file",
"C:\\Users\\USER\\Documents\\PowerShell\\profile.ps1"]
}
},
"terminal.integrated.defaultProfile.windows": "PowerShell7"
}
Then in the settings.json, press Ctrl+s and restart (!) vscode. You will see PowerShell7 as the new default terminal in the dropdown of terminal types:
From the VSCode Command Palette (Ctrl+Shift+P), select
Terminal: Select default shell
and then pick PowerShell.
Then from the Command Palette (Ctrl+Shift+P), select
Python: Select Interpreter
and pick one of the conda environments. When you now open a new terminal VSCode starts PowerShell and activates the selected environment. This is exactly what the Anaconda-Prompt does. However, you should not set a PYTHONPATH in the environment in combination with an Anaconda install. Conda activation is all you need. It not only adds the selected interpreter to the PATH, but the required libraries too.
I'd like to have both an Anaconda python v2 and python v3 environment. I've run both Anaconda installers, working in Microsoft's powershell. Then to create the python3 env I run:
PS C:\Users\jo> conda create -n py3 python=3.4
Fetching package metadata: ....
Solving package specifications: .
Package plan for installation in environment C:\Anaconda\envs\py3:
The following NEW packages will be INSTALLED:
pip: 6.1.1-py34_0
python: 3.4.3-0
setuptools: 15.2-py34_0
Proceed ([y]/n)? y
Linking packages ...
[ COMPLETE ]|##################################################| 100%
#
# To activate this environment, use:
# > activate py3
#
However activating this env ignores the new env:
PS C:\Users\jo> activate py3
Activating environment "py3"...
PS C:\Users\jo> python
Python 2.7.9 |Anaconda 2.2.0 (64-bit)| (default, Dec 18 2014, 16:57:52) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>>
.. as we see by looking at which is current in the list of installed envs:
PS C:\Users\jo> conda env list
# conda environments:
#
py3 C:\Anaconda\envs\py3
root * C:\Anaconda
When sanity reigns, the activate command performs this switch by modifying the path variable. Is there something else I need to do in this environment to get it to work?
Switch first to cmd, then activate py3:
> cmd
> activate py3
If you run a cmd.exe shell script from PowerShell (a batch file), PowerShell spawns an instance of cmd.exe to run the script. If the batch file sets environment variables, they exist only in the spawned cmd.exe instance. Once that instance terminates (i.e., when the script ends), the environment variables do not propagate to the calling process (PowerShell, in this case). This is by design.
If you want to propagate environment variables, you can use the following Invoke-CmdScript function in PowerShell:
function Invoke-CmdScript {
param(
[String] $scriptName
)
$cmdLine = """$scriptName"" $args & set"
& $Env:SystemRoot\system32\cmd.exe /c $cmdLine |
Select-String '^([^=]*)=(.*)$' | ForEach-Object {
$varName = $_.Matches[0].Groups[1].Value
$varValue = $_.Matches[0].Groups[2].Value
Set-Item Env:$varName $varValue
}
}
Some more information about this in the following article:
Windows IT Pro: Take Charge of Environment Variables in PowerShell
After a quick google search I found this link: Mercurial: simple way to revert .orig files?, with the following line of code in the comment:
for /f %i in ('dir /s /b *.orig') do #copy %i %~dpni
In powershell, I tried running it, but I got the following error: Missing opening ( after kyeword for.
Is powershell not the correct way to run this code or is the syntax incorrect?
I am trying to revert my orig files back to their original version (get rid of the .orig extension). I am using windows so BASH is not an easy option.
The comment that snippet came from suggests that the environment you can run that line in is the "Windows command prompt with Command Extensions."
You can create that environment like so: cmd /e:on. It didn't seem like using powershell was the intention, but you could type cmd /e:on into the powershell console and get that environment.
PS C:\> cmd /e:on
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\> Command Prompt Input is Valid Here
You received that error because that syntax is incorrect for powershell -- for statements expect parameters in parenthesis. It should work once you're in the appropriate shell, though.