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.
Related
Where can you view the full history from all sessions in Windows Server 2016?
The following PowerShell command only includes the commands from the current session:
Get-History
In PowerShell enter the following command:
(Get-PSReadlineOption).HistorySavePath
This gives you the path where all of the history is saved. Then open the path in a text editor.
Try cat (Get-PSReadlineOption).HistorySavePath to list the history in PowerShell.
For getting full history from PowerShell and save the output to file I use this command:
Get-Content (Get-PSReadlineOption).HistorySavePath > D:\PowerShellHistory.txt
Since you are on windows you can also use below to open 'notepad' with it.
notepad (Get-PSReadlineOption).HistorySavePath
On windows PowerShell
To get in a session you can use
h or history
but to get all commands written in the computer you use
cat (Get-PSReadlineOption).HistorySavePath
There's mention of Windows Server/Enterprise editions, but as a Pro (standard retail version) user HistorySavePath is also available to me. I needed to see what python packages were recently installed in an older session and wanted to add an answer here for people looking for specific things in the history.
# if you like file names and line numbers included in your output
Select-String "<search pattern>" (Get-PSReadlineOption).HistorySavePath
# if you Just want the text without any of the other information
Get-Content (Get-PSReadlineOption).HistorySavePath | Select-String "<search pattern>"
In my case I ran
Select-String 'pip install' (Get-PSReadlineOption).HistorySavePath
which gave me a list of pip install commands run from my previous sessions
...
[Path/To/File]:10401:pip install dash
[Path/To/File]:10824:pip install -r .\requirements.txt
[Path/To/File]:11296:pip install flask-mysqldb
[Path/To/File]:11480:pip install Flask-Markdown
[Path/To/File]:11486:pip install pygments
[Path/To/File]:11487:pip install py-gfm
[Path/To/File]:11540:pip install bs4
The Psreadline module 2.1 beta1 on Powershell gallery (Powershell 7 only) https://www.powershellgallery.com/packages/PSReadLine/2.1.0-beta1 does intellisense on the commandline using the saved history: https://github.com/PowerShell/PSReadLine/issues/1468 It's been starting to show up in Vscode. https://www.reddit.com/r/PowerShell/comments/g33503/completion_on_history_in_vscode/
Also in Psreadline, you can search the saved history backwards with either f8 (after typing something on the command line) or control-R. Get-psreadlinekeyhandler lists the key bindings.
get-psreadlinekeyhandler -bound -unbound | ? function -match history
You can try this PowerShell Command History
I've been testing various ways to use conda/VS Code lately - before I just decided to add the conda path to my user Path variable. I don't recall the details, but at some point user profile "profile.ps1" scripts were added, possibly autogenerated by VS Code.
The scripts are located in user/Document/PowerShell and user/Documents/WindowsPowerShell and both contain the code
#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
(& "C:\Users\me\anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression
#endregion
The filenames of the scripts are saved in powershell $Profile variables.
Can anyone illuminate the origin of these scripts ? The are not from the conda installation...
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'
In virtualenvwrapper, is there a simple way to list all virtualenv on my machine?
(like what yolk -l does to list all python packages in the current virtual environment?)
CLARIFICATION: "ls -la" in my env directory does not count. I am looking for a virtualenv or virtualenvwrapper specific command.
You can use the lsvirtualenv, in which you have two options "long" or "brief":
"long" option is the default one, it searches for any hook you may have around this command and executes it, which takes more time.
"brief" just take the virtualenvs names and prints it.
brief usage:
$ lsvirtualenv -b
long usage:
$ lsvirtualenv -l
if you don't have any hooks, or don't even know what i'm talking about, just use "brief".
To list all the virtual environments (if using the anaconda distribution):
conda info --envs
Hope my answer helps someone...
Silly question. Found that there's a
lsvirtualenv
command which lists all existing virtualenv.
See the command documentation.
If you are using virtualenv or Python 3's built in venv the above answers might not work.
If you are on Linux, just locate the activate script that is always present inside a env.
locate -b '\activate' | grep "/home"
This will grab all Python virtual environments present inside your home directory.
For Mac Users, find works pretty good too
find $HOME -name "*activate" -type f
See Demo Here
Run workon with no argument to list available environments.
For conda created env use:
conda info --envs # or
conda info -e # or
conda env list
For virtualenvwrapper created env use:
lsvirtualenv
If you came here from Google, trying to find where your previously created virtualenv installation ended up, and why there is no command to find it, here's the low-down.
The design of virtualenv has a fundamental flaw of not being able to keep track of it's own created environments. Someone was not quite in their right mind when they created virtualenv without having a rudimentary way to keep track of already created environments, and certainly not fit for a time and age when most pip requirements require multi-giga-byte installations, which should certainly not go into some obscure .virtualenvs sub-directory of your ~/home.
IMO, the created virtualenv directory should be created in $CWD and a file called ~/.virtualenv (in home) should keep track of the name and path of that creation. Which is a darn good reason to use Conda/Miniconda3 instead, which does seem to keep good track of this.
As answered here, the only way to keep track of this, is to install yet another package called virtualenvwrapper. If you don't do that, you will have to search for the created directory by yourself. Clearly, if you don't remember the name or the location it was created with/at, you will most likely never find your virtual environment again...
One try to remedy the situation in windows, is by putting the following functions into your powershell profile:
# wrap virtualenv.exe and write last argument (presumably
# your virtualenv name) to the file: $HOME/.virtualenv.
function ven { if( $args.count -eq 0) {Get-Content ~/.virtualenv } else {virtualenv.exe "$args"; Write-Output ("{0} `t{1}" -f $args[-1],$PWD) | Out-File -Append $HOME/.virtualenv }}
# List what's in the file or the directories under ~/.virtualenvs
function lsven { try {Get-Content ~/.virtualenv } catch {Get-ChildItem ~\.virtualenvs -Directory | Select-Object -Property Name } }
WARNING: This will write to ~\.virtualenv...
If you use the recommended venvlink, then you can add the following powershell function, to list your available virtual environments.
# List what's in the directories of C:\venvs\
# - installed venvlink, with venvs in C:\venvs\
# - venvlink uses: ~/.venvlinkrc
function lsven { Get-ChildItem -Path C:\venvs\ -Name }
This can surely be improved to automatically detect the venvlink root directory.
To list all virtualenvs
conda env list
Output:
# conda environments:
#
D:\Programs\Anaconda3
D:\Programs\Anaconda3\envs\notebook
D:\Programs\Anaconda3\envs\snakes
D:\Programs\Anaconda3\envs\snowflakes
base * D:\Programs\Miniconda3
gluon D:\Programs\Miniconda3\envs\gluon
If using Anaconda conda env list
If using Python3 lsvirtualenv after you installed pip install virtualenvwrapper
Use below bash command to locate all virtual env in your system. You can modify the command according to your need to get in your desired format.
locate --regex "bin/activate"$ | sed 's/bin\/activate$//'
How do I find the Django virtual environment name if I forgot?. It is very Simple, you can find from the following location, if you forgot Django Virtual Environment name on Windows 10 Operating System.
c:\Users<name>\Envs<Virtual Environments>
The best answer I can find is we can check the installed python directory.
As for windows, the default directory for virtualenv and pipenv is.
/c/User/<username>/.virtualenv/
The directory(folder) inside the above directory shows all virtualenvs
Example -
ankit#ankit-PC MINGW64 ~/Desktop/study
$ ls /c/Users/ankit/.virtualenvs/
get_env_details* postactivate* postmkproject* postrmvirtualenv* predeactivate* premkvirtualenv* study-OwW1UW_H/
initialize* postdeactivate* postmkvirtualenv* preactivate* premkproject* prermvirtualenv*
study-OwW1UW_H/ is the only directory and virtualenv in the above example.
This works on Windows only:
If you are trying to find all envs created using virtualenv
search for "activate_this.py" or "pip-selfcheck.json"
if you're working on windows and conda, typing conda info --envs in cmd will show all the existing virtual envirentment.