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
Related
I use Anaconda environments to manage my Python installations.
I have my Python projects organized in folders; every folder contains the code for one project and a file environments.yml that specifies the packages that a conda environment should have to run that specific Python project.
Often I start working on a project as follows:
I navigate to the project folder (in Total Commander, but that's irrelevant I think).
I look at the contents of the environment.yml file to see what conda environment needs to be used for this project
I start a Powershell session in the folder and activate the conda environment in Powershell with conda activate some_env_name.
I start the rest my programs, e.g. jupyter lab or spyder.
I feel the steps 2 and 3 should somehow be automatic, but how would I go about doing that?
After trying out various methods I settled on a function in my profile.ps1 file that gets triggered every time I start a new powershell session.
The function reads the environment name from the environment.yml file and tries to activate that environment:
function conda_here {
$first_line = Get-Content -Head 1 environment.yml # This returns "name: conda_env_name"
$env_name = $first_line.Split(": ")[-1] # This splits it into an array of [name, : , conda_env_name] and takes the last element
try {
conda activate $env_name
} catch {
Write-Host "Tried to activate environment $env_name, but failed." -ForeGroundColor Red
}
}
To let that function trigger on every powershell start I added the following after the function definition:
# Activate a conda environment when started in a dir with a .conda-env file
if ( Test-Path environment.yml ){
conda_here
}
Now whenever I start a powershell session in a folder with an environment.yml file the corresponding conda environment gets activated automatically.
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 am running some automated Python tests with GitLab-CI on a Windows 10 machine. The GitLab-Runner on the machine used to work with executor = "shell" using the simple Windows shell. This recently stopped working (The docs say support for this shell is depreceated) and the only way to get it work again has been to use the powershell instead with adding shell = "powershell" to our config.toml file. For the tests to run, we need to activate a conda environment. Unfortunately, this seems not to work via the powershell script that GitLab-CI creates for the job.
When I open the powershell manually logged in as the user that is executing the gitlab runner jobs, changing conda environments works. I have run conda init powershell and can change the environment with conda activate myenv. Yet, when I include the following in my gitlab-ci.yml file:
script:
- conda activate myenv
- conda list
the output from conda list confirms that the environment myenv is not activated and instead the base environment is used.
Also trying the absolute path like this
script:
- conda activate C:\Users\myuser\Miniconda3\envs\myenv
- conda list
does not work.
So it seems like I can manually activate the correct conda environment in the powershell, but activating the environment via the powershell script created by GitLab-CI does not work. Is there a fix for this problem? Any help is greatly appreciated.
Looks like gitlab executes each line of the script in a separate subshell. Combine the commands into a single line.
If that doesn't work, most conda commands will accept the name of the environment as parameter -n:
conda list -n myenv
conda install -n myenv PackageName
...
As long as you're just using conda, it shouldn't be necessary to activate the environment.
As it seems to be a problem within powershell but not with cmd, one could use the following in the gitlab-ci yaml:
- cmd '/C' 'conda activate myenv && python myunittests.py'
Conda 4.6.14 over-writes my default PowerShell prompt from:
C:\whatevs\okay>
to:
(env) PS>
but I ideally want what it was previously:
(env) C:\whatevs\okay>
How can I undo this change to my PowerShell prompt? I like knowing what path and Conda environment I'm using whenever I execute a command! Is there a setting I can change?
I know it's possible to modify the PowerShell prompt by editing $profile. However, I want to keep the (env). Is there some quick way to get the current Conda environment so I can include it into a custom path?
Details
The file being modified by conda init is $HOME\Documents\WindowsPowerShell\profile.ps1 who's contents are:
#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
This is an issue with Conda 4.6.14 and can be resolved by downgrading back to 4.6.12 or upgrade to the (currently) experimental 4.7.
I am trying to use packer to provision awscli on a Windows machine. To install the awscli is use the following PowerShell script:
$download_url = 'https://s3.amazonaws.com/aws-cli/AWSCLI64.msi'
$downloaddestination = 'C:\Program Files\awscli.msi'
$checkpath='C:\Program Files\Amazon\AWSCLI'
if (Test-Path $downloaddestination) {
# // File exists do nothing
} else {
# // File does not exist download it
(New-Object System.Net.WebClient).DownloadFile($download_url, $downloaddestination)
}
$env:SEE_MASK_NOZONECHECKS = 1
Start-Process $downloaddestination /qn -Wait | Out-Null
Start-Sleep -Seconds 60
if (Test-Path $checkpath) {
Write-Host "awscli installed"
} else {
Write-Host "Installation failed"
}
I am unable to install awscli, its getting failed to install the MSI package even though its able to download the packege.
I use Python to install the cli, it makes updating easier too. I normally use cloud formation, cloud formation uses the userdata to invoke a script that is run by powershell.
The script looks like this:
mkdir c:\setup-downloads
cd \setup-downloads
curl https://www.python.org/ftp/python/3.7.3/python-3.7.3-amd64.exe --output python-inst.exe
.\python-inst.exe /quiet InstallAllUsers=1 PrependPath=1 Include_test=0
Start-Sleep -s 30
$env:Path += ";C:\Program Files\Python37"
$env:Path += ";C:\Program Files\Python37\Scripts"
pip3 install awscli
Create a directory somewhere mkdir c:\setup-downloads . Change to that directory cd \setup-downloads I then use curl to download python: curl https://www.python.org/ftp/python/3.7.3/python-3.7.3-amd64.exe --output python-inst.exe
Then I run the install using quiet mode, and I set it up to install itself into the path, and installed for everyone .\python-inst.exe /quiet InstallAllUsers=1 PrependPath=1 Include_test=0
I wait for this to finish Start-Sleep -s 30 But you need to restart the powershell to actually access python so I run the following to set the environment variables:
$env:Path += ";C:\Program Files\Python37"
$env:Path += ";C:\Program Files\Python37\Scripts"
Now that I have python installed, and the environment variables configured I can install the cli as follows:
pip3 install awscli
If you run aws --version it works
I tried #Jasons answer, calling the PowerShell file from an autoattend syncronusCommand block, however, I could not get pip3 command to be recognised.. setting the python env path inline I felt was a little hacky.
Check out - https://www.python.org/download/releases/2.5/msi/ - which explains that when using an MSI file the - installation can be initiated programmatically.
My PowerShell file now looks like this
Invoke-WebRequest -Uri https://awscli.amazonaws.com/AWSCLIV2.msi -Outfile "C:\aws.msi"
Start-Process -Wait -FilePath msiexec -ArgumentList /i, "c:\aws.msi", /qn
Now I have automated the install of aws cli on windows server.
I would suggest changing the approach and installing aws cli via pip the Python package manager that comes with the default Python installation.
Follow this guide to install Python silently on windows. Then you can just run the command below and it will install the awscli.
pip install awscli
You can use pip on windows, it's easy:
1. download and Install Python 2.7 for Win x64 in Python27 folder (default) [I had trouble with Python3.0 but I may have installed it wrong]
https://www.python.org/downloads/
2. in control panel, system, advanced settings add (ie., "new") to the user or system variable:
PYTHONPATH %PYTHONPATH%;C:\Python27\Lib
3. in control panel, system, advanced settings, edit the system PATH variable and add at the end:
;C:\Python27;C:\Python27\Scripts;C:\Program Files\Amazon\AWSCLI\
4. download the awscli package from
https://s3.amazonaws.com/aws-cli/AWSCLI64.msi
5. now you can run in the Windows DOS Command box: "pip install --upgrade awscli"
6. you can run cli commands in Windows DOS Command box, powershell or powershell ise. First run "aws configure" in powershell or win/dos command box
7. then you can run any CLI or CLI SDK command in powershell ise